discord.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015 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.8.0-alpha"
  15. /*
  16. type Config struct {
  17. Debug bool
  18. }
  19. */
  20. /*
  21. // possible future main struct for discord connection
  22. type Discord struct {
  23. Debug bool // Set to true to enable debug logging
  24. Token string // authentication token
  25. User User // authenticated user info
  26. Guilds []Guild // Cached Guild info
  27. Channels []Channel // Cached Channel info
  28. API api // all api endpoint functions
  29. DataWS dataWS // data websocket connection
  30. VoiceWS voiceWS // voice websocket/udp connections
  31. VoiceUDP voiceUDP
  32. }
  33. type api struct {
  34. Session
  35. }
  36. type dataWS struct {
  37. }
  38. type voiceWS struct {
  39. }
  40. type voiceUDP struct {
  41. }
  42. */
  43. // NOTICE: This function should be considered unstable because I am still
  44. // exploring the best way to implement my goals here. So, it is more likely
  45. // to change than than the low level API functions.
  46. //
  47. // New creates a new Discord session interface and will automate some startup
  48. // tasks if given enough information to do so. Currently you can pass zero
  49. // arguments and it will return an empty Discord session. If you pass a token
  50. // or username and password (in that order), then it will attempt to login to
  51. // Discord and open a websocket connection.
  52. func New(args ...interface{}) (s *Session, err error) {
  53. // Create an empty Session interface.
  54. s = &Session{
  55. State: NewState(),
  56. StateEnabled: true,
  57. }
  58. // If no arguments are passed return the empty Session interface.
  59. // Later I will add default values, if appropriate.
  60. if args == nil {
  61. return
  62. }
  63. // Varibles used below when parsing func arguments
  64. var auth, pass string
  65. // Parse passed arguments
  66. for _, arg := range args {
  67. switch v := arg.(type) {
  68. case []string:
  69. if len(v) > 2 {
  70. err = fmt.Errorf("Too many string parameters provided.")
  71. return
  72. }
  73. // First string is either token or username
  74. if len(v) > 0 {
  75. auth = v[0]
  76. }
  77. // If second string exists, it must be a password.
  78. if len(v) > 1 {
  79. pass = v[1]
  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 {
  90. err = fmt.Errorf("Too many string parameters provided.")
  91. return
  92. }
  93. // case Config:
  94. // TODO: Parse configuration
  95. default:
  96. err = fmt.Errorf("Unsupported parameter type provided.")
  97. return
  98. }
  99. }
  100. // If only one string was provided, assume it is an auth token.
  101. // Otherwise get auth token from Discord
  102. if pass == "" {
  103. s.Token = auth
  104. } else {
  105. s.Token, err = s.Login(auth, pass)
  106. if err != nil || s.Token == "" {
  107. err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
  108. return
  109. }
  110. }
  111. // TODO: Add code here to fetch authenticated user info like settings,
  112. // avatar, User ID, etc. If fails, return error.
  113. // Open websocket connection
  114. err = s.Open()
  115. if err != nil {
  116. fmt.Println(err)
  117. }
  118. // Do websocket handshake.
  119. err = s.Handshake()
  120. if err != nil {
  121. fmt.Println(err)
  122. }
  123. // Listen for events.
  124. go s.Listen()
  125. return
  126. }
  127. // Close closes a Discord session
  128. // TODO: Add support for Voice WS/UDP connections
  129. func (s *Session) Close() {
  130. s.DataReady = false
  131. close(s.listenChan)
  132. close(s.heartbeatChan)
  133. if s.wsConn != nil {
  134. s.wsConn.Close()
  135. }
  136. }