discord.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.5.0"
  15. /*
  16. type Config struct {
  17. Debug bool
  18. }
  19. */
  20. // NOTICE: This function should be considered unstable because I am still
  21. // exploring the best way to implement my goals here. So, it is more likely
  22. // to change than than the low level API functions.
  23. //
  24. // New creates a new Discord session interface and will automate some startup
  25. // tasks if given enough information to do so. Currently you can pass zero
  26. // arguments and it will return an empty Discord session. If you pass a token
  27. // or username and password (in that order), then it will attempt to login to
  28. // Discord and open a websocket connection.
  29. func New(args ...interface{}) (s *Session, err error) {
  30. // Create an empty Session interface.
  31. s = &Session{}
  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. // Varibles 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) > 2 {
  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. case string:
  56. // First string must be either auth token or username.
  57. // Second string must be a password.
  58. // Only 2 input strings are supported.
  59. if auth == "" {
  60. auth = v
  61. } else if pass == "" {
  62. pass = v
  63. } else {
  64. err = fmt.Errorf("Too many string parameters provided.")
  65. return
  66. }
  67. // case Config:
  68. // TODO: Parse configuration
  69. default:
  70. err = fmt.Errorf("Unsupported parameter type provided.")
  71. return
  72. }
  73. }
  74. // If only one string was provided, assume it is an auth token.
  75. // Otherwise get auth token from Discord
  76. if pass == "" {
  77. s.Token = auth
  78. } else {
  79. s.Token, err = s.Login(auth, pass)
  80. if err != nil || s.Token == "" {
  81. err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
  82. return
  83. }
  84. }
  85. // TODO: Add code here to fetch authenticated user info like settings,
  86. // avatar, User ID, etc. If fails, return error.
  87. // Open websocket connection
  88. err = s.Open()
  89. if err != nil {
  90. fmt.Println(err)
  91. }
  92. // Do websocket handshake.
  93. err = s.Handshake()
  94. if err != nil {
  95. fmt.Println(err)
  96. }
  97. // Listen for events.
  98. go s.Listen()
  99. return
  100. }