discord.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.
  6. // This file contains high level helper functions and easy entry points for the
  7. // entire discordgo package. These functions are being developed and are very
  8. // experimental at this point. They will most likely change so please use the
  9. // low level functions if that's a problem.
  10. // Package discordgo provides Discord binding for Go
  11. package discordgo
  12. import (
  13. "errors"
  14. "fmt"
  15. "net/http"
  16. "runtime"
  17. "time"
  18. )
  19. // VERSION of DiscordGo, follows Semantic Versioning. (http://semver.org/)
  20. const VERSION = "0.21.0-develop"
  21. // ErrMFA will be risen by New when the user has 2FA.
  22. var ErrMFA = errors.New("account has 2FA enabled")
  23. // New creates a new Discord session and will automate some startup
  24. // tasks if given enough information to do so. Currently you can pass zero
  25. // arguments and it will return an empty Discord session.
  26. // There are 3 ways to call New:
  27. // With a single auth token - All requests will use the token blindly,
  28. // no verification of the token will be done and requests may fail.
  29. // IF THE TOKEN IS FOR A BOT, IT MUST BE PREFIXED WITH `BOT `
  30. // eg: `"Bot <token>"`
  31. // With an email and password - Discord will sign in with the provided
  32. // credentials.
  33. // With an email, password and auth token - Discord will verify the auth
  34. // token, if it is invalid it will sign in with the provided
  35. // credentials. This is the Discord recommended way to sign in.
  36. //
  37. // NOTE: While email/pass authentication is supported by DiscordGo it is
  38. // HIGHLY DISCOURAGED by Discord. Please only use email/pass to obtain a token
  39. // and then use that authentication token for all future connections.
  40. // Also, doing any form of automation with a user (non Bot) account may result
  41. // in that account being permanently banned from Discord.
  42. func New(args ...interface{}) (s *Session, err error) {
  43. // Create an empty Session interface.
  44. s = &Session{
  45. State: NewState(),
  46. Ratelimiter: NewRatelimiter(),
  47. StateEnabled: true,
  48. Compress: true,
  49. ShouldReconnectOnError: true,
  50. ShardID: 0,
  51. ShardCount: 1,
  52. MaxRestRetries: 3,
  53. Client: &http.Client{Timeout: (20 * time.Second)},
  54. UserAgent: "DiscordBot (https://github.com/bwmarrin/discordgo, v" + VERSION + ")",
  55. sequence: new(int64),
  56. LastHeartbeatAck: time.Now().UTC(),
  57. }
  58. // Initilize the Identify Package with defaults
  59. // These can be modified prior to calling Open()
  60. s.Identify.Compress = true
  61. s.Identify.LargeThreshold = 250
  62. s.Identify.GuildSubscriptions = true
  63. s.Identify.Properties.OS = runtime.GOOS
  64. s.Identify.Properties.Browser = "DiscordGo v" + VERSION
  65. // If no arguments are passed return the empty Session interface.
  66. if args == nil {
  67. return
  68. }
  69. // Variables used below when parsing func arguments
  70. var auth, pass string
  71. // Parse passed arguments
  72. for _, arg := range args {
  73. switch v := arg.(type) {
  74. case []string:
  75. if len(v) > 3 {
  76. err = fmt.Errorf("too many string parameters provided")
  77. return
  78. }
  79. // First string is either token or username
  80. if len(v) > 0 {
  81. auth = v[0]
  82. }
  83. // If second string exists, it must be a password.
  84. if len(v) > 1 {
  85. pass = v[1]
  86. }
  87. // If third string exists, it must be an auth token.
  88. if len(v) > 2 {
  89. s.Identify.Token = v[2]
  90. s.Token = v[2] // TODO: Remove, Deprecated - Kept for backwards compatibility.
  91. }
  92. case string:
  93. // First string must be either auth token or username.
  94. // Second string must be a password.
  95. // Only 2 input strings are supported.
  96. if auth == "" {
  97. auth = v
  98. } else if pass == "" {
  99. pass = v
  100. } else if s.Token == "" {
  101. s.Identify.Token = v
  102. s.Token = v // TODO: Remove, Deprecated - Kept for backwards compatibility.
  103. } else {
  104. err = fmt.Errorf("too many string parameters provided")
  105. return
  106. }
  107. // case Config:
  108. // TODO: Parse configuration struct
  109. default:
  110. err = fmt.Errorf("unsupported parameter type provided")
  111. return
  112. }
  113. }
  114. // If only one string was provided, assume it is an auth token.
  115. // Otherwise get auth token from Discord, if a token was specified
  116. // Discord will verify it for free, or log the user in if it is
  117. // invalid.
  118. if pass == "" {
  119. s.Identify.Token = auth
  120. s.Token = auth // TODO: Remove, Deprecated - Kept for backwards compatibility.
  121. } else {
  122. err = s.Login(auth, pass)
  123. // TODO: Remove last s.Token part, Deprecated - Kept for backwards compatibility.
  124. if err != nil || s.Identify.Token == "" || s.Token == "" {
  125. if s.MFA {
  126. err = ErrMFA
  127. } else {
  128. err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
  129. }
  130. return
  131. }
  132. }
  133. return
  134. }