discord.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. }
  57. // If no arguments are passed return the empty Session interface.
  58. // Later I will add default values, if appropriate.
  59. if args == nil {
  60. return
  61. }
  62. // Varibles used below when parsing func arguments
  63. var auth, pass string
  64. // Parse passed arguments
  65. for _, arg := range args {
  66. switch v := arg.(type) {
  67. case []string:
  68. if len(v) > 2 {
  69. err = fmt.Errorf("Too many string parameters provided.")
  70. return
  71. }
  72. // First string is either token or username
  73. if len(v) > 0 {
  74. auth = v[0]
  75. }
  76. // If second string exists, it must be a password.
  77. if len(v) > 1 {
  78. pass = v[1]
  79. }
  80. case string:
  81. // First string must be either auth token or username.
  82. // Second string must be a password.
  83. // Only 2 input strings are supported.
  84. if auth == "" {
  85. auth = v
  86. } else if pass == "" {
  87. pass = v
  88. } else {
  89. err = fmt.Errorf("Too many string parameters provided.")
  90. return
  91. }
  92. // case Config:
  93. // TODO: Parse configuration
  94. default:
  95. err = fmt.Errorf("Unsupported parameter type provided.")
  96. return
  97. }
  98. }
  99. // If only one string was provided, assume it is an auth token.
  100. // Otherwise get auth token from Discord
  101. if pass == "" {
  102. s.Token = auth
  103. } else {
  104. s.Token, err = s.Login(auth, pass)
  105. if err != nil || s.Token == "" {
  106. err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
  107. return
  108. }
  109. }
  110. // TODO: Add code here to fetch authenticated user info like settings,
  111. // avatar, User ID, etc. If fails, return error.
  112. // Open websocket connection
  113. err = s.Open()
  114. if err != nil {
  115. fmt.Println(err)
  116. }
  117. // Do websocket handshake.
  118. err = s.Handshake()
  119. if err != nil {
  120. fmt.Println(err)
  121. }
  122. // Listen for events.
  123. go s.Listen()
  124. return
  125. }