restapi.go 24 KB

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