session.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /******************************************************************************
  2. * Discordgo by Bruce Marriner <bruce@sqls.net>
  3. * A Discord API for Golang.
  4. */
  5. package discordgo
  6. import (
  7. "bytes"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "io/ioutil"
  12. "net/http"
  13. "time"
  14. )
  15. // A Session represents a connection to the Discord REST API.
  16. // Token : The authentication token returned from Discord
  17. // Debug : If set to ture debug logging will be displayed.
  18. type Session struct {
  19. Token string
  20. Debug bool
  21. }
  22. // RequestToken asks the Discord server for an authentication token
  23. func (session *Session) RequestToken(email string, password string) (token string, err error) {
  24. var urlStr string = fmt.Sprintf("%s/%s", discordApi, "auth/login")
  25. req, err := http.NewRequest("POST", urlStr, bytes.NewBuffer([]byte(fmt.Sprintf(`{"email":"%s", "password":"%s"}`, email, password))))
  26. if err != nil {
  27. return
  28. }
  29. req.Header.Set("Content-Type", "application/json")
  30. client := &http.Client{Timeout: (20 * time.Second)}
  31. resp, err := client.Do(req)
  32. if err != nil {
  33. return
  34. }
  35. defer resp.Body.Close()
  36. body, _ := ioutil.ReadAll(resp.Body)
  37. if resp.StatusCode != 200 {
  38. err = errors.New(fmt.Sprintf("StatusCode: %d, %s", resp.StatusCode, string(body)))
  39. return
  40. }
  41. if session.Debug {
  42. var prettyJSON bytes.Buffer
  43. error := json.Indent(&prettyJSON, body, "", "\t")
  44. if error != nil {
  45. fmt.Print("JSON parse error: ", error)
  46. return
  47. }
  48. fmt.Println("requestToken Response:\n", string(prettyJSON.Bytes()))
  49. }
  50. temp := &Session{} // TODO Must be a better way
  51. err = json.Unmarshal(body, &temp)
  52. token = temp.Token
  53. return
  54. }
  55. // Self returns a User structure of the session authenticated user.
  56. func (session *Session) Self() (user User, err error) {
  57. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, "users/@me"))
  58. err = json.Unmarshal(body, &user)
  59. return
  60. }
  61. // Request makes a REST API GET Request with Discord.
  62. func Request(session *Session, urlStr string) (body []byte, err error) {
  63. req, err := http.NewRequest("GET", urlStr, bytes.NewBuffer([]byte(fmt.Sprintf(``))))
  64. if err != nil {
  65. return
  66. }
  67. req.Header.Set("authorization", session.Token)
  68. req.Header.Set("Content-Type", "application/json")
  69. client := &http.Client{Timeout: (20 * time.Second)}
  70. resp, err := client.Do(req)
  71. if err != nil {
  72. return
  73. }
  74. body, err = ioutil.ReadAll(resp.Body)
  75. resp.Body.Close()
  76. if err != nil {
  77. return
  78. }
  79. if resp.StatusCode != 200 {
  80. err = errors.New(fmt.Sprintf("StatusCode: %d, %s", resp.StatusCode, string(body)))
  81. return
  82. }
  83. if session.Debug {
  84. var prettyJSON bytes.Buffer
  85. error := json.Indent(&prettyJSON, body, "", "\t")
  86. if error != nil {
  87. fmt.Print("JSON parse error: ", error)
  88. return
  89. }
  90. fmt.Println(urlStr+" Response:\n", string(prettyJSON.Bytes()))
  91. }
  92. return
  93. }
  94. // PrivateChannels returns an array of Channel structures for all private
  95. // channels of the session authenticated user.
  96. func (session *Session) PrivateChannels() (channels []Channel, err error) {
  97. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("users/@me/channels")))
  98. err = json.Unmarshal(body, &channels)
  99. return
  100. }
  101. // Servers returns an array of Server structures for all servers for the
  102. // session authenticated user.
  103. func (session *Session) Servers() (servers []Server, err error) {
  104. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("users/@me/guilds")))
  105. err = json.Unmarshal(body, &servers)
  106. return
  107. }
  108. // Channels returns an array of Channel structures for all channels of a given
  109. // server.
  110. func (session *Session) Channels(serverId int) (channels []Channel, err error) {
  111. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("guilds/%d/channels", serverId)))
  112. err = json.Unmarshal(body, &channels)
  113. return
  114. }