restapi.go 24 KB

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