discord.go 3.0 KB

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