This repository has been archived by the owner on Jan 17, 2021. It is now read-only.
forked from bwmarrin/discordgo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
discord.go
150 lines (129 loc) · 4.78 KB
/
discord.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Discordgo - Discord bindings for Go
// Available at https://github.com/Bios-Marcel/discordgo
// Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file contains high level helper functions and easy entry points for the
// entire discordgo package. These functions are being developed and are very
// experimental at this point. They will most likely change so please use the
// low level functions if that's a problem.
// Package discordgo provides Discord binding for Go
package discordgo
import (
"errors"
"fmt"
"net/http"
"runtime"
"strings"
"time"
)
// ErrMFA will be risen by New when the user has 2FA.
var ErrMFA = errors.New("account has 2FA enabled")
const fakeBrowserUserAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0"
const botUserAgent = "Discord Bot (Bios-Marcel/discordgo)"
// NewWithToken creates a new Discord session and will use the given token
// for authorization.
func NewWithToken(token string) (s *Session, err error) {
s = createEmptySession()
//Make sure there's no unnecessary spaces / newlines from pasting.
token = strings.TrimSpace(token)
if strings.HasPrefix(strings.ToLower(token), "bot") {
//Cut off "bot", ignoring casing and make sure it's "Bot "
token = "Bot " + strings.TrimSpace(string([]rune(token)[3:]))
s.Identify.Intents = MakeIntent(IntentsAllWithoutPrivileged)
s.UserAgent = botUserAgent
} else {
s.MFA = strings.HasPrefix(token, "mfa")
s.UserAgent = fakeBrowserUserAgent
}
s.Token = token
s.Identify.Token = token
// The Session is now able to have RestAPI methods called on it.
// It is recommended that you now call Open() so that events will trigger.
return s, nil
}
func createEmptySession() *Session {
session := &Session{
State: NewState(),
Ratelimiter: NewRatelimiter(),
StateEnabled: true,
Compress: true,
ShouldReconnectOnError: true,
ShardID: 0,
ShardCount: 1,
MaxRestRetries: 3,
Client: &http.Client{Timeout: (20 * time.Second)},
sequence: new(int64),
LastHeartbeatAck: time.Now().UTC(),
}
// These can be modified prior to calling Open()
// Initilize the Identify Package with defaults
session.Identify.Compress = true
session.Identify.LargeThreshold = 250
session.Identify.GuildSubscriptions = true
session.Identify.Properties.OS = runtime.GOOS
session.Identify.Properties.Browser = "Firefox"
session.Identify.Intents = MakeIntent(IntentsAll)
return session
}
// NewWithPassword creates a new Discord session and will sign in with the
// provided credentials.
//
// NOTE: While email/pass authentication is supported by DiscordGo it is
// HIGHLY DISCOURAGED by Discord. Please only use email/pass to obtain a token
// and then use that authentication token for all future connections.
// Also, doing any form of automation with a user (non Bot) account may result
// in that account being permanently banned from Discord.
func NewWithPassword(username, password string) (s *Session, err error) {
s = createEmptySession()
s.UserAgent = fakeBrowserUserAgent
_, err = s.Login(username, password)
if err != nil || s.Token == "" {
if s.MFA {
err = ErrMFA
} else {
err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
}
}
return
}
// The Session is now able to have RestAPI methods called on it.
// It is recommended that you now call Open() so that events will trigger.
// NewWithPasswordAndMFA that also takes a MFA token generated by an authenticator.
//
// NOTE: While email/pass authentication is supported by DiscordGo it is
// HIGHLY DISCOURAGED by Discord. Please only use email/pass to obtain a token
// and then use that authentication token for all future connections.
// Also, doing any form of automation with a user (non Bot) account may result
// in that account being permanently banned from Discord.
func NewWithPasswordAndMFA(username, password, mfaToken string) (s *Session, err error) {
s = createEmptySession()
s.UserAgent = fakeBrowserUserAgent
var loginInfo *LoginInfo
loginInfo, err = s.Login(username, password)
if err != nil || s.Token == "" {
if s.MFA {
if mfaToken == "" {
err = ErrMFA
} else {
if loginInfo == nil {
err = ErrMFA
return
}
var token string
token, err = s.totp(loginInfo.Ticket, mfaToken)
if err != nil {
return
}
s.Token = token
s.Identify.Token = token
}
} else {
err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
}
return
}
// The Session is now able to have RestAPI methods called on it.
// It is recommended that you now call Open() so that events will trigger.
return
}