restapi.go 24 KB

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