discord.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // A Discord structure represents a all-inclusive (hopefully) structure to
  12. // access the Discord REST API for a given authenticated user.
  13. type Discord struct {
  14. Session Session
  15. User User
  16. Servers []Server
  17. }
  18. // New creates a new connection to Discord and returns a Discord structure.
  19. // This provides an easy entry where most commonly needed information is
  20. // automatically fetched.
  21. // TODO add websocket code in here too
  22. func New(email string, password string) (d *Discord, err error) {
  23. session := Session{}
  24. session.Token, err = session.Login(email, password)
  25. if err != nil {
  26. return
  27. }
  28. user, err := session.Self()
  29. if err != nil {
  30. return
  31. }
  32. servers, err := session.Servers()
  33. d = &Discord{session, user, servers}
  34. return
  35. }
  36. // Renew essentially reruns the New command without creating a new session.
  37. // This will update all the user, server, and channel information that was
  38. // fetched with the New command. This is not an efficient way of doing this
  39. // but if used infrequently it does provide convenience.
  40. func (d *Discord) Renew() (err error) {
  41. d.User, err = Users(&d.Session, "@me")
  42. d.Servers, err = Servers(&d.Session, "@me")
  43. return
  44. }