123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package discordgo
- import (
- "errors"
- "fmt"
- "net/http"
- "time"
- )
- const VERSION = "0.17.0"
- var ErrMFA = errors.New("account has 2FA enabled")
- func New(args ...interface{}) (s *Session, err error) {
-
- s = &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(),
- }
-
- if args == nil {
- return
- }
-
- var auth, pass string
-
- for _, arg := range args {
- switch v := arg.(type) {
- case []string:
- if len(v) > 3 {
- err = fmt.Errorf("too many string parameters provided")
- return
- }
-
- if len(v) > 0 {
- auth = v[0]
- }
-
- if len(v) > 1 {
- pass = v[1]
- }
-
- if len(v) > 2 {
- s.Token = v[2]
- }
- case string:
-
-
-
- if auth == "" {
- auth = v
- } else if pass == "" {
- pass = v
- } else if s.Token == "" {
- s.Token = v
- } else {
- err = fmt.Errorf("too many string parameters provided")
- return
- }
-
-
- default:
- err = fmt.Errorf("unsupported parameter type provided")
- return
- }
- }
-
-
-
-
- if pass == "" {
- s.Token = auth
- } else {
- err = s.Login(auth, pass)
- if err != nil || s.Token == "" {
- if s.MFA {
- err = ErrMFA
- } else {
- err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
- }
- return
- }
- }
-
-
- return
- }
|