restapi.go 16 KB

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