restapi.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /******************************************************************************
  2. * A Discord API for Golang.
  3. * See discord.go for more information.
  4. *
  5. * This file contains functions for interacting with the Discord HTTP REST API
  6. * at the lowest level.
  7. */
  8. package discordgo
  9. import (
  10. "bytes"
  11. "encoding/json"
  12. "errors"
  13. "fmt"
  14. "io/ioutil"
  15. "net/http"
  16. "time"
  17. )
  18. // Request makes a (GET/POST/?) Requests to Discord REST API.
  19. // All the other Discord REST Calls in this file use this function.
  20. func (s *Session) Request(method, urlStr, body string) (response []byte, err error) {
  21. if s.Debug {
  22. fmt.Println("REQUEST :: " + method + " " + urlStr + "\n" + body)
  23. }
  24. req, err := http.NewRequest(method, urlStr, bytes.NewBuffer([]byte(body)))
  25. if err != nil {
  26. return
  27. }
  28. // Not used on initial login..
  29. if s.Token != "" {
  30. req.Header.Set("authorization", s.Token)
  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. response, err = ioutil.ReadAll(resp.Body)
  39. if err != nil {
  40. return
  41. }
  42. resp.Body.Close()
  43. if resp.StatusCode != 204 && resp.StatusCode != 200 {
  44. err = fmt.Errorf("StatusCode: %d, %s", resp.StatusCode, string(response))
  45. return
  46. }
  47. if s.Debug {
  48. printJSON(response)
  49. }
  50. return
  51. }
  52. /***************************************************************************************************
  53. * Functions specific to this session.
  54. */
  55. // Login asks the Discord server for an authentication token
  56. func (s *Session) Login(email string, password string) (token string, err error) {
  57. response, err := s.Request("POST", LOGIN, fmt.Sprintf(`{"email":"%s", "password":"%s"}`, email, password))
  58. var temp map[string]interface{}
  59. err = json.Unmarshal(response, &temp)
  60. token = temp["token"].(string)
  61. return
  62. }
  63. // Logout sends a logout request to Discord.
  64. // This does not seem to actually invalidate the token. So you can still
  65. // make API calls even after a Logout. So, it seems almost pointless to
  66. // even use.
  67. func (s *Session) Logout() (err error) {
  68. _, err = s.Request("POST", LOGOUT, fmt.Sprintf(`{"token": "%s"}`, s.Token))
  69. return
  70. }
  71. /***************************************************************************************************
  72. * Functions related to a specific user
  73. */
  74. // User returns the user details of the given userID
  75. // userID : A user ID or "@me" which is a shortcut of current user ID
  76. func (s *Session) User(userID string) (st User, err error) {
  77. body, err := s.Request("GET", USER(userID), ``)
  78. err = json.Unmarshal(body, &st)
  79. return
  80. }
  81. // UserAvatar returns a ?? of a users Avatar
  82. // userID : A user ID or "@me" which is a shortcut of current user ID
  83. func (s *Session) UserAvatar(userID string) (st User, err error) {
  84. u, err := s.User(userID)
  85. _, err = s.Request("GET", USER_AVATAR(userID, u.Avatar), ``)
  86. // TODO need to figure out how to handle returning a file
  87. return
  88. }
  89. // UserSettings returns the settings for a given user
  90. // userID : A user ID or "@me" which is a shortcut of current user ID
  91. // This seems to only return a result for "@me"
  92. func (s *Session) UserSettings(userID string) (st Settings, err error) {
  93. body, err := s.Request("GET", USER_SETTINGS(userID), ``)
  94. err = json.Unmarshal(body, &st)
  95. return
  96. }
  97. // UserChannels returns an array of Channel structures for all private
  98. // channels for a user
  99. // userID : A user ID or "@me" which is a shortcut of current user ID
  100. func (s *Session) UserChannels(userID string) (st []Channel, err error) {
  101. body, err := s.Request("GET", USER_CHANNELS(userID), ``)
  102. err = json.Unmarshal(body, &st)
  103. return
  104. }
  105. // UserChannelCreate creates a new User (Private) Channel with another User
  106. // userID : A user ID or "@me" which is a shortcut of current user ID
  107. // recipientID : A user ID for the user to which this channel is opened with.
  108. func (s *Session) UserChannelCreate(userID, recipientID string) (st []Channel, err error) {
  109. body, err := s.Request(
  110. "POST",
  111. USER_CHANNELS(userID),
  112. fmt.Sprintf(`{"recipient_id": "%s"}`, recipientID))
  113. err = json.Unmarshal(body, &st)
  114. return
  115. }
  116. // UserGuilds returns an array of Guild structures for all guilds for a given user
  117. // userID : A user ID or "@me" which is a shortcut of current user ID
  118. func (s *Session) UserGuilds(userID string) (st []Guild, err error) {
  119. body, err := s.Request("GET", USER_GUILDS(userID), ``)
  120. err = json.Unmarshal(body, &st)
  121. return
  122. }
  123. /***************************************************************************************************
  124. * Functions related to a specific guild
  125. */
  126. // Guild returns a Guild structure of a specific Guild.
  127. // guildID : The ID of a Guild
  128. func (s *Session) Guild(guildID string) (st []Guild, err error) {
  129. body, err := s.Request("GET", GUILD(guildID), ``)
  130. err = json.Unmarshal(body, &st)
  131. return
  132. }
  133. // GuildCreate creates a new Guild
  134. // name : A name for the Guild (2-100 characters)
  135. func (s *Session) GuildCreate(name string) (st []Guild, err error) {
  136. body, err := s.Request("POST", GUILDS, fmt.Sprintf(`{"name":"%s"}`, name))
  137. err = json.Unmarshal(body, &st)
  138. return
  139. }
  140. // GuildEdit edits a new Guild
  141. // guildID : The ID of a Guild
  142. // name : A name for the Guild (2-100 characters)
  143. func (s *Session) GuildEdit(guildID, name string) (st []Guild, err error) {
  144. body, err := s.Request("POST", GUILD(guildID), fmt.Sprintf(`{"name":"%s"}`, name))
  145. err = json.Unmarshal(body, &st)
  146. return
  147. }
  148. // GuildDelete deletes or leaves a Guild.
  149. // guildID : The ID of a Guild
  150. func (s *Session) GuildDelete(guildID string) (st []Guild, err error) {
  151. body, err := s.Request("DELETE", GUILD(guildID), ``)
  152. err = json.Unmarshal(body, &st)
  153. return
  154. }
  155. // GuildBans returns an array of User structures for all bans of a
  156. // given guild.
  157. // guildID : The ID of a Guild.
  158. func (s *Session) GuildBans(guildID string) (st []User, err error) {
  159. body, err := s.Request("GET", GUILD_BANS(guildID), ``)
  160. err = json.Unmarshal(body, &st)
  161. return
  162. }
  163. // GuildBanAdd bans the given user from the given guild.
  164. // guildID : The ID of a Guild.
  165. // userID : The ID of a User
  166. func (s *Session) GuildBanAdd(guildID, userID string) (err error) {
  167. _, err = s.Request("PUT", GUILD_BAN(guildID, userID), ``)
  168. return
  169. }
  170. // GuildBanDelete removes the given user from the guild bans
  171. // guildID : The ID of a Guild.
  172. // userID : The ID of a User
  173. func (s *Session) GuildBanDelete(guildID, userID string) (err error) {
  174. _, err = s.Request("DELETE", GUILD_BAN(guildID, userID), ``)
  175. return
  176. }
  177. // GuildMembers returns an array of Member structures for all members of a
  178. // given guild.
  179. // guildID : The ID of a Guild.
  180. func (s *Session) GuildMembers(guildID string) (st []Member, err error) {
  181. body, err := s.Request("GET", GUILD_MEMBERS(guildID), ``)
  182. err = json.Unmarshal(body, &st)
  183. return
  184. }
  185. // GuildMemberDelete removes the given user from the given guild.
  186. // guildID : The ID of a Guild.
  187. // userID : The ID of a User
  188. func (s *Session) GuildMemberDelete(guildID, userID string) (err error) {
  189. _, err = s.Request("DELETE", GUILD_MEMBER_DEL(guildID, userID), ``)
  190. return
  191. }
  192. // GuildChannels returns an array of Channel structures for all channels of a
  193. // given guild.
  194. // guildID : The ID of a Guild.
  195. func (s *Session) GuildChannels(guildID string) (st []Channel, err error) {
  196. body, err := s.Request("GET", GUILD_CHANNELS(guildID), ``)
  197. err = json.Unmarshal(body, &st)
  198. return
  199. }
  200. // GuildChannelCreate creates a new channel in the given guild
  201. // guildID : The ID of a Guild.
  202. // name : Name of the channel (2-100 chars length)
  203. // ctype : Tpye of the channel (voice or text)
  204. func (s *Session) GuildChannelCreate(guildID, name, ctype string) (st []Channel, err error) {
  205. body, err := s.Request("POST", GUILD_CHANNELS(guildID), fmt.Sprintf(`{"name":"%s", "type":"%s"}`, name, ctype))
  206. err = json.Unmarshal(body, &st)
  207. return
  208. }
  209. // GuildInvites returns an array of Invite structures for the given guild
  210. // guildID : The ID of a Guild.
  211. func (s *Session) GuildInvites(guildID string) (st []Invite, err error) {
  212. body, err := s.Request("GET", GUILD_INVITES(guildID), ``)
  213. err = json.Unmarshal(body, &st)
  214. return
  215. }
  216. // GuildInviteCreate creates a new invite for the given guild.
  217. // guildID : The ID of a Guild.
  218. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  219. // and XkcdPass defined.
  220. func (s *Session) GuildInviteCreate(guildID string, i Invite) (st Invite, err error) {
  221. payload := fmt.Sprintf(
  222. `{"max_age":%d, "max_uses":%d, "temporary":%t, "xkcdpass":%t}`,
  223. i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass)
  224. body, err := s.Request("POST", GUILD_INVITES(guildID), payload)
  225. err = json.Unmarshal(body, &st)
  226. return
  227. }
  228. /***************************************************************************************************
  229. * Functions related to a specific channel
  230. */
  231. // Channel returns a Channel strucutre of a specific Channel.
  232. // channelID : The ID of the Channel you want returend.
  233. func (s *Session) Channel(channelID string) (st Channel, err error) {
  234. body, err := s.Request("GET", CHANNEL(channelID), ``)
  235. err = json.Unmarshal(body, &st)
  236. return
  237. }
  238. // ChannelEdit edits the given channel
  239. // channelID : The ID of a Channel
  240. // name : The new name to assign the channel.
  241. func (s *Session) ChannelEdit(channelID, name string) (st []Channel, err error) {
  242. body, err := s.Request("PATCH", CHANNEL(channelID), fmt.Sprintf(`{"name":"%s"}`, name))
  243. err = json.Unmarshal(body, &st)
  244. return
  245. }
  246. // ChannelDelete deletes the given channel
  247. // channelID : The ID of a Channel
  248. func (s *Session) ChannelDelete(channelID string) (st []Channel, err error) {
  249. body, err := s.Request("DELETE", CHANNEL(channelID), ``)
  250. err = json.Unmarshal(body, &st)
  251. return
  252. }
  253. // ChannelTyping broadcasts to all members that authenticated user is typing in
  254. // the given channel.
  255. // channelID : The ID of a Channel
  256. func (s *Session) ChannelTyping(channelID string) (err error) {
  257. _, err = s.Request("POST", CHANNEL_TYPING(channelID), ``)
  258. return
  259. }
  260. // ChannelMessages returns an array of Message structures for messaages within
  261. // a given channel.
  262. // channelID : The ID of a Channel.
  263. // limit : The number messages that can be returned.
  264. // beforeID : If provided all messages returned will be before given ID.
  265. // afterID : If provided all messages returned will be after given ID.
  266. func (s *Session) ChannelMessages(channelID string, limit int, beforeID int, afterID int) (st []Message, err error) {
  267. var urlStr string
  268. if limit > 0 {
  269. urlStr = fmt.Sprintf("?limit=%d", limit)
  270. }
  271. if afterID > 0 {
  272. if urlStr != "" {
  273. urlStr = urlStr + fmt.Sprintf("&after=%d", afterID)
  274. } else {
  275. urlStr = fmt.Sprintf("?after=%d", afterID)
  276. }
  277. }
  278. if beforeID > 0 {
  279. if urlStr != "" {
  280. urlStr = urlStr + fmt.Sprintf("&before=%d", beforeID)
  281. } else {
  282. urlStr = fmt.Sprintf("?before=%d", beforeID)
  283. }
  284. }
  285. body, err := s.Request("GET", CHANNEL_MESSAGES(channelID)+urlStr, ``)
  286. err = json.Unmarshal(body, &st)
  287. return
  288. }
  289. // ChannelMessageAck acknowledges and marks the given message as read
  290. // channeld : The ID of a Channel
  291. // messageID : the ID of a Message
  292. func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
  293. _, err = s.Request("POST", CHANNEL_MESSAGE_ACK(channelID, messageID), ``)
  294. return
  295. }
  296. // ChannelMessageSend sends a message to the given channel.
  297. // channelID : The ID of a Channel.
  298. // content : The message to send.
  299. func (s *Session) ChannelMessageSend(channelID string, content string) (st Message, err error) {
  300. response, err := s.Request("POST", CHANNEL_MESSAGES(channelID), fmt.Sprintf(`{"content":"%s"}`, content))
  301. err = json.Unmarshal(response, &st)
  302. return
  303. }
  304. // ChannelMessageEdit edits an existing message, replacing it entirely with
  305. // the given content.
  306. // channeld : The ID of a Channel
  307. // messageID : the ID of a Message
  308. func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (st Message, err error) {
  309. response, err := s.Request("PATCH", CHANNEL_MESSAGE(channelID, messageID), fmt.Sprintf(`{"content":"%s"}`, content))
  310. err = json.Unmarshal(response, &st)
  311. return
  312. }
  313. // ChannelMessageDelete deletes a message from the Channel.
  314. func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) {
  315. _, err = s.Request("DELETE", CHANNEL_MESSAGE(channelID, messageID), ``)
  316. return
  317. }
  318. // ChannelInvites returns an array of Invite structures for the given channel
  319. // channelID : The ID of a Channel
  320. func (s *Session) ChannelInvites(channelID string) (st []Invite, err error) {
  321. body, err := s.Request("GET", CHANNEL_INVITES(channelID), ``)
  322. err = json.Unmarshal(body, &st)
  323. return
  324. }
  325. // ChannelInviteCreate creates a new invite for the given channel.
  326. // channelID : The ID of a Channel
  327. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  328. // and XkcdPass defined.
  329. func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st Invite, err error) {
  330. payload := fmt.Sprintf(
  331. `{"max_age":%d, "max_uses":%d, "temporary":%t, "xkcdpass":%t}`,
  332. i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass)
  333. body, err := s.Request("POST", CHANNEL_INVITES(channelID), payload)
  334. err = json.Unmarshal(body, &st)
  335. return
  336. }
  337. /***************************************************************************************************
  338. * Functions related to an invite
  339. */
  340. // Invite returns an Invite structure of the given invite
  341. // inviteID : The invite code (or maybe xkcdpass?)
  342. func (s *Session) Invite(inviteID string) (st Invite, err error) {
  343. body, err := s.Request("GET", INVITE(inviteID), ``)
  344. err = json.Unmarshal(body, &st)
  345. return
  346. }
  347. // InviteDelete deletes an existing invite
  348. // inviteID : the code (or maybe xkcdpass?) of an invite
  349. func (s *Session) InviteDelete(inviteID string) (st Invite, err error) {
  350. body, err := s.Request("DELETE", INVITE(inviteID), ``)
  351. err = json.Unmarshal(body, &st)
  352. return
  353. }
  354. // InviteAccept accepts an Invite to a Guild or Channel
  355. // inviteID : The invite code (or maybe xkcdpass?)
  356. func (s *Session) InviteAccept(inviteID string) (st Invite, err error) {
  357. body, err := s.Request("POST", INVITE(inviteID), ``)
  358. err = json.Unmarshal(body, &st)
  359. return
  360. }
  361. /***************************************************************************************************
  362. * Functions related to Voice/Audio
  363. */
  364. // VoiceRegions returns the voice server regions
  365. func (s *Session) VoiceRegions() (st []VoiceRegion, err error) {
  366. body, err := s.Request("GET", VOICE_REGIONS, ``)
  367. err = json.Unmarshal(body, &st)
  368. return
  369. }
  370. // VoiceICE returns the voice server ICE information
  371. func (s *Session) VoiceICE() (st VoiceICE, err error) {
  372. body, err := s.Request("GET", VOICE_ICE, ``)
  373. err = json.Unmarshal(body, &st)
  374. return
  375. }
  376. /***************************************************************************************************
  377. * Functions related to Websockets
  378. */
  379. // Gateway returns the a websocket Gateway address
  380. func (s *Session) Gateway() (gateway string, err error) {
  381. response, err := s.Request("GET", GATEWAY, ``)
  382. var temp map[string]interface{}
  383. err = json.Unmarshal(response, &temp)
  384. gateway = temp["url"].(string)
  385. return
  386. }