session.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /******************************************************************************
  2. * Discordgo v0 by Bruce Marriner <bruce@sqls.net>
  3. * A DiscordApp 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. import (
  11. "bytes"
  12. "encoding/json"
  13. "errors"
  14. "fmt"
  15. "io/ioutil"
  16. "net/http"
  17. "time"
  18. )
  19. // Represents a session connection to the Discord REST API.
  20. // I suspect I'll be adding more to this later :)
  21. type Session struct {
  22. Token string
  23. Debug bool
  24. }
  25. // RequestToken asks the Rest server for a token by provided email/password
  26. func (session *Session) RequestToken(email string, password string) (token string, err error) {
  27. var urlStr string = fmt.Sprintf("%s/%s", discordApi, "auth/login")
  28. req, err := http.NewRequest("POST", urlStr, bytes.NewBuffer([]byte(fmt.Sprintf(`{"email":"%s", "password":"%s"}`, email, password))))
  29. if err != nil {
  30. return
  31. }
  32. req.Header.Set("Content-Type", "application/json")
  33. client := &http.Client{Timeout: (20 * time.Second)}
  34. resp, err := client.Do(req)
  35. if err != nil {
  36. return
  37. }
  38. defer resp.Body.Close()
  39. body, _ := ioutil.ReadAll(resp.Body)
  40. if resp.StatusCode != 200 {
  41. err = errors.New(fmt.Sprintf("StatusCode: %d, %s", resp.StatusCode, string(body)))
  42. return
  43. }
  44. if session.Debug {
  45. var prettyJSON bytes.Buffer
  46. error := json.Indent(&prettyJSON, body, "", "\t")
  47. if error != nil {
  48. fmt.Print("JSON parse error: ", error)
  49. return
  50. }
  51. fmt.Println("requestToken Response:\n", string(prettyJSON.Bytes()))
  52. }
  53. temp := &Session{} // TODO Must be a better way
  54. err = json.Unmarshal(body, &temp)
  55. token = temp.Token
  56. return
  57. }
  58. // Identify session user
  59. func (session *Session) Self() (user User, err error) {
  60. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, "users/@me"))
  61. err = json.Unmarshal(body, &user)
  62. return
  63. }
  64. // Request makes a API GET Request. This is a general purpose function
  65. // and is used by all API functions. It is exposed currently so it can
  66. // also be used outside of this library.
  67. func Request(session *Session, urlStr string) (body []byte, err error) {
  68. req, err := http.NewRequest("GET", urlStr, bytes.NewBuffer([]byte(fmt.Sprintf(``))))
  69. if err != nil {
  70. return
  71. }
  72. req.Header.Set("authorization", session.Token)
  73. req.Header.Set("Content-Type", "application/json")
  74. client := &http.Client{Timeout: (20 * time.Second)}
  75. resp, err := client.Do(req)
  76. if err != nil {
  77. return
  78. }
  79. body, err = ioutil.ReadAll(resp.Body)
  80. resp.Body.Close()
  81. if err != nil {
  82. return
  83. }
  84. if resp.StatusCode != 200 {
  85. err = errors.New(fmt.Sprintf("StatusCode: %d, %s", resp.StatusCode, string(body)))
  86. return
  87. }
  88. if session.Debug {
  89. var prettyJSON bytes.Buffer
  90. error := json.Indent(&prettyJSON, body, "", "\t")
  91. if error != nil {
  92. fmt.Print("JSON parse error: ", error)
  93. return
  94. }
  95. fmt.Println(urlStr+" Response:\n", string(prettyJSON.Bytes()))
  96. }
  97. return
  98. }
  99. func (session *Session) Servers() (servers []Server, err error) {
  100. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("users/@me/guilds")))
  101. //body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("users/%s/guilds", session.Id)))
  102. err = json.Unmarshal(body, &servers)
  103. return
  104. }
  105. func (session *Session) Channels(serverId int) (channels []Channel, err error) {
  106. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("guilds/%d/channels", serverId)))
  107. err = json.Unmarshal(body, &channels)
  108. return
  109. }
  110. func (session *Session) PrivateChannels() (channels []Channel, err error) {
  111. body, err := Request(session, fmt.Sprintf("%s/%s", discordApi, fmt.Sprintf("users/@me/channels")))
  112. err = json.Unmarshal(body, &channels)
  113. return
  114. }