restapi.go 22 KB

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