restapi.go 16 KB

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