discord.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "time"
  17. )
  18. // VERSION of DiscordGo, follows Semantic Versioning. (http://semver.org/)
  19. const VERSION = "0.20.2"
  20. // ErrMFA will be risen by New when the user has 2FA.
  21. var ErrMFA = errors.New("account has 2FA enabled")
  22. // New creates a new Discord session and will automate some startup
  23. // tasks if given enough information to do so. Currently you can pass zero
  24. // arguments and it will return an empty Discord session.
  25. // There are 3 ways to call New:
  26. // With a single auth token - All requests will use the token blindly
  27. // (just tossing it into the HTTP Authorization header);
  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. // IF IT IS AN OAUTH2 ACCESS TOKEN, IT MUST BE PREFIXED WITH `Bearer `
  32. // eg: `"Bearer <token>"`
  33. // With an email and password - Discord will sign in with the provided
  34. // credentials.
  35. // With an email, password and auth token - Discord will verify the auth
  36. // token, if it is invalid it will sign in with the provided
  37. // credentials. This is the Discord recommended way to sign in.
  38. //
  39. // NOTE: While email/pass authentication is supported by DiscordGo it is
  40. // HIGHLY DISCOURAGED by Discord. Please only use email/pass to obtain a token
  41. // and then use that authentication token for all future connections.
  42. // Also, doing any form of automation with a user (non Bot) account may result
  43. // in that account being permanently banned from Discord.
  44. func New(args ...interface{}) (s *Session, err error) {
  45. // Create an empty Session interface.
  46. s = &Session{
  47. State: NewState(),
  48. Ratelimiter: NewRatelimiter(),
  49. StateEnabled: true,
  50. Compress: true,
  51. ShouldReconnectOnError: true,
  52. ShardID: 0,
  53. ShardCount: 1,
  54. MaxRestRetries: 3,
  55. Client: &http.Client{Timeout: (20 * time.Second)},
  56. UserAgent: "DiscordBot (https://github.com/bwmarrin/discordgo, v" + VERSION + ")",
  57. sequence: new(int64),
  58. LastHeartbeatAck: time.Now().UTC(),
  59. }
  60. // If no arguments are passed return the empty Session interface.
  61. if args == nil {
  62. return
  63. }
  64. // Variables used below when parsing func arguments
  65. var auth, pass string
  66. // Parse passed arguments
  67. for _, arg := range args {
  68. switch v := arg.(type) {
  69. case []string:
  70. if len(v) > 3 {
  71. err = fmt.Errorf("too many string parameters provided")
  72. return
  73. }
  74. // First string is either token or username
  75. if len(v) > 0 {
  76. auth = v[0]
  77. }
  78. // If second string exists, it must be a password.
  79. if len(v) > 1 {
  80. pass = v[1]
  81. }
  82. // If third string exists, it must be an auth token.
  83. if len(v) > 2 {
  84. s.Token = v[2]
  85. }
  86. case string:
  87. // First string must be either auth token or username.
  88. // Second string must be a password.
  89. // Only 2 input strings are supported.
  90. if auth == "" {
  91. auth = v
  92. } else if pass == "" {
  93. pass = v
  94. } else if s.Token == "" {
  95. s.Token = v
  96. } else {
  97. err = fmt.Errorf("too many string parameters provided")
  98. return
  99. }
  100. // case Config:
  101. // TODO: Parse configuration struct
  102. default:
  103. err = fmt.Errorf("unsupported parameter type provided")
  104. return
  105. }
  106. }
  107. // If only one string was provided, assume it is an auth token.
  108. // Otherwise get auth token from Discord, if a token was specified
  109. // Discord will verify it for free, or log the user in if it is
  110. // invalid.
  111. if pass == "" {
  112. s.Token = auth
  113. } else {
  114. err = s.Login(auth, pass)
  115. if err != nil || s.Token == "" {
  116. if s.MFA {
  117. err = ErrMFA
  118. } else {
  119. err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
  120. }
  121. return
  122. }
  123. }
  124. // The Session is now able to have RestAPI methods called on it.
  125. // It is recommended that you now call Open() so that events will trigger.
  126. return
  127. }