discord.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 beling developed and are very
  8. // experimental at this point. They will most likley 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.16.0-dev"
  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. // no verification of the token will be done and requests may fail.
  28. // IF THE TOKEN IS FOR A BOT, IT MUST BE PREFIXED WITH `BOT `
  29. // eg: `"Bot <token>"`
  30. // With an email and password - Discord will sign in with the provided
  31. // credentials.
  32. // With an email, password and auth token - Discord will verify the auth
  33. // token, if it is invalid it will sign in with the provided
  34. // credentials. This is the Discord recommended way to sign in.
  35. //
  36. // NOTE: While email/pass authentication is supported by DiscordGo it is
  37. // HIGHLY DISCOURAGED by Discord. Please only use email/pass to obtain a token
  38. // and then use that authentication token for all future connections.
  39. // Also, doing any form of automation with a user (non Bot) account may result
  40. // in that account being permanently banned from Discord.
  41. func New(args ...interface{}) (s *Session, err error) {
  42. // Create an empty Session interface.
  43. s = &Session{
  44. State: NewState(),
  45. ratelimiter: NewRatelimiter(),
  46. StateEnabled: true,
  47. Compress: true,
  48. ShouldReconnectOnError: true,
  49. ShardID: 0,
  50. ShardCount: 1,
  51. MaxRestRetries: 3,
  52. Client: &http.Client{Timeout: (20 * time.Second)},
  53. sequence: new(int64),
  54. }
  55. // If no arguments are passed return the empty Session interface.
  56. if args == nil {
  57. return
  58. }
  59. // Variables used below when parsing func arguments
  60. var auth, pass string
  61. // Parse passed arguments
  62. for _, arg := range args {
  63. switch v := arg.(type) {
  64. case []string:
  65. if len(v) > 3 {
  66. err = fmt.Errorf("too many string parameters provided")
  67. return
  68. }
  69. // First string is either token or username
  70. if len(v) > 0 {
  71. auth = v[0]
  72. }
  73. // If second string exists, it must be a password.
  74. if len(v) > 1 {
  75. pass = v[1]
  76. }
  77. // If third string exists, it must be an auth token.
  78. if len(v) > 2 {
  79. s.Token = v[2]
  80. }
  81. case string:
  82. // First string must be either auth token or username.
  83. // Second string must be a password.
  84. // Only 2 input strings are supported.
  85. if auth == "" {
  86. auth = v
  87. } else if pass == "" {
  88. pass = v
  89. } else if s.Token == "" {
  90. s.Token = v
  91. } else {
  92. err = fmt.Errorf("too many string parameters provided")
  93. return
  94. }
  95. // case Config:
  96. // TODO: Parse configuration struct
  97. default:
  98. err = fmt.Errorf("unsupported parameter type provided")
  99. return
  100. }
  101. }
  102. // If only one string was provided, assume it is an auth token.
  103. // Otherwise get auth token from Discord, if a token was specified
  104. // Discord will verify it for free, or log the user in if it is
  105. // invalid.
  106. if pass == "" {
  107. s.Token = auth
  108. } else {
  109. err = s.Login(auth, pass)
  110. if err != nil || s.Token == "" {
  111. if s.MFA {
  112. err = ErrMFA
  113. } else {
  114. err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
  115. }
  116. return
  117. }
  118. }
  119. // The Session is now able to have RestAPI methods called on it.
  120. // It is recommended that you now call Open() so that events will trigger.
  121. return
  122. }