discord.go 1.9 KB

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