discord.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.11.0-alpha"
  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. // With an email and password - Discord will sign in with the provided
  22. // credentials.
  23. // With an email, password and auth token - Discord will verify the auth
  24. // token, if it is invalid it will sign in with the provided
  25. // credentials. This is the Discord recommended way to sign in.
  26. func New(args ...interface{}) (s *Session, err error) {
  27. // Create an empty Session interface.
  28. s = &Session{
  29. State: NewState(),
  30. StateEnabled: true,
  31. Compress: true,
  32. ShouldReconnectOnError: true,
  33. }
  34. // If no arguments are passed return the empty Session interface.
  35. // Later I will add default values, if appropriate.
  36. if args == nil {
  37. return
  38. }
  39. // Variables used below when parsing func arguments
  40. var auth, pass string
  41. // Parse passed arguments
  42. for _, arg := range args {
  43. switch v := arg.(type) {
  44. case []string:
  45. if len(v) > 3 {
  46. err = fmt.Errorf("Too many string parameters provided.")
  47. return
  48. }
  49. // First string is either token or username
  50. if len(v) > 0 {
  51. auth = v[0]
  52. }
  53. // If second string exists, it must be a password.
  54. if len(v) > 1 {
  55. pass = v[1]
  56. }
  57. // If third string exists, it must be an auth token.
  58. if len(v) > 2 {
  59. s.Token = v[2]
  60. }
  61. case string:
  62. // First string must be either auth token or username.
  63. // Second string must be a password.
  64. // Only 2 input strings are supported.
  65. if auth == "" {
  66. auth = v
  67. } else if pass == "" {
  68. pass = v
  69. } else if s.Token == "" {
  70. s.Token = v
  71. } else {
  72. err = fmt.Errorf("Too many string parameters provided.")
  73. return
  74. }
  75. // case Config:
  76. // TODO: Parse configuration
  77. default:
  78. err = fmt.Errorf("Unsupported parameter type provided.")
  79. return
  80. }
  81. }
  82. // If only one string was provided, assume it is an auth token.
  83. // Otherwise get auth token from Discord, if a token was specified
  84. // Discord will verify it for free, or log the user in if it is
  85. // invalid.
  86. if pass == "" {
  87. s.Token = auth
  88. } else {
  89. err = s.Login(auth, pass)
  90. if err != nil || s.Token == "" {
  91. err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
  92. return
  93. }
  94. }
  95. // The Session is now able to have RestAPI methods called on it.
  96. // It is recommended that you now call Open() so that events will trigger.
  97. return
  98. }