restapi.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. // Attempt to grab the guild from State first.
  255. st, err = s.State.Guild(guildID)
  256. if err == nil {
  257. return
  258. }
  259. body, err := s.Request("GET", GUILD(guildID), nil)
  260. if err != nil {
  261. return
  262. }
  263. err = unmarshal(body, &st)
  264. return
  265. }
  266. // GuildCreate creates a new Guild
  267. // name : A name for the Guild (2-100 characters)
  268. func (s *Session) GuildCreate(name string) (st *Guild, err error) {
  269. data := struct {
  270. Name string `json:"name"`
  271. }{name}
  272. body, err := s.Request("POST", GUILDS, data)
  273. if err != nil {
  274. return
  275. }
  276. err = unmarshal(body, &st)
  277. return
  278. }
  279. // GuildEdit edits a new Guild
  280. // guildID : The ID of a Guild
  281. // name : A name for the Guild (2-100 characters)
  282. func (s *Session) GuildEdit(guildID, name string) (st *Guild, err error) {
  283. data := struct {
  284. Name string `json:"name"`
  285. }{name}
  286. body, err := s.Request("POST", GUILD(guildID), data)
  287. if err != nil {
  288. return
  289. }
  290. err = unmarshal(body, &st)
  291. return
  292. }
  293. // GuildDelete deletes or leaves a Guild.
  294. // guildID : The ID of a Guild
  295. func (s *Session) GuildDelete(guildID string) (st *Guild, err error) {
  296. body, err := s.Request("DELETE", GUILD(guildID), nil)
  297. if err != nil {
  298. return
  299. }
  300. err = unmarshal(body, &st)
  301. return
  302. }
  303. // GuildBans returns an array of User structures for all bans of a
  304. // given guild.
  305. // guildID : The ID of a Guild.
  306. func (s *Session) GuildBans(guildID string) (st []*User, err error) {
  307. body, err := s.Request("GET", GUILD_BANS(guildID), nil)
  308. if err != nil {
  309. return
  310. }
  311. err = unmarshal(body, &st)
  312. return
  313. }
  314. // GuildBanCreate bans the given user from the given guild.
  315. // guildID : The ID of a Guild.
  316. // userID : The ID of a User
  317. // days : The number of days of previous comments to delete.
  318. func (s *Session) GuildBanCreate(guildID, userID string, days int) (err error) {
  319. uri := GUILD_BAN(guildID, userID)
  320. if days > 0 {
  321. uri = fmt.Sprintf("%s?delete-message-days=%d", uri, days)
  322. }
  323. _, err = s.Request("PUT", uri, nil)
  324. return
  325. }
  326. // GuildBanDelete removes the given user from the guild bans
  327. // guildID : The ID of a Guild.
  328. // userID : The ID of a User
  329. func (s *Session) GuildBanDelete(guildID, userID string) (err error) {
  330. _, err = s.Request("DELETE", GUILD_BAN(guildID, userID), nil)
  331. return
  332. }
  333. // GuildMemberDelete removes the given user from the given guild.
  334. // guildID : The ID of a Guild.
  335. // userID : The ID of a User
  336. func (s *Session) GuildMemberDelete(guildID, userID string) (err error) {
  337. _, err = s.Request("DELETE", GUILD_MEMBER_DEL(guildID, userID), nil)
  338. return
  339. }
  340. // GuildChannels returns an array of Channel structures for all channels of a
  341. // given guild.
  342. // guildID : The ID of a Guild.
  343. func (s *Session) GuildChannels(guildID string) (st []*Channel, err error) {
  344. body, err := s.Request("GET", GUILD_CHANNELS(guildID), nil)
  345. if err != nil {
  346. return
  347. }
  348. err = unmarshal(body, &st)
  349. return
  350. }
  351. // GuildChannelCreate creates a new channel in the given guild
  352. // guildID : The ID of a Guild.
  353. // name : Name of the channel (2-100 chars length)
  354. // ctype : Tpye of the channel (voice or text)
  355. func (s *Session) GuildChannelCreate(guildID, name, ctype string) (st *Channel, err error) {
  356. data := struct {
  357. Name string `json:"name"`
  358. Type string `json:"type"`
  359. }{name, ctype}
  360. body, err := s.Request("POST", GUILD_CHANNELS(guildID), data)
  361. if err != nil {
  362. return
  363. }
  364. err = unmarshal(body, &st)
  365. return
  366. }
  367. // GuildInvites returns an array of Invite structures for the given guild
  368. // guildID : The ID of a Guild.
  369. func (s *Session) GuildInvites(guildID string) (st []*Invite, err error) {
  370. body, err := s.Request("GET", GUILD_INVITES(guildID), nil)
  371. if err != nil {
  372. return
  373. }
  374. err = unmarshal(body, &st)
  375. return
  376. }
  377. // GuildInviteCreate creates a new invite for the given guild.
  378. // guildID : The ID of a Guild.
  379. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  380. // and XkcdPass defined.
  381. func (s *Session) GuildInviteCreate(guildID string, i *Invite) (st *Invite, err error) {
  382. data := struct {
  383. MaxAge int `json:"max_age"`
  384. MaxUses int `json:"max_uses"`
  385. Temporary bool `json:"temporary"`
  386. XKCDPass bool `json:"xkcdpass"`
  387. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  388. body, err := s.Request("POST", GUILD_INVITES(guildID), data)
  389. if err != nil {
  390. return
  391. }
  392. err = unmarshal(body, &st)
  393. return
  394. }
  395. // GuildRoles returns all roles for a given guild.
  396. // guildID : The ID of a Guild.
  397. func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
  398. body, err := s.Request("GET", GUILD_ROLES(guildID), nil)
  399. if err != nil {
  400. return
  401. }
  402. err = unmarshal(body, &st)
  403. return // TODO return pointer
  404. }
  405. // GuildRoleCreate returns a new Guild Role.
  406. // guildID: The ID of a Guild.
  407. func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
  408. body, err := s.Request("POST", GUILD_ROLES(guildID), nil)
  409. if err != nil {
  410. return
  411. }
  412. err = unmarshal(body, &st)
  413. return
  414. }
  415. // GuildRoleEdit updates an existing Guild Role with new values
  416. // guildID : The ID of a Guild.
  417. // roleID : The ID of a Role.
  418. // name : The name of the Role.
  419. // color : The color of the role (decimal, not hex).
  420. // hoist : Whether to display the role's users separately.
  421. // perm : The permissions for the role.
  422. func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int) (st *Role, err error) {
  423. data := struct {
  424. Name string `json:"name"` // The color the role should have (as a decimal, not hex)
  425. Color int `json:"color"` // Whether to display the role's users separately
  426. Hoist bool `json:"hoist"` // The role's name (overwrites existing)
  427. Permissions int `json:"permissions"` // The overall permissions number of the role (overwrites existing)
  428. }{name, color, hoist, perm}
  429. body, err := s.Request("PATCH", GUILD_ROLE(guildID, roleID), data)
  430. if err != nil {
  431. return
  432. }
  433. err = unmarshal(body, &st)
  434. return
  435. }
  436. // GuildRoleReorder reoders guild roles
  437. // guildID : The ID of a Guild.
  438. // roles : A list of ordered roles.
  439. func (s *Session) GuildRoleReorder(guildID string, roles []*Role) (st []*Role, err error) {
  440. body, err := s.Request("PATCH", GUILD_ROLES(guildID), roles)
  441. if err != nil {
  442. return
  443. }
  444. err = unmarshal(body, &st)
  445. return
  446. }
  447. // GuildRoleDelete deletes an existing role.
  448. // guildID : The ID of a Guild.
  449. // roleID : The ID of a Role.
  450. func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) {
  451. _, err = s.Request("DELETE", GUILD_ROLE(guildID, roleID), nil)
  452. return
  453. }
  454. // GuildIcon returns an image.Image of a guild icon.
  455. // guildID : The ID of a Guild.
  456. func (s *Session) GuildIcon(guildID string) (img image.Image, err error) {
  457. g, err := s.Guild(guildID)
  458. if err != nil {
  459. return
  460. }
  461. if g.Icon == "" {
  462. err = errors.New("Guild does not have an icon set.")
  463. return
  464. }
  465. body, err := s.Request("GET", GUILD_ICON(guildID, g.Icon), nil)
  466. if err != nil {
  467. return
  468. }
  469. img, _, err = image.Decode(bytes.NewReader(body))
  470. return
  471. }
  472. // GuildSplash returns an image.Image of a guild splash image.
  473. // guildID : The ID of a Guild.
  474. func (s *Session) GuildSplash(guildID string) (img image.Image, err error) {
  475. g, err := s.Guild(guildID)
  476. if err != nil {
  477. return
  478. }
  479. if g.Splash == "" {
  480. err = errors.New("Guild does not have a splash set.")
  481. return
  482. }
  483. body, err := s.Request("GET", GUILD_SPLASH(guildID, g.Splash), nil)
  484. if err != nil {
  485. return
  486. }
  487. img, _, err = image.Decode(bytes.NewReader(body))
  488. return
  489. }
  490. // ------------------------------------------------------------------------------------------------
  491. // Functions specific to Discord Channels
  492. // ------------------------------------------------------------------------------------------------
  493. // Channel returns a Channel strucutre of a specific Channel.
  494. // channelID : The ID of the Channel you want returend.
  495. func (s *Session) Channel(channelID string) (st *Channel, err error) {
  496. body, err := s.Request("GET", CHANNEL(channelID), nil)
  497. if err != nil {
  498. return
  499. }
  500. err = unmarshal(body, &st)
  501. return
  502. }
  503. // ChannelEdit edits the given channel
  504. // channelID : The ID of a Channel
  505. // name : The new name to assign the channel.
  506. func (s *Session) ChannelEdit(channelID, name string) (st *Channel, err error) {
  507. data := struct {
  508. Name string `json:"name"`
  509. }{name}
  510. body, err := s.Request("PATCH", CHANNEL(channelID), data)
  511. if err != nil {
  512. return
  513. }
  514. err = unmarshal(body, &st)
  515. return
  516. }
  517. // ChannelDelete deletes the given channel
  518. // channelID : The ID of a Channel
  519. func (s *Session) ChannelDelete(channelID string) (st *Channel, err error) {
  520. body, err := s.Request("DELETE", CHANNEL(channelID), nil)
  521. if err != nil {
  522. return
  523. }
  524. err = unmarshal(body, &st)
  525. return
  526. }
  527. // ChannelTyping broadcasts to all members that authenticated user is typing in
  528. // the given channel.
  529. // channelID : The ID of a Channel
  530. func (s *Session) ChannelTyping(channelID string) (err error) {
  531. _, err = s.Request("POST", CHANNEL_TYPING(channelID), nil)
  532. return
  533. }
  534. // ChannelMessages returns an array of Message structures for messages within
  535. // a given channel.
  536. // channelID : The ID of a Channel.
  537. // limit : The number messages that can be returned.
  538. // beforeID : If provided all messages returned will be before given ID.
  539. // afterID : If provided all messages returned will be after given ID.
  540. func (s *Session) ChannelMessages(channelID string, limit int, beforeID int, afterID int) (st []*Message, err error) {
  541. uri := CHANNEL_MESSAGES(channelID)
  542. v := url.Values{}
  543. if limit > 0 {
  544. v.Set("limit", strconv.Itoa(limit))
  545. }
  546. if afterID > 0 {
  547. v.Set("after", strconv.Itoa(afterID))
  548. }
  549. if beforeID > 0 {
  550. v.Set("before", strconv.Itoa(beforeID))
  551. }
  552. if len(v) > 0 {
  553. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  554. }
  555. body, err := s.Request("GET", uri, nil)
  556. if err != nil {
  557. return
  558. }
  559. err = unmarshal(body, &st)
  560. return
  561. }
  562. // ChannelMessageAck acknowledges and marks the given message as read
  563. // channeld : The ID of a Channel
  564. // messageID : the ID of a Message
  565. func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
  566. _, err = s.Request("POST", CHANNEL_MESSAGE_ACK(channelID, messageID), nil)
  567. return
  568. }
  569. // ChannelMessageSend sends a message to the given channel.
  570. // channelID : The ID of a Channel.
  571. // content : The message to send.
  572. // NOTE, mention and tts parameters may be added in 2.x branch.
  573. func (s *Session) ChannelMessageSend(channelID string, content string) (st *Message, err error) {
  574. // TODO: nonce string ?
  575. data := struct {
  576. Content string `json:"content"`
  577. TTS bool `json:"tts"`
  578. }{content, false}
  579. // Send the message to the given channel
  580. response, err := s.Request("POST", CHANNEL_MESSAGES(channelID), data)
  581. if err != nil {
  582. return
  583. }
  584. err = unmarshal(response, &st)
  585. return
  586. }
  587. // ChannelMessageEdit edits an existing message, replacing it entirely with
  588. // the given content.
  589. // channeld : The ID of a Channel
  590. // messageID : the ID of a Message
  591. func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (st *Message, err error) {
  592. data := struct {
  593. Content string `json:"content"`
  594. }{content}
  595. response, err := s.Request("PATCH", CHANNEL_MESSAGE(channelID, messageID), data)
  596. if err != nil {
  597. return
  598. }
  599. err = unmarshal(response, &st)
  600. return
  601. }
  602. // ChannelMessageDelete deletes a message from the Channel.
  603. func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) {
  604. _, err = s.Request("DELETE", CHANNEL_MESSAGE(channelID, messageID), nil)
  605. return
  606. }
  607. // ChannelInvites returns an array of Invite structures for the given channel
  608. // channelID : The ID of a Channel
  609. func (s *Session) ChannelInvites(channelID string) (st []*Invite, err error) {
  610. body, err := s.Request("GET", CHANNEL_INVITES(channelID), nil)
  611. if err != nil {
  612. return
  613. }
  614. err = unmarshal(body, &st)
  615. return
  616. }
  617. // ChannelInviteCreate creates a new invite for the given channel.
  618. // channelID : The ID of a Channel
  619. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  620. // and XkcdPass defined.
  621. func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st *Invite, err error) {
  622. data := struct {
  623. MaxAge int `json:"max_age"`
  624. MaxUses int `json:"max_uses"`
  625. Temporary bool `json:"temporary"`
  626. XKCDPass bool `json:"xkcdpass"`
  627. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  628. body, err := s.Request("POST", CHANNEL_INVITES(channelID), data)
  629. if err != nil {
  630. return
  631. }
  632. err = unmarshal(body, &st)
  633. return
  634. }
  635. // ChannelPermissionSet creates a Permission Override for the given channel.
  636. // NOTE: This func name may changed. Using Set instead of Create because
  637. // you can both create a new override or update an override with this function.
  638. func (s *Session) ChannelPermissionSet(channelID, targetID, targetType string, allow, deny int) (err error) {
  639. data := struct {
  640. ID string `json:"id"`
  641. Type string `json:"type"`
  642. Allow int `json:"allow"`
  643. Deny int `json:"deny"`
  644. }{targetID, targetType, allow, deny}
  645. _, err = s.Request("PUT", CHANNEL_PERMISSION(channelID, targetID), data)
  646. return
  647. }
  648. // ChannelPermissionDelete deletes a specific permission override for the given channel.
  649. // NOTE: Name of this func may change.
  650. func (s *Session) ChannelPermissionDelete(channelID, targetID string) (err error) {
  651. _, err = s.Request("DELETE", CHANNEL_PERMISSION(channelID, targetID), nil)
  652. return
  653. }
  654. // ------------------------------------------------------------------------------------------------
  655. // Functions specific to Discord Invites
  656. // ------------------------------------------------------------------------------------------------
  657. // Invite returns an Invite structure of the given invite
  658. // inviteID : The invite code (or maybe xkcdpass?)
  659. func (s *Session) Invite(inviteID string) (st *Invite, err error) {
  660. body, err := s.Request("GET", INVITE(inviteID), nil)
  661. if err != nil {
  662. return
  663. }
  664. err = unmarshal(body, &st)
  665. return
  666. }
  667. // InviteDelete deletes an existing invite
  668. // inviteID : the code (or maybe xkcdpass?) of an invite
  669. func (s *Session) InviteDelete(inviteID string) (st *Invite, err error) {
  670. body, err := s.Request("DELETE", INVITE(inviteID), nil)
  671. if err != nil {
  672. return
  673. }
  674. err = unmarshal(body, &st)
  675. return
  676. }
  677. // InviteAccept accepts an Invite to a Guild or Channel
  678. // inviteID : The invite code (or maybe xkcdpass?)
  679. func (s *Session) InviteAccept(inviteID string) (st *Invite, err error) {
  680. body, err := s.Request("POST", INVITE(inviteID), nil)
  681. if err != nil {
  682. return
  683. }
  684. err = unmarshal(body, &st)
  685. return
  686. }
  687. // ------------------------------------------------------------------------------------------------
  688. // Functions specific to Discord Voice
  689. // ------------------------------------------------------------------------------------------------
  690. // VoiceRegions returns the voice server regions
  691. func (s *Session) VoiceRegions() (st []*VoiceRegion, err error) {
  692. body, err := s.Request("GET", VOICE_REGIONS, nil)
  693. if err != nil {
  694. return
  695. }
  696. err = unmarshal(body, &st)
  697. return
  698. }
  699. // VoiceICE returns the voice server ICE information
  700. func (s *Session) VoiceICE() (st *VoiceICE, err error) {
  701. body, err := s.Request("GET", VOICE_ICE, nil)
  702. if err != nil {
  703. return
  704. }
  705. err = unmarshal(body, &st)
  706. return
  707. }
  708. // ------------------------------------------------------------------------------------------------
  709. // Functions specific to Discord Websockets
  710. // ------------------------------------------------------------------------------------------------
  711. // Gateway returns the a websocket Gateway address
  712. func (s *Session) Gateway() (gateway string, err error) {
  713. response, err := s.Request("GET", GATEWAY, nil)
  714. if err != nil {
  715. return
  716. }
  717. temp := struct {
  718. URL string `json:"url"`
  719. }{}
  720. err = unmarshal(response, &temp)
  721. if err != nil {
  722. return
  723. }
  724. gateway = temp.URL
  725. return
  726. }