restapi.go 25 KB

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