discord.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // Discordgo Version, follows Symantic Versioning. (http://semver.org/)
  14. const VERSION = "0.10.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. }
  32. // If no arguments are passed return the empty Session interface.
  33. // Later I will add default values, if appropriate.
  34. if args == nil {
  35. return
  36. }
  37. // Variables used below when parsing func arguments
  38. var auth, pass string
  39. // Parse passed arguments
  40. for _, arg := range args {
  41. switch v := arg.(type) {
  42. case []string:
  43. if len(v) > 3 {
  44. err = fmt.Errorf("Too many string parameters provided.")
  45. return
  46. }
  47. // First string is either token or username
  48. if len(v) > 0 {
  49. auth = v[0]
  50. }
  51. // If second string exists, it must be a password.
  52. if len(v) > 1 {
  53. pass = v[1]
  54. }
  55. // If third string exists, it must be an auth token.
  56. if len(v) > 2 {
  57. s.Token = v[2]
  58. }
  59. case string:
  60. // First string must be either auth token or username.
  61. // Second string must be a password.
  62. // Only 2 input strings are supported.
  63. if auth == "" {
  64. auth = v
  65. } else if pass == "" {
  66. pass = v
  67. } else if s.Token == "" {
  68. s.Token = v
  69. } else {
  70. err = fmt.Errorf("Too many string parameters provided.")
  71. return
  72. }
  73. // case Config:
  74. // TODO: Parse configuration
  75. default:
  76. err = fmt.Errorf("Unsupported parameter type provided.")
  77. return
  78. }
  79. }
  80. // If only one string was provided, assume it is an auth token.
  81. // Otherwise get auth token from Discord, if a token was specified
  82. // Discord will verify it for free, or log the user in if it is
  83. // invalid.
  84. if pass == "" {
  85. s.Token = auth
  86. } else {
  87. err = s.Login(auth, pass)
  88. if err != nil || s.Token == "" {
  89. err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
  90. return
  91. }
  92. }
  93. // TODO: Add code here to fetch authenticated user info like settings,
  94. // avatar, User ID, etc. If fails, return error.
  95. // Open websocket connection
  96. err = s.Open()
  97. if err != nil {
  98. fmt.Println(err)
  99. return
  100. }
  101. // Do websocket handshake.
  102. err = s.Handshake()
  103. if err != nil {
  104. fmt.Println(err)
  105. return
  106. }
  107. // Listen for events.
  108. go s.Listen()
  109. return
  110. }
  111. // Close closes a Discord session
  112. // TODO: Add support for Voice WS/UDP connections
  113. func (s *Session) Close() {
  114. s.DataReady = false
  115. if s.heartbeatChan != nil {
  116. select {
  117. case <-s.heartbeatChan:
  118. break
  119. default:
  120. close(s.heartbeatChan)
  121. }
  122. s.heartbeatChan = nil
  123. }
  124. if s.listenChan != nil {
  125. select {
  126. case <-s.listenChan:
  127. break
  128. default:
  129. close(s.listenChan)
  130. }
  131. s.listenChan = nil
  132. }
  133. if s.wsConn != nil {
  134. s.wsConn.Close()
  135. }
  136. }