discord.go 3.7 KB

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