discord.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /******************************************************************************
  2. * Discordgo v0 by Bruce Marriner <bruce@sqls.net>
  3. * A Discord API for Golang.
  4. *
  5. * Currently only the REST API is functional. I will add on the websocket
  6. * layer once I get the API section where I want it.
  7. *
  8. */
  9. package discordgo
  10. // These will absolutely change. I already don't like them.
  11. // Constants that define the different Discord API URLs.
  12. const (
  13. discordUrl = "http://discordapp.com"
  14. discordApi = discordUrl + "/api/"
  15. servers = discordApi + "guilds"
  16. channels = discordApi + "channels"
  17. users = discordApi + "users"
  18. )
  19. // A Discord structure represents a all-inclusive (hopefully) structure to
  20. // access the Discord REST API for a given authenticated user.
  21. type Discord struct {
  22. Session Session
  23. User User
  24. Servers []Server
  25. }
  26. // New creates a new connection to Discord and returns a Discord structure.
  27. // This provides an easy entry where most commonly needed information is
  28. // automatically fetched.
  29. func New(email string, password string) (d *Discord, err error) {
  30. session := Session{}
  31. session.Token, err = session.Login(email, password)
  32. if err != nil {
  33. return
  34. }
  35. user, err := session.Self()
  36. if err != nil {
  37. return
  38. }
  39. servers, err := session.Servers()
  40. d = &Discord{session, user, servers}
  41. return
  42. }
  43. // Renew essentially reruns the New command without creating a new session.
  44. // This will update all the user, server, and channel information that was
  45. // fetched with the New command. This is not an efficient way of doing this
  46. // but if used infrequently it does provide convenience.
  47. func (d *Discord) Renew() (err error) {
  48. d.User, err = Users(&d.Session, "@me")
  49. d.Servers, err = Servers(&d.Session, "@me")
  50. return
  51. }