restapi.go 24 KB

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