restapi.go 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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 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. // GuildLeave leaves a Guild.
  320. // guildID : The ID of a Guild
  321. func (s *Session) GuildLeave(guildID string) (err error) {
  322. _, err = s.Request("DELETE", USER_GUILD("@me", guildID), nil)
  323. return
  324. }
  325. // GuildBans returns an array of User structures for all bans of a
  326. // given guild.
  327. // guildID : The ID of a Guild.
  328. func (s *Session) GuildBans(guildID string) (st []*User, err error) {
  329. body, err := s.Request("GET", GUILD_BANS(guildID), nil)
  330. if err != nil {
  331. return
  332. }
  333. err = unmarshal(body, &st)
  334. return
  335. }
  336. // GuildBanCreate bans the given user from the given guild.
  337. // guildID : The ID of a Guild.
  338. // userID : The ID of a User
  339. // days : The number of days of previous comments to delete.
  340. func (s *Session) GuildBanCreate(guildID, userID string, days int) (err error) {
  341. uri := GUILD_BAN(guildID, userID)
  342. if days > 0 {
  343. uri = fmt.Sprintf("%s?delete-message-days=%d", uri, days)
  344. }
  345. _, err = s.Request("PUT", uri, nil)
  346. return
  347. }
  348. // GuildBanDelete removes the given user from the guild bans
  349. // guildID : The ID of a Guild.
  350. // userID : The ID of a User
  351. func (s *Session) GuildBanDelete(guildID, userID string) (err error) {
  352. _, err = s.Request("DELETE", GUILD_BAN(guildID, userID), nil)
  353. return
  354. }
  355. // GuildMembers returns a list of members for a guild.
  356. // guildID : The ID of a Guild.
  357. // offset : A number of members to skip
  358. // limit : max number of members to return
  359. func (s *Session) GuildMembers(guildID string, offset, limit int) (st []*Member, err error) {
  360. uri := GUILD_MEMBERS(guildID)
  361. v := url.Values{}
  362. if offset > 0 {
  363. v.Set("offset", strconv.Itoa(offset))
  364. }
  365. if limit > 0 {
  366. v.Set("limit", strconv.Itoa(limit))
  367. }
  368. if len(v) > 0 {
  369. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  370. }
  371. body, err := s.Request("GET", uri, nil)
  372. if err != nil {
  373. return
  374. }
  375. err = unmarshal(body, &st)
  376. return
  377. }
  378. // GuildMember returns a member of a guild.
  379. // guildID : The ID of a Guild.
  380. // userID : The ID of a User
  381. func (s *Session) GuildMember(guildID, userID string) (st *Member, err error) {
  382. body, err := s.Request("GET", GUILD_MEMBER(guildID, userID), nil)
  383. if err != nil {
  384. return
  385. }
  386. err = unmarshal(body, &st)
  387. return
  388. }
  389. // GuildMemberDelete removes the given user from the given guild.
  390. // guildID : The ID of a Guild.
  391. // userID : The ID of a User
  392. func (s *Session) GuildMemberDelete(guildID, userID string) (err error) {
  393. _, err = s.Request("DELETE", GUILD_MEMBER(guildID, userID), nil)
  394. return
  395. }
  396. // GuildMemberEdit edits the roles of a member.
  397. // guildID : The ID of a Guild.
  398. // userID : The ID of a User.
  399. // roles : A list of role ID's to set on the member.
  400. func (s *Session) GuildMemberEdit(guildID, userID string, roles []string) (err error) {
  401. data := struct {
  402. Roles []string `json:"roles"`
  403. }{roles}
  404. _, err = s.Request("PATCH", GUILD_MEMBER(guildID, userID), data)
  405. if err != nil {
  406. return
  407. }
  408. return
  409. }
  410. // GuildMemberMove moves a guild member from one voice channel to another/none
  411. // guildID : The ID of a Guild.
  412. // userID : The ID of a User.
  413. // channelID : The ID of a channel to move user to, or null?
  414. // NOTE : I am not entirely set on the name of this function and it may change
  415. // prior to the final 1.0.0 release of Discordgo
  416. func (s *Session) GuildMemberMove(guildID, userID, channelID string) (err error) {
  417. data := struct {
  418. ChannelID string `json:"channel_id"`
  419. }{channelID}
  420. _, err = s.Request("PATCH", GUILD_MEMBER(guildID, userID), data)
  421. if err != nil {
  422. return
  423. }
  424. return
  425. }
  426. // GuildChannels returns an array of Channel structures for all channels of a
  427. // given guild.
  428. // guildID : The ID of a Guild.
  429. func (s *Session) GuildChannels(guildID string) (st []*Channel, err error) {
  430. body, err := s.Request("GET", GUILD_CHANNELS(guildID), nil)
  431. if err != nil {
  432. return
  433. }
  434. err = unmarshal(body, &st)
  435. return
  436. }
  437. // GuildChannelCreate creates a new channel in the given guild
  438. // guildID : The ID of a Guild.
  439. // name : Name of the channel (2-100 chars length)
  440. // ctype : Tpye of the channel (voice or text)
  441. func (s *Session) GuildChannelCreate(guildID, name, ctype string) (st *Channel, err error) {
  442. data := struct {
  443. Name string `json:"name"`
  444. Type string `json:"type"`
  445. }{name, ctype}
  446. body, err := s.Request("POST", GUILD_CHANNELS(guildID), data)
  447. if err != nil {
  448. return
  449. }
  450. err = unmarshal(body, &st)
  451. return
  452. }
  453. // GuildInvites returns an array of Invite structures for the given guild
  454. // guildID : The ID of a Guild.
  455. func (s *Session) GuildInvites(guildID string) (st []*Invite, err error) {
  456. body, err := s.Request("GET", GUILD_INVITES(guildID), nil)
  457. if err != nil {
  458. return
  459. }
  460. err = unmarshal(body, &st)
  461. return
  462. }
  463. // GuildInviteCreate creates a new invite for the given guild.
  464. // guildID : The ID of a Guild.
  465. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  466. // and XkcdPass defined.
  467. func (s *Session) GuildInviteCreate(guildID string, i *Invite) (st *Invite, err error) {
  468. data := struct {
  469. MaxAge int `json:"max_age"`
  470. MaxUses int `json:"max_uses"`
  471. Temporary bool `json:"temporary"`
  472. XKCDPass bool `json:"xkcdpass"`
  473. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  474. body, err := s.Request("POST", GUILD_INVITES(guildID), data)
  475. if err != nil {
  476. return
  477. }
  478. err = unmarshal(body, &st)
  479. return
  480. }
  481. // GuildRoles returns all roles for a given guild.
  482. // guildID : The ID of a Guild.
  483. func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
  484. body, err := s.Request("GET", GUILD_ROLES(guildID), nil)
  485. if err != nil {
  486. return
  487. }
  488. err = unmarshal(body, &st)
  489. return // TODO return pointer
  490. }
  491. // GuildRoleCreate returns a new Guild Role.
  492. // guildID: The ID of a Guild.
  493. func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
  494. body, err := s.Request("POST", GUILD_ROLES(guildID), nil)
  495. if err != nil {
  496. return
  497. }
  498. err = unmarshal(body, &st)
  499. return
  500. }
  501. // GuildRoleEdit updates an existing Guild Role with new values
  502. // guildID : The ID of a Guild.
  503. // roleID : The ID of a Role.
  504. // name : The name of the Role.
  505. // color : The color of the role (decimal, not hex).
  506. // hoist : Whether to display the role's users separately.
  507. // perm : The permissions for the role.
  508. func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int) (st *Role, err error) {
  509. data := struct {
  510. Name string `json:"name"` // The color the role should have (as a decimal, not hex)
  511. Color int `json:"color"` // Whether to display the role's users separately
  512. Hoist bool `json:"hoist"` // The role's name (overwrites existing)
  513. Permissions int `json:"permissions"` // The overall permissions number of the role (overwrites existing)
  514. }{name, color, hoist, perm}
  515. body, err := s.Request("PATCH", GUILD_ROLE(guildID, roleID), data)
  516. if err != nil {
  517. return
  518. }
  519. err = unmarshal(body, &st)
  520. return
  521. }
  522. // GuildRoleReorder reoders guild roles
  523. // guildID : The ID of a Guild.
  524. // roles : A list of ordered roles.
  525. func (s *Session) GuildRoleReorder(guildID string, roles []*Role) (st []*Role, err error) {
  526. body, err := s.Request("PATCH", GUILD_ROLES(guildID), roles)
  527. if err != nil {
  528. return
  529. }
  530. err = unmarshal(body, &st)
  531. return
  532. }
  533. // GuildRoleDelete deletes an existing role.
  534. // guildID : The ID of a Guild.
  535. // roleID : The ID of a Role.
  536. func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) {
  537. _, err = s.Request("DELETE", GUILD_ROLE(guildID, roleID), nil)
  538. return
  539. }
  540. // GuildIcon returns an image.Image of a guild icon.
  541. // guildID : The ID of a Guild.
  542. func (s *Session) GuildIcon(guildID string) (img image.Image, err error) {
  543. g, err := s.Guild(guildID)
  544. if err != nil {
  545. return
  546. }
  547. if g.Icon == "" {
  548. err = errors.New("Guild does not have an icon set.")
  549. return
  550. }
  551. body, err := s.Request("GET", GUILD_ICON(guildID, g.Icon), nil)
  552. if err != nil {
  553. return
  554. }
  555. img, _, err = image.Decode(bytes.NewReader(body))
  556. return
  557. }
  558. // GuildSplash returns an image.Image of a guild splash image.
  559. // guildID : The ID of a Guild.
  560. func (s *Session) GuildSplash(guildID string) (img image.Image, err error) {
  561. g, err := s.Guild(guildID)
  562. if err != nil {
  563. return
  564. }
  565. if g.Splash == "" {
  566. err = errors.New("Guild does not have a splash set.")
  567. return
  568. }
  569. body, err := s.Request("GET", GUILD_SPLASH(guildID, g.Splash), nil)
  570. if err != nil {
  571. return
  572. }
  573. img, _, err = image.Decode(bytes.NewReader(body))
  574. return
  575. }
  576. // ------------------------------------------------------------------------------------------------
  577. // Functions specific to Discord Channels
  578. // ------------------------------------------------------------------------------------------------
  579. // Channel returns a Channel strucutre of a specific Channel.
  580. // channelID : The ID of the Channel you want returned.
  581. func (s *Session) Channel(channelID string) (st *Channel, err error) {
  582. body, err := s.Request("GET", CHANNEL(channelID), nil)
  583. if err != nil {
  584. return
  585. }
  586. err = unmarshal(body, &st)
  587. return
  588. }
  589. // ChannelEdit edits the given channel
  590. // channelID : The ID of a Channel
  591. // name : The new name to assign the channel.
  592. func (s *Session) ChannelEdit(channelID, name string) (st *Channel, err error) {
  593. data := struct {
  594. Name string `json:"name"`
  595. }{name}
  596. body, err := s.Request("PATCH", CHANNEL(channelID), data)
  597. if err != nil {
  598. return
  599. }
  600. err = unmarshal(body, &st)
  601. return
  602. }
  603. // ChannelDelete deletes the given channel
  604. // channelID : The ID of a Channel
  605. func (s *Session) ChannelDelete(channelID string) (st *Channel, err error) {
  606. body, err := s.Request("DELETE", CHANNEL(channelID), nil)
  607. if err != nil {
  608. return
  609. }
  610. err = unmarshal(body, &st)
  611. return
  612. }
  613. // ChannelTyping broadcasts to all members that authenticated user is typing in
  614. // the given channel.
  615. // channelID : The ID of a Channel
  616. func (s *Session) ChannelTyping(channelID string) (err error) {
  617. _, err = s.Request("POST", CHANNEL_TYPING(channelID), nil)
  618. return
  619. }
  620. // ChannelMessages returns an array of Message structures for messages within
  621. // a given channel.
  622. // channelID : The ID of a Channel.
  623. // limit : The number messages that can be returned.
  624. // beforeID : If provided all messages returned will be before given ID.
  625. // afterID : If provided all messages returned will be after given ID.
  626. func (s *Session) ChannelMessages(channelID string, limit int, beforeID int, afterID int) (st []*Message, err error) {
  627. uri := CHANNEL_MESSAGES(channelID)
  628. v := url.Values{}
  629. if limit > 0 {
  630. v.Set("limit", strconv.Itoa(limit))
  631. }
  632. if afterID > 0 {
  633. v.Set("after", strconv.Itoa(afterID))
  634. }
  635. if beforeID > 0 {
  636. v.Set("before", strconv.Itoa(beforeID))
  637. }
  638. if len(v) > 0 {
  639. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  640. }
  641. body, err := s.Request("GET", uri, nil)
  642. if err != nil {
  643. return
  644. }
  645. err = unmarshal(body, &st)
  646. return
  647. }
  648. // ChannelMessageAck acknowledges and marks the given message as read
  649. // channeld : The ID of a Channel
  650. // messageID : the ID of a Message
  651. func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
  652. _, err = s.Request("POST", CHANNEL_MESSAGE_ACK(channelID, messageID), nil)
  653. return
  654. }
  655. // channelMessageSend sends a message to the given channel.
  656. // channelID : The ID of a Channel.
  657. // content : The message to send.
  658. // tts : Whether to send the message with TTS.
  659. func (s *Session) channelMessageSend(channelID, content string, tts bool) (st *Message, err error) {
  660. // TODO: nonce string ?
  661. data := struct {
  662. Content string `json:"content"`
  663. TTS bool `json:"tts"`
  664. }{content, tts}
  665. // Send the message to the given channel
  666. response, err := s.Request("POST", CHANNEL_MESSAGES(channelID), data)
  667. if err != nil {
  668. return
  669. }
  670. err = unmarshal(response, &st)
  671. return
  672. }
  673. // ChannelMessageSend sends a message to the given channel.
  674. // channelID : The ID of a Channel.
  675. // content : The message to send.
  676. func (s *Session) ChannelMessageSend(channelID string, content string) (st *Message, err error) {
  677. return s.channelMessageSend(channelID, content, false)
  678. }
  679. // ChannelMessageSendTTS sends a message to the given channel with Text to Speech.
  680. // channelID : The ID of a Channel.
  681. // content : The message to send.
  682. func (s *Session) ChannelMessageSendTTS(channelID string, content string) (st *Message, err error) {
  683. return s.channelMessageSend(channelID, content, true)
  684. }
  685. // ChannelMessageEdit edits an existing message, replacing it entirely with
  686. // the given content.
  687. // channeld : The ID of a Channel
  688. // messageID : the ID of a Message
  689. func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (st *Message, err error) {
  690. data := struct {
  691. Content string `json:"content"`
  692. }{content}
  693. response, err := s.Request("PATCH", CHANNEL_MESSAGE(channelID, messageID), data)
  694. if err != nil {
  695. return
  696. }
  697. err = unmarshal(response, &st)
  698. return
  699. }
  700. // ChannelMessageDelete deletes a message from the Channel.
  701. func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) {
  702. _, err = s.Request("DELETE", CHANNEL_MESSAGE(channelID, messageID), nil)
  703. return
  704. }
  705. // ChannelFileSend sends a file to the given channel.
  706. // channelID : The ID of a Channel.
  707. // io.Reader : A reader for the file contents.
  708. func (s *Session) ChannelFileSend(channelID, name string, r io.Reader) (st *Message, err error) {
  709. body := &bytes.Buffer{}
  710. bodywriter := multipart.NewWriter(body)
  711. writer, err := bodywriter.CreateFormFile("file", name)
  712. if err != nil {
  713. return nil, err
  714. }
  715. _, err = io.Copy(writer, r)
  716. if err != nil {
  717. return
  718. }
  719. err = bodywriter.Close()
  720. if err != nil {
  721. return
  722. }
  723. response, err := s.request("POST", CHANNEL_MESSAGES(channelID), bodywriter.FormDataContentType(), body.Bytes())
  724. if err != nil {
  725. return
  726. }
  727. err = unmarshal(response, &st)
  728. return
  729. }
  730. // ChannelInvites returns an array of Invite structures for the given channel
  731. // channelID : The ID of a Channel
  732. func (s *Session) ChannelInvites(channelID string) (st []*Invite, err error) {
  733. body, err := s.Request("GET", CHANNEL_INVITES(channelID), nil)
  734. if err != nil {
  735. return
  736. }
  737. err = unmarshal(body, &st)
  738. return
  739. }
  740. // ChannelInviteCreate creates a new invite for the given channel.
  741. // channelID : The ID of a Channel
  742. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  743. // and XkcdPass defined.
  744. func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st *Invite, err error) {
  745. data := struct {
  746. MaxAge int `json:"max_age"`
  747. MaxUses int `json:"max_uses"`
  748. Temporary bool `json:"temporary"`
  749. XKCDPass bool `json:"xkcdpass"`
  750. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  751. body, err := s.Request("POST", CHANNEL_INVITES(channelID), data)
  752. if err != nil {
  753. return
  754. }
  755. err = unmarshal(body, &st)
  756. return
  757. }
  758. // ChannelPermissionSet creates a Permission Override for the given channel.
  759. // NOTE: This func name may changed. Using Set instead of Create because
  760. // you can both create a new override or update an override with this function.
  761. func (s *Session) ChannelPermissionSet(channelID, targetID, targetType string, allow, deny int) (err error) {
  762. data := struct {
  763. ID string `json:"id"`
  764. Type string `json:"type"`
  765. Allow int `json:"allow"`
  766. Deny int `json:"deny"`
  767. }{targetID, targetType, allow, deny}
  768. _, err = s.Request("PUT", CHANNEL_PERMISSION(channelID, targetID), data)
  769. return
  770. }
  771. // ChannelPermissionDelete deletes a specific permission override for the given channel.
  772. // NOTE: Name of this func may change.
  773. func (s *Session) ChannelPermissionDelete(channelID, targetID string) (err error) {
  774. _, err = s.Request("DELETE", CHANNEL_PERMISSION(channelID, targetID), nil)
  775. return
  776. }
  777. // ------------------------------------------------------------------------------------------------
  778. // Functions specific to Discord Invites
  779. // ------------------------------------------------------------------------------------------------
  780. // Invite returns an Invite structure of the given invite
  781. // inviteID : The invite code (or maybe xkcdpass?)
  782. func (s *Session) Invite(inviteID string) (st *Invite, err error) {
  783. body, err := s.Request("GET", INVITE(inviteID), nil)
  784. if err != nil {
  785. return
  786. }
  787. err = unmarshal(body, &st)
  788. return
  789. }
  790. // InviteDelete deletes an existing invite
  791. // inviteID : the code (or maybe xkcdpass?) of an invite
  792. func (s *Session) InviteDelete(inviteID string) (st *Invite, err error) {
  793. body, err := s.Request("DELETE", INVITE(inviteID), nil)
  794. if err != nil {
  795. return
  796. }
  797. err = unmarshal(body, &st)
  798. return
  799. }
  800. // InviteAccept accepts an Invite to a Guild or Channel
  801. // inviteID : The invite code (or maybe xkcdpass?)
  802. func (s *Session) InviteAccept(inviteID string) (st *Invite, err error) {
  803. body, err := s.Request("POST", INVITE(inviteID), nil)
  804. if err != nil {
  805. return
  806. }
  807. err = unmarshal(body, &st)
  808. return
  809. }
  810. // ------------------------------------------------------------------------------------------------
  811. // Functions specific to Discord Voice
  812. // ------------------------------------------------------------------------------------------------
  813. // VoiceRegions returns the voice server regions
  814. func (s *Session) VoiceRegions() (st []*VoiceRegion, err error) {
  815. body, err := s.Request("GET", VOICE_REGIONS, nil)
  816. if err != nil {
  817. return
  818. }
  819. err = unmarshal(body, &st)
  820. return
  821. }
  822. // VoiceICE returns the voice server ICE information
  823. func (s *Session) VoiceICE() (st *VoiceICE, err error) {
  824. body, err := s.Request("GET", VOICE_ICE, nil)
  825. if err != nil {
  826. return
  827. }
  828. err = unmarshal(body, &st)
  829. return
  830. }
  831. // ------------------------------------------------------------------------------------------------
  832. // Functions specific to Discord Websockets
  833. // ------------------------------------------------------------------------------------------------
  834. // Gateway returns the a websocket Gateway address
  835. func (s *Session) Gateway() (gateway string, err error) {
  836. response, err := s.Request("GET", GATEWAY, nil)
  837. if err != nil {
  838. return
  839. }
  840. temp := struct {
  841. URL string `json:"url"`
  842. }{}
  843. err = unmarshal(response, &temp)
  844. if err != nil {
  845. return
  846. }
  847. gateway = temp.URL
  848. return
  849. }