discord.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "fmt"
  13. // VERSION of Discordgo, follows Symantic Versioning. (http://semver.org/)
  14. const VERSION = "0.15.0"
  15. // New creates a new Discord session and will automate some startup
  16. // tasks if given enough information to do so. Currently you can pass zero
  17. // arguments and it will return an empty Discord session.
  18. // There are 3 ways to call New:
  19. // With a single auth token - All requests will use the token blindly,
  20. // no verification of the token will be done and requests may fail.
  21. // IF THE TOKEN IS FOR A BOT, IT MUST BE PREFIXED WITH `BOT `
  22. // eg: `"Bot <token>"`
  23. // With an email and password - Discord will sign in with the provided
  24. // credentials.
  25. // With an email, password and auth token - Discord will verify the auth
  26. // token, if it is invalid it will sign in with the provided
  27. // credentials. This is the Discord recommended way to sign in.
  28. func New(args ...interface{}) (s *Session, err error) {
  29. // Create an empty Session interface.
  30. s = &Session{
  31. State: NewState(),
  32. ratelimiter: NewRatelimiter(),
  33. StateEnabled: true,
  34. Compress: true,
  35. ShouldReconnectOnError: true,
  36. ShardID: 0,
  37. ShardCount: 1,
  38. MaxRestRetries: 3,
  39. }
  40. // If no arguments are passed return the empty Session interface.
  41. if args == nil {
  42. return
  43. }
  44. // Variables used below when parsing func arguments
  45. var auth, pass string
  46. // Parse passed arguments
  47. for _, arg := range args {
  48. switch v := arg.(type) {
  49. case []string:
  50. if len(v) > 3 {
  51. err = fmt.Errorf("Too many string parameters provided.")
  52. return
  53. }
  54. // First string is either token or username
  55. if len(v) > 0 {
  56. auth = v[0]
  57. }
  58. // If second string exists, it must be a password.
  59. if len(v) > 1 {
  60. pass = v[1]
  61. }
  62. // If third string exists, it must be an auth token.
  63. if len(v) > 2 {
  64. s.Token = v[2]
  65. }
  66. case string:
  67. // First string must be either auth token or username.
  68. // Second string must be a password.
  69. // Only 2 input strings are supported.
  70. if auth == "" {
  71. auth = v
  72. } else if pass == "" {
  73. pass = v
  74. } else if s.Token == "" {
  75. s.Token = v
  76. } else {
  77. err = fmt.Errorf("Too many string parameters provided.")
  78. return
  79. }
  80. // case Config:
  81. // TODO: Parse configuration struct
  82. default:
  83. err = fmt.Errorf("Unsupported parameter type provided.")
  84. return
  85. }
  86. }
  87. // If only one string was provided, assume it is an auth token.
  88. // Otherwise get auth token from Discord, if a token was specified
  89. // Discord will verify it for free, or log the user in if it is
  90. // invalid.
  91. if pass == "" {
  92. s.Token = auth
  93. } else {
  94. err = s.Login(auth, pass)
  95. if err != nil || s.Token == "" {
  96. err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
  97. return
  98. }
  99. }
  100. // The Session is now able to have RestAPI methods called on it.
  101. // It is recommended that you now call Open() so that events will trigger.
  102. return
  103. }