discord.go 3.7 KB

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