restapi.go 26 KB

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