restapi.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015-2016 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. "image"
  14. _ "image/jpeg" // For JPEG decoding
  15. _ "image/png" // For PNG decoding
  16. "io/ioutil"
  17. "net/http"
  18. "net/url"
  19. "strconv"
  20. "time"
  21. )
  22. // Request makes a (GET/POST/...) Requests to Discord REST API.
  23. // All the other Discord REST Calls in this file use this function.
  24. func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) {
  25. if s.Debug {
  26. fmt.Printf("API REQUEST %8s :: %s\n", method, urlStr)
  27. fmt.Println("API REQUEST PAYLOAD :: [" + fmt.Sprintf("%+v", data) + "]")
  28. }
  29. var body []byte
  30. if data != nil {
  31. body, err = json.Marshal(data)
  32. if err != nil {
  33. return
  34. }
  35. }
  36. req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(body))
  37. if err != nil {
  38. return
  39. }
  40. // Not used on initial login..
  41. // TODO: Verify if a login, otherwise complain about no-token
  42. if s.Token != "" {
  43. req.Header.Set("authorization", s.Token)
  44. }
  45. req.Header.Set("Content-Type", "application/json")
  46. // TODO: Make a configurable static variable.
  47. req.Header.Set("User-Agent", fmt.Sprintf("DiscordBot (https://github.com/bwmarrin/discordgo, v%s)", VERSION))
  48. if s.Debug {
  49. for k, v := range req.Header {
  50. fmt.Printf("API REQUEST HEADER :: [%s] = %+v\n", k, v)
  51. }
  52. }
  53. client := &http.Client{Timeout: (20 * time.Second)}
  54. resp, err := client.Do(req)
  55. defer resp.Body.Close()
  56. if err != nil {
  57. return
  58. }
  59. response, err = ioutil.ReadAll(resp.Body)
  60. if err != nil {
  61. return
  62. }
  63. if s.Debug {
  64. fmt.Printf("API RESPONSE STATUS :: %s\n", resp.Status)
  65. for k, v := range resp.Header {
  66. fmt.Printf("API RESPONSE HEADER :: [%s] = %+v\n", k, v)
  67. }
  68. fmt.Printf("API RESPONSE BODY :: [%s]\n", response)
  69. }
  70. // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  71. switch resp.StatusCode {
  72. case 200: // OK
  73. case 204: // No Content
  74. // TODO check for 401 response, invalidate token if we get one.
  75. case 429: // TOO MANY REQUESTS - Rate limiting
  76. // This will be changed to a more robust method later.
  77. // which may be hugely different as this method could cause
  78. // unending recursion
  79. rl := RateLimit{}
  80. err = json.Unmarshal(response, &rl)
  81. if err != nil {
  82. err = fmt.Errorf("Request unmarshal rate limit error : ", err)
  83. return
  84. }
  85. time.Sleep(rl.RetryAfter)
  86. response, err = s.Request(method, urlStr, data)
  87. default: // Error condition
  88. err = fmt.Errorf("HTTP %s, %s", resp.Status, response)
  89. }
  90. return
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. // Functions specific to Discord Sessions
  94. // ------------------------------------------------------------------------------------------------
  95. // Login asks the Discord server for an authentication token
  96. func (s *Session) Login(email string, password string) (token string, err error) {
  97. data := struct {
  98. Email string `json:"email"`
  99. Password string `json:"password"`
  100. }{email, password}
  101. response, err := s.Request("POST", LOGIN, data)
  102. if err != nil {
  103. return
  104. }
  105. temp := struct {
  106. Token string `json:"token"`
  107. }{}
  108. err = json.Unmarshal(response, &temp)
  109. if err != nil {
  110. return
  111. }
  112. token = temp.Token
  113. return
  114. }
  115. // Register sends a Register request to Discord, and returns the authentication token
  116. // Note that this account is temporary and should be verified for future use.
  117. // Another option is to save the authentication token external, but this isn't recommended.
  118. func (s *Session) Register(username string) (token string, err error) {
  119. data := struct {
  120. Username string `json:"username"`
  121. }{username}
  122. response, err := s.Request("POST", REGISTER, data)
  123. if err != nil {
  124. return
  125. }
  126. temp := struct {
  127. Token string `json:"token"`
  128. }{}
  129. err = json.Unmarshal(response, &temp)
  130. if err != nil {
  131. return
  132. }
  133. token = temp.Token
  134. return
  135. }
  136. // Logout sends a logout request to Discord.
  137. // This does not seem to actually invalidate the token. So you can still
  138. // make API calls even after a Logout. So, it seems almost pointless to
  139. // even use.
  140. func (s *Session) Logout() (err error) {
  141. // _, err = s.Request("POST", LOGOUT, fmt.Sprintf(`{"token": "%s"}`, s.Token))
  142. if s.Token == "" {
  143. return
  144. }
  145. data := struct {
  146. Token string `json:"token"`
  147. }{s.Token}
  148. _, err = s.Request("POST", LOGOUT, data)
  149. return
  150. }
  151. // ------------------------------------------------------------------------------------------------
  152. // Functions specific to Discord Users
  153. // ------------------------------------------------------------------------------------------------
  154. // User returns the user details of the given userID
  155. // userID : A user ID or "@me" which is a shortcut of current user ID
  156. func (s *Session) User(userID string) (st *User, err error) {
  157. body, err := s.Request("GET", USER(userID), nil)
  158. if err != nil {
  159. return
  160. }
  161. err = json.Unmarshal(body, &st)
  162. return
  163. }
  164. // UserAvatar returns an image.Image of a users Avatar
  165. // userID : A user ID or "@me" which is a shortcut of current user ID
  166. func (s *Session) UserAvatar(userID string) (img image.Image, err error) {
  167. u, err := s.User(userID)
  168. if err != nil {
  169. return
  170. }
  171. body, err := s.Request("GET", USER_AVATAR(userID, u.Avatar), nil)
  172. if err != nil {
  173. return
  174. }
  175. img, _, err = image.Decode(bytes.NewReader(body))
  176. return
  177. }
  178. // UserUpdate updates a users settings.
  179. func (s *Session) UserUpdate(email, password, username, avatar, newPassword string) (st *User, err error) {
  180. // NOTE: Avatar must be either the hash/id of existing Avatar or
  181. // data:image/png;base64,BASE64_STRING_OF_NEW_AVATAR_PNG
  182. // to set a new avatar.
  183. // If left blank, avatar will be set to null/blank
  184. data := struct {
  185. Email string `json:"email"`
  186. Password string `json:"password"`
  187. Username string `json:"username"`
  188. Avatar string `json:"avatar,omitempty"`
  189. NewPassword string `json:"new_password,omitempty"`
  190. }{email, password, username, avatar, newPassword}
  191. body, err := s.Request("PATCH", USER("@me"), data)
  192. if err != nil {
  193. return
  194. }
  195. err = json.Unmarshal(body, &st)
  196. return
  197. }
  198. // UserSettings returns the settings for a given user
  199. func (s *Session) UserSettings() (st *Settings, err error) {
  200. body, err := s.Request("GET", USER_SETTINGS("@me"), nil)
  201. if err != nil {
  202. return
  203. }
  204. err = json.Unmarshal(body, &st)
  205. return
  206. }
  207. // UserChannels returns an array of Channel structures for all private
  208. // channels.
  209. func (s *Session) UserChannels() (st []*Channel, err error) {
  210. body, err := s.Request("GET", USER_CHANNELS("@me"), nil)
  211. if err != nil {
  212. return
  213. }
  214. err = json.Unmarshal(body, &st)
  215. return
  216. }
  217. // UserChannelCreate creates a new User (Private) Channel with another User
  218. // recipientID : A user ID for the user to which this channel is opened with.
  219. func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) {
  220. data := struct {
  221. RecipientID string `json:"recipient_id"`
  222. }{recipientID}
  223. body, err := s.Request("POST", USER_CHANNELS("@me"), data)
  224. if err != nil {
  225. return
  226. }
  227. err = json.Unmarshal(body, &st)
  228. return
  229. }
  230. // UserGuilds returns an array of Guild structures for all guilds.
  231. func (s *Session) UserGuilds() (st []*Guild, err error) {
  232. body, err := s.Request("GET", USER_GUILDS("@me"), nil)
  233. if err != nil {
  234. return
  235. }
  236. err = json.Unmarshal(body, &st)
  237. return
  238. }
  239. // ------------------------------------------------------------------------------------------------
  240. // Functions specific to Discord Guilds
  241. // ------------------------------------------------------------------------------------------------
  242. // Guild returns a Guild structure of a specific Guild.
  243. // guildID : The ID of a Guild
  244. func (s *Session) Guild(guildID string) (st *Guild, err error) {
  245. body, err := s.Request("GET", GUILD(guildID), nil)
  246. if err != nil {
  247. return
  248. }
  249. err = json.Unmarshal(body, &st)
  250. return
  251. }
  252. // GuildCreate creates a new Guild
  253. // name : A name for the Guild (2-100 characters)
  254. func (s *Session) GuildCreate(name string) (st *Guild, err error) {
  255. data := struct {
  256. Name string `json:"name"`
  257. }{name}
  258. body, err := s.Request("POST", GUILDS, data)
  259. if err != nil {
  260. return
  261. }
  262. err = json.Unmarshal(body, &st)
  263. return
  264. }
  265. // GuildEdit edits a new Guild
  266. // guildID : The ID of a Guild
  267. // name : A name for the Guild (2-100 characters)
  268. func (s *Session) GuildEdit(guildID, name string) (st *Guild, err error) {
  269. data := struct {
  270. Name string `json:"name"`
  271. }{name}
  272. body, err := s.Request("POST", GUILD(guildID), data)
  273. if err != nil {
  274. return
  275. }
  276. err = json.Unmarshal(body, &st)
  277. return
  278. }
  279. // GuildDelete deletes or leaves a Guild.
  280. // guildID : The ID of a Guild
  281. func (s *Session) GuildDelete(guildID string) (st *Guild, err error) {
  282. body, err := s.Request("DELETE", GUILD(guildID), nil)
  283. if err != nil {
  284. return
  285. }
  286. err = json.Unmarshal(body, &st)
  287. return
  288. }
  289. // GuildBans returns an array of User structures for all bans of a
  290. // given guild.
  291. // guildID : The ID of a Guild.
  292. func (s *Session) GuildBans(guildID string) (st []*User, err error) {
  293. body, err := s.Request("GET", GUILD_BANS(guildID), nil)
  294. if err != nil {
  295. return
  296. }
  297. err = json.Unmarshal(body, &st)
  298. return
  299. }
  300. // GuildBanCreate bans the given user from the given guild.
  301. // guildID : The ID of a Guild.
  302. // userID : The ID of a User
  303. // days : The number of days of previous comments to delete.
  304. func (s *Session) GuildBanCreate(guildID, userID string, days int) (err error) {
  305. uri := GUILD_BAN(guildID, userID)
  306. if days > 0 {
  307. uri = fmt.Sprintf("%s?delete-message-days=%d", uri, days)
  308. }
  309. _, err = s.Request("PUT", uri, nil)
  310. return
  311. }
  312. // GuildBanDelete removes the given user from the guild bans
  313. // guildID : The ID of a Guild.
  314. // userID : The ID of a User
  315. func (s *Session) GuildBanDelete(guildID, userID string) (err error) {
  316. _, err = s.Request("DELETE", GUILD_BAN(guildID, userID), nil)
  317. return
  318. }
  319. // GuildMemberDelete removes the given user from the given guild.
  320. // guildID : The ID of a Guild.
  321. // userID : The ID of a User
  322. func (s *Session) GuildMemberDelete(guildID, userID string) (err error) {
  323. _, err = s.Request("DELETE", GUILD_MEMBER_DEL(guildID, userID), nil)
  324. return
  325. }
  326. // GuildChannels returns an array of Channel structures for all channels of a
  327. // given guild.
  328. // guildID : The ID of a Guild.
  329. func (s *Session) GuildChannels(guildID string) (st []*Channel, err error) {
  330. body, err := s.Request("GET", GUILD_CHANNELS(guildID), nil)
  331. if err != nil {
  332. return
  333. }
  334. err = json.Unmarshal(body, &st)
  335. return
  336. }
  337. // GuildChannelCreate creates a new channel in the given guild
  338. // guildID : The ID of a Guild.
  339. // name : Name of the channel (2-100 chars length)
  340. // ctype : Tpye of the channel (voice or text)
  341. func (s *Session) GuildChannelCreate(guildID, name, ctype string) (st *Channel, err error) {
  342. data := struct {
  343. Name string `json:"name"`
  344. Type string `json:"type"`
  345. }{name, ctype}
  346. body, err := s.Request("POST", GUILD_CHANNELS(guildID), data)
  347. if err != nil {
  348. return
  349. }
  350. err = json.Unmarshal(body, &st)
  351. return
  352. }
  353. // GuildInvites returns an array of Invite structures for the given guild
  354. // guildID : The ID of a Guild.
  355. func (s *Session) GuildInvites(guildID string) (st []*Invite, err error) {
  356. body, err := s.Request("GET", GUILD_INVITES(guildID), nil)
  357. if err != nil {
  358. return
  359. }
  360. err = json.Unmarshal(body, &st)
  361. return
  362. }
  363. // GuildInviteCreate creates a new invite for the given guild.
  364. // guildID : The ID of a Guild.
  365. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  366. // and XkcdPass defined.
  367. func (s *Session) GuildInviteCreate(guildID string, i *Invite) (st *Invite, err error) {
  368. data := struct {
  369. MaxAge int `json:"max_age"`
  370. MaxUses int `json:"max_uses"`
  371. Temporary bool `json:"temporary"`
  372. XKCDPass bool `json:"xkcdpass"`
  373. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  374. body, err := s.Request("POST", GUILD_INVITES(guildID), data)
  375. if err != nil {
  376. return
  377. }
  378. err = json.Unmarshal(body, &st)
  379. return
  380. }
  381. // GuildRoles returns all roles for a given guild.
  382. func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
  383. body, err := s.Request("GET", GUILD_ROLES(guildID), nil)
  384. if err != nil {
  385. return
  386. }
  387. err = json.Unmarshal(body, &st)
  388. return // TODO return pointer
  389. }
  390. // GuildRoleCreate returns a new Guild Role
  391. func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
  392. body, err := s.Request("POST", GUILD_ROLES(guildID), nil)
  393. if err != nil {
  394. return
  395. }
  396. err = json.Unmarshal(body, &st)
  397. return
  398. }
  399. // GuildRoleEdit updates an existing Guild Role with new values
  400. func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int) (st *Role, err error) {
  401. data := struct {
  402. Name string `json:"name"` // The color the role should have (as a decimal, not hex)
  403. Color int `json:"color"` // Whether to display the role's users separately
  404. Hoist bool `json:"hoist"` // The role's name (overwrites existing)
  405. Permissions int `json:"permissions"` // The overall permissions number of the role (overwrites existing)
  406. }{name, color, hoist, perm}
  407. body, err := s.Request("PATCH", GUILD_ROLE(guildID, roleID), data)
  408. if err != nil {
  409. return
  410. }
  411. err = json.Unmarshal(body, &st)
  412. return
  413. }
  414. // GuildRoleReorder reoders guild roles
  415. func (s *Session) GuildRoleReorder(guildID string, roles []Role) (st []*Role, err error) {
  416. body, err := s.Request("PATCH", GUILD_ROLES(guildID), roles)
  417. if err != nil {
  418. return
  419. }
  420. err = json.Unmarshal(body, &st)
  421. return
  422. }
  423. // GuildRoleDelete deletes an existing role.
  424. func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) {
  425. _, err = s.Request("DELETE", GUILD_ROLE(guildID, roleID), nil)
  426. return
  427. }
  428. // ------------------------------------------------------------------------------------------------
  429. // Functions specific to Discord Channels
  430. // ------------------------------------------------------------------------------------------------
  431. // Channel returns a Channel strucutre of a specific Channel.
  432. // channelID : The ID of the Channel you want returend.
  433. func (s *Session) Channel(channelID string) (st *Channel, err error) {
  434. body, err := s.Request("GET", CHANNEL(channelID), nil)
  435. if err != nil {
  436. return
  437. }
  438. err = json.Unmarshal(body, &st)
  439. return
  440. }
  441. // ChannelEdit edits the given channel
  442. // channelID : The ID of a Channel
  443. // name : The new name to assign the channel.
  444. func (s *Session) ChannelEdit(channelID, name string) (st *Channel, err error) {
  445. data := struct {
  446. Name string `json:"name"`
  447. }{name}
  448. body, err := s.Request("PATCH", CHANNEL(channelID), data)
  449. if err != nil {
  450. return
  451. }
  452. err = json.Unmarshal(body, &st)
  453. return
  454. }
  455. // ChannelDelete deletes the given channel
  456. // channelID : The ID of a Channel
  457. func (s *Session) ChannelDelete(channelID string) (st *Channel, err error) {
  458. body, err := s.Request("DELETE", CHANNEL(channelID), nil)
  459. if err != nil {
  460. return
  461. }
  462. err = json.Unmarshal(body, &st)
  463. return
  464. }
  465. // ChannelTyping broadcasts to all members that authenticated user is typing in
  466. // the given channel.
  467. // channelID : The ID of a Channel
  468. func (s *Session) ChannelTyping(channelID string) (err error) {
  469. _, err = s.Request("POST", CHANNEL_TYPING(channelID), nil)
  470. return
  471. }
  472. // ChannelMessages returns an array of Message structures for messages within
  473. // a given channel.
  474. // channelID : The ID of a Channel.
  475. // limit : The number messages that can be returned.
  476. // beforeID : If provided all messages returned will be before given ID.
  477. // afterID : If provided all messages returned will be after given ID.
  478. func (s *Session) ChannelMessages(channelID string, limit int, beforeID int, afterID int) (st []*Message, err error) {
  479. uri := CHANNEL_MESSAGES(channelID)
  480. v := url.Values{}
  481. if limit > 0 {
  482. v.Set("limit", strconv.Itoa(limit))
  483. }
  484. if afterID > 0 {
  485. v.Set("after", strconv.Itoa(afterID))
  486. }
  487. if beforeID > 0 {
  488. v.Set("before", strconv.Itoa(beforeID))
  489. }
  490. if len(v) > 0 {
  491. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  492. }
  493. body, err := s.Request("GET", uri, nil)
  494. if err != nil {
  495. return
  496. }
  497. err = json.Unmarshal(body, &st)
  498. return
  499. }
  500. // ChannelMessageAck acknowledges and marks the given message as read
  501. // channeld : The ID of a Channel
  502. // messageID : the ID of a Message
  503. func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
  504. _, err = s.Request("POST", CHANNEL_MESSAGE_ACK(channelID, messageID), nil)
  505. return
  506. }
  507. // ChannelMessageSend sends a message to the given channel.
  508. // channelID : The ID of a Channel.
  509. // content : The message to send.
  510. // NOTE, mention and tts parameters may be added in 2.x branch.
  511. func (s *Session) ChannelMessageSend(channelID string, content string) (st *Message, err error) {
  512. // TODO: nonce string ?
  513. data := struct {
  514. Content string `json:"content"`
  515. TTS bool `json:"tts"`
  516. }{content, false}
  517. // Send the message to the given channel
  518. response, err := s.Request("POST", CHANNEL_MESSAGES(channelID), data)
  519. if err != nil {
  520. return
  521. }
  522. err = json.Unmarshal(response, &st)
  523. return
  524. }
  525. // ChannelMessageEdit edits an existing message, replacing it entirely with
  526. // the given content.
  527. // channeld : The ID of a Channel
  528. // messageID : the ID of a Message
  529. func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (st *Message, err error) {
  530. data := struct {
  531. Content string `json:"content"`
  532. }{content}
  533. response, err := s.Request("PATCH", CHANNEL_MESSAGE(channelID, messageID), data)
  534. if err != nil {
  535. return
  536. }
  537. err = json.Unmarshal(response, &st)
  538. return
  539. }
  540. // ChannelMessageDelete deletes a message from the Channel.
  541. func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) {
  542. _, err = s.Request("DELETE", CHANNEL_MESSAGE(channelID, messageID), nil)
  543. return
  544. }
  545. // ChannelInvites returns an array of Invite structures for the given channel
  546. // channelID : The ID of a Channel
  547. func (s *Session) ChannelInvites(channelID string) (st []*Invite, err error) {
  548. body, err := s.Request("GET", CHANNEL_INVITES(channelID), nil)
  549. if err != nil {
  550. return
  551. }
  552. err = json.Unmarshal(body, &st)
  553. return
  554. }
  555. // ChannelInviteCreate creates a new invite for the given channel.
  556. // channelID : The ID of a Channel
  557. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  558. // and XkcdPass defined.
  559. func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st *Invite, err error) {
  560. data := struct {
  561. MaxAge int `json:"max_age"`
  562. MaxUses int `json:"max_uses"`
  563. Temporary bool `json:"temporary"`
  564. XKCDPass bool `json:"xkcdpass"`
  565. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  566. body, err := s.Request("POST", CHANNEL_INVITES(channelID), data)
  567. if err != nil {
  568. return
  569. }
  570. err = json.Unmarshal(body, &st)
  571. return
  572. }
  573. // ChannelPermissionSet creates a Permission Override for the given channel.
  574. // NOTE: This func name may changed. Using Set instead of Create because
  575. // you can both create a new override or update an override with this function.
  576. func (s *Session) ChannelPermissionSet(channelID, targetID, targetType string, allow, deny int) (err error) {
  577. data := struct {
  578. ID string `json:"id"`
  579. Type string `json:"type"`
  580. Allow int `json:"allow"`
  581. Deny int `json:"deny"`
  582. }{targetID, targetType, allow, deny}
  583. _, err = s.Request("PUT", CHANNEL_PERMISSION(channelID, targetID), data)
  584. return
  585. }
  586. // ChannelPermissionDelete deletes a specific permission override for the given channel.
  587. // NOTE: Name of this func may change.
  588. func (s *Session) ChannelPermissionDelete(channelID, targetID string) (err error) {
  589. _, err = s.Request("DELETE", CHANNEL_PERMISSION(channelID, targetID), nil)
  590. return
  591. }
  592. // ------------------------------------------------------------------------------------------------
  593. // Functions specific to Discord Invites
  594. // ------------------------------------------------------------------------------------------------
  595. // Invite returns an Invite structure of the given invite
  596. // inviteID : The invite code (or maybe xkcdpass?)
  597. func (s *Session) Invite(inviteID string) (st *Invite, err error) {
  598. body, err := s.Request("GET", INVITE(inviteID), nil)
  599. if err != nil {
  600. return
  601. }
  602. err = json.Unmarshal(body, &st)
  603. return
  604. }
  605. // InviteDelete deletes an existing invite
  606. // inviteID : the code (or maybe xkcdpass?) of an invite
  607. func (s *Session) InviteDelete(inviteID string) (st *Invite, err error) {
  608. body, err := s.Request("DELETE", INVITE(inviteID), nil)
  609. if err != nil {
  610. return
  611. }
  612. err = json.Unmarshal(body, &st)
  613. return
  614. }
  615. // InviteAccept accepts an Invite to a Guild or Channel
  616. // inviteID : The invite code (or maybe xkcdpass?)
  617. func (s *Session) InviteAccept(inviteID string) (st *Invite, err error) {
  618. body, err := s.Request("POST", INVITE(inviteID), nil)
  619. if err != nil {
  620. return
  621. }
  622. err = json.Unmarshal(body, &st)
  623. return
  624. }
  625. // ------------------------------------------------------------------------------------------------
  626. // Functions specific to Discord Voice
  627. // ------------------------------------------------------------------------------------------------
  628. // VoiceRegions returns the voice server regions
  629. func (s *Session) VoiceRegions() (st []*VoiceRegion, err error) {
  630. body, err := s.Request("GET", VOICE_REGIONS, nil)
  631. if err != nil {
  632. return
  633. }
  634. err = json.Unmarshal(body, &st)
  635. return
  636. }
  637. // VoiceICE returns the voice server ICE information
  638. func (s *Session) VoiceICE() (st *VoiceICE, err error) {
  639. body, err := s.Request("GET", VOICE_ICE, nil)
  640. if err != nil {
  641. return
  642. }
  643. err = json.Unmarshal(body, &st)
  644. return
  645. }
  646. // ------------------------------------------------------------------------------------------------
  647. // Functions specific to Discord Websockets
  648. // ------------------------------------------------------------------------------------------------
  649. // Gateway returns the a websocket Gateway address
  650. func (s *Session) Gateway() (gateway string, err error) {
  651. response, err := s.Request("GET", GATEWAY, nil)
  652. if err != nil {
  653. return
  654. }
  655. temp := struct {
  656. URL string `json:"url"`
  657. }{}
  658. err = json.Unmarshal(response, &temp)
  659. if err != nil {
  660. return
  661. }
  662. gateway = temp.URL
  663. return
  664. }