restapi.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. "fmt"
  13. "io/ioutil"
  14. "net/http"
  15. "net/url"
  16. "strconv"
  17. "time"
  18. )
  19. // Request makes a (GET/POST/...) Requests to Discord REST API.
  20. // All the other Discord REST Calls in this file use this function.
  21. func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) {
  22. if s.Debug {
  23. fmt.Printf("API REQUEST %8s :: %s\n", method, urlStr)
  24. fmt.Println("API REQUEST PAYLOAD :: [" + fmt.Sprintf("%+v", data) + "]")
  25. }
  26. var body []byte
  27. if data != nil {
  28. body, err = json.Marshal(data)
  29. if err != nil {
  30. return
  31. }
  32. }
  33. req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(body))
  34. if err != nil {
  35. return
  36. }
  37. // Not used on initial login..
  38. // TODO: Verify if a login, otherwise complain about no-token
  39. if s.Token != "" {
  40. req.Header.Set("authorization", s.Token)
  41. }
  42. req.Header.Set("Content-Type", "application/json")
  43. // TODO: Make a configurable static variable.
  44. req.Header.Set("User-Agent", fmt.Sprintf("DiscordBot (https://github.com/bwmarrin/discordgo, v%s)", VERSION))
  45. if s.Debug {
  46. for k, v := range req.Header {
  47. fmt.Printf("API REQUEST HEADER :: [%s] = %+v\n", k, v)
  48. }
  49. }
  50. client := &http.Client{Timeout: (20 * time.Second)}
  51. resp, err := client.Do(req)
  52. defer resp.Body.Close()
  53. if err != nil {
  54. return
  55. }
  56. response, err = ioutil.ReadAll(resp.Body)
  57. if err != nil {
  58. return
  59. }
  60. if s.Debug {
  61. fmt.Printf("API RESPONSE STATUS :: %s\n", resp.Status)
  62. for k, v := range resp.Header {
  63. fmt.Printf("API RESPONSE HEADER :: [%s] = %+v\n", k, v)
  64. }
  65. fmt.Printf("API RESPONSE BODY :: [%s]\n", response)
  66. }
  67. // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  68. switch resp.StatusCode {
  69. case 200: // OK
  70. case 204: // No Content
  71. // TODO check for 401 response, invalidate token if we get one.
  72. // TODO check for 429 response, rate-limit when we get one.
  73. default: // Error condition
  74. err = fmt.Errorf("HTTP %s, %s", resp.Status, response)
  75. }
  76. return
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Functions specific to Discord Sessions
  80. // ------------------------------------------------------------------------------------------------
  81. // Login asks the Discord server for an authentication token
  82. func (s *Session) Login(email string, password string) (token string, err error) {
  83. data := struct {
  84. Email string `json:"email"`
  85. Password string `json:"password"`
  86. }{email, password}
  87. response, err := s.Request("POST", LOGIN, data)
  88. if err != nil {
  89. return "", err
  90. }
  91. temp := struct {
  92. Token string `json:"token"`
  93. }{}
  94. err = json.Unmarshal(response, &temp)
  95. token = temp.Token
  96. return
  97. }
  98. // Register sends a Register request to Discord, and returns the authentication token
  99. // Note that this account is temporary and should be verified for future use.
  100. // Another option is to save the authentication token external, but this isn't recommended.
  101. func (s *Session) Register(username string) (token string, err error) {
  102. data := struct {
  103. Username string `json:"username"`
  104. }{username}
  105. response, err := s.Request("POST", REGISTER, data)
  106. temp := struct {
  107. Token string `json:"token"`
  108. }{}
  109. err = json.Unmarshal(response, &temp)
  110. token = temp.Token
  111. return
  112. }
  113. // Logout sends a logout request to Discord.
  114. // This does not seem to actually invalidate the token. So you can still
  115. // make API calls even after a Logout. So, it seems almost pointless to
  116. // even use.
  117. func (s *Session) Logout() (err error) {
  118. // _, err = s.Request("POST", LOGOUT, fmt.Sprintf(`{"token": "%s"}`, s.Token))
  119. return
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. // Functions specific to Discord Users
  123. // ------------------------------------------------------------------------------------------------
  124. // User returns the user details of the given userID
  125. // userID : A user ID or "@me" which is a shortcut of current user ID
  126. func (s *Session) User(userID string) (st User, err error) {
  127. body, err := s.Request("GET", USER(userID), nil)
  128. err = json.Unmarshal(body, &st)
  129. return
  130. }
  131. // UserUpdate updates a users settings.
  132. // userID : A user ID or "@me" which is a shortcut of current user ID
  133. func (s *Session) UserUpdate(userID, email, password, username, avatar, newPassword string) (st *User, err error) {
  134. // NOTE: Avatar must be either the hash/id of existing Avatar or
  135. // data:image/png;base64,BASE64_STRING_OF_NEW_AVATAR_PNG
  136. // to set a new avatar.
  137. // If left blank, avatar will be set to null/blank
  138. data := struct {
  139. Email string `json:"email"`
  140. Password string `json:"password"`
  141. Username string `json:"username"`
  142. Avatar string `json:"avatar,omitempty"`
  143. NewPassword string `json:"new_password,omitempty"`
  144. }{email, password, username, avatar, newPassword}
  145. body, err := s.Request("PATCH", USER(userID), data)
  146. err = json.Unmarshal(body, &st)
  147. return
  148. }
  149. // UserAvatar returns a ?? of a users Avatar
  150. // userID : A user ID or "@me" which is a shortcut of current user ID
  151. func (s *Session) UserAvatar(userID string) (st User, err error) {
  152. u, err := s.User(userID)
  153. _, err = s.Request("GET", USER_AVATAR(userID, u.Avatar), nil)
  154. // TODO need to figure out how to handle returning a file
  155. return
  156. }
  157. // UserSettings returns the settings for a given user
  158. // userID : A user ID or "@me" which is a shortcut of current user ID
  159. // This seems to only return a result for "@me"
  160. func (s *Session) UserSettings(userID string) (st *Settings, err error) {
  161. body, err := s.Request("GET", USER_SETTINGS(userID), nil)
  162. err = json.Unmarshal(body, &st)
  163. return
  164. }
  165. // UserChannels returns an array of Channel structures for all private
  166. // channels for a user
  167. // userID : A user ID or "@me" which is a shortcut of current user ID
  168. func (s *Session) UserChannels(userID string) (st []*Channel, err error) {
  169. body, err := s.Request("GET", USER_CHANNELS(userID), nil)
  170. err = json.Unmarshal(body, &st)
  171. return
  172. }
  173. // UserChannelCreate creates a new User (Private) Channel with another User
  174. // userID : A user ID or "@me" which is a shortcut of current user ID
  175. // recipientID : A user ID for the user to which this channel is opened with.
  176. func (s *Session) UserChannelCreate(userID, recipientID string) (st *Channel, err error) {
  177. data := struct {
  178. RecipientID string `json:"recipient_id"`
  179. }{recipientID}
  180. body, err := s.Request(
  181. "POST",
  182. USER_CHANNELS(userID),
  183. data)
  184. err = json.Unmarshal(body, &st)
  185. return
  186. }
  187. // UserGuilds returns an array of Guild structures for all guilds for a given user
  188. // userID : A user ID or "@me" which is a shortcut of current user ID
  189. func (s *Session) UserGuilds(userID string) (st []*Guild, err error) {
  190. body, err := s.Request("GET", USER_GUILDS(userID), nil)
  191. err = json.Unmarshal(body, &st)
  192. return
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. // Functions specific to Discord Guilds
  196. // ------------------------------------------------------------------------------------------------
  197. // Guild returns a Guild structure of a specific Guild.
  198. // guildID : The ID of a Guild
  199. func (s *Session) Guild(guildID string) (st *Guild, err error) {
  200. body, err := s.Request("GET", GUILD(guildID), nil)
  201. err = json.Unmarshal(body, &st)
  202. return
  203. }
  204. // GuildCreate creates a new Guild
  205. // name : A name for the Guild (2-100 characters)
  206. func (s *Session) GuildCreate(name string) (st *Guild, err error) {
  207. data := struct {
  208. Name string `json:"name"`
  209. }{name}
  210. body, err := s.Request("POST", GUILDS, data)
  211. err = json.Unmarshal(body, &st)
  212. return
  213. }
  214. // GuildEdit edits a new Guild
  215. // guildID : The ID of a Guild
  216. // name : A name for the Guild (2-100 characters)
  217. func (s *Session) GuildEdit(guildID, name string) (st *Guild, err error) {
  218. data := struct {
  219. Name string `json:"name"`
  220. }{name}
  221. body, err := s.Request("POST", GUILD(guildID), data)
  222. err = json.Unmarshal(body, &st)
  223. return
  224. }
  225. // GuildDelete deletes or leaves a Guild.
  226. // guildID : The ID of a Guild
  227. func (s *Session) GuildDelete(guildID string) (st *Guild, err error) {
  228. body, err := s.Request("DELETE", GUILD(guildID), nil)
  229. err = json.Unmarshal(body, &st)
  230. return
  231. }
  232. // GuildBans returns an array of User structures for all bans of a
  233. // given guild.
  234. // guildID : The ID of a Guild.
  235. func (s *Session) GuildBans(guildID string) (st []*User, err error) {
  236. body, err := s.Request("GET", GUILD_BANS(guildID), nil)
  237. err = json.Unmarshal(body, &st)
  238. return
  239. }
  240. // GuildBanCreate bans the given user from the given guild.
  241. // guildID : The ID of a Guild.
  242. // userID : The ID of a User
  243. // days : The number of days of previous comments to delete.
  244. func (s *Session) GuildBanCreate(guildID, userID string, days int) (err error) {
  245. uri := GUILD_BAN(guildID, userID)
  246. if days > 0 {
  247. uri = fmt.Sprintf("%s?delete-message-days=%d", uri, days)
  248. }
  249. _, err = s.Request("PUT", uri, nil)
  250. return
  251. }
  252. // GuildBanDelete removes the given user from the guild bans
  253. // guildID : The ID of a Guild.
  254. // userID : The ID of a User
  255. func (s *Session) GuildBanDelete(guildID, userID string) (err error) {
  256. _, err = s.Request("DELETE", GUILD_BAN(guildID, userID), nil)
  257. return
  258. }
  259. // GuildMemberDelete removes the given user from the given guild.
  260. // guildID : The ID of a Guild.
  261. // userID : The ID of a User
  262. func (s *Session) GuildMemberDelete(guildID, userID string) (err error) {
  263. _, err = s.Request("DELETE", GUILD_MEMBER_DEL(guildID, userID), nil)
  264. return
  265. }
  266. // GuildChannels returns an array of Channel structures for all channels of a
  267. // given guild.
  268. // guildID : The ID of a Guild.
  269. func (s *Session) GuildChannels(guildID string) (st []*Channel, err error) {
  270. body, err := s.Request("GET", GUILD_CHANNELS(guildID), nil)
  271. err = json.Unmarshal(body, &st)
  272. return
  273. }
  274. // GuildChannelCreate creates a new channel in the given guild
  275. // guildID : The ID of a Guild.
  276. // name : Name of the channel (2-100 chars length)
  277. // ctype : Tpye of the channel (voice or text)
  278. func (s *Session) GuildChannelCreate(guildID, name, ctype string) (st *Channel, err error) {
  279. data := struct {
  280. Name string `json:"name"`
  281. Type string `json:"type"`
  282. }{name, ctype}
  283. body, err := s.Request("POST", GUILD_CHANNELS(guildID), data)
  284. err = json.Unmarshal(body, &st)
  285. return
  286. }
  287. // GuildInvites returns an array of Invite structures for the given guild
  288. // guildID : The ID of a Guild.
  289. func (s *Session) GuildInvites(guildID string) (st []*Invite, err error) {
  290. body, err := s.Request("GET", GUILD_INVITES(guildID), nil)
  291. err = json.Unmarshal(body, &st)
  292. return
  293. }
  294. // GuildInviteCreate creates a new invite for the given guild.
  295. // guildID : The ID of a Guild.
  296. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  297. // and XkcdPass defined.
  298. func (s *Session) GuildInviteCreate(guildID string, i Invite) (st *Invite, err error) {
  299. data := struct {
  300. MaxAge int `json:"max_age"`
  301. MaxUses int `json:"max_uses"`
  302. Temporary bool `json:"temporary"`
  303. XKCDPass bool `json:"xkcdpass"`
  304. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  305. body, err := s.Request("POST", GUILD_INVITES(guildID), data)
  306. err = json.Unmarshal(body, &st)
  307. return
  308. }
  309. // GuildRoles returns all roles for a given guild.
  310. func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
  311. body, err := s.Request("GET", GUILD_ROLES(guildID), nil)
  312. err = json.Unmarshal(body, &st)
  313. return // TODO return pointer
  314. }
  315. // GuildRoleCreate returns a new Guild Role
  316. func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
  317. body, err := s.Request("POST", GUILD_ROLES(guildID), nil)
  318. err = json.Unmarshal(body, &st)
  319. return
  320. }
  321. // GuildRoleEdit updates an existing Guild Role with new values
  322. func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int) (st *Role, err error) {
  323. data := struct {
  324. Name string `json:"name"` // The color the role should have (as a decimal, not hex)
  325. Color int `json:"color"` // Whether to display the role's users separately
  326. Hoist bool `json:"hoist"` // The role's name (overwrites existing)
  327. Permissions int `json:"permissions"` // The overall permissions number of the role (overwrites existing)
  328. }{name, color, hoist, perm}
  329. body, err := s.Request("PATCH", GUILD_ROLE(guildID, roleID), data)
  330. err = json.Unmarshal(body, &st)
  331. return
  332. }
  333. // GuildRoleReorder reoders guild roles
  334. func (s *Session) GuildRoleReorder(guildID string, roles []Role) (st []*Role, err error) {
  335. body, err := s.Request("PATCH", GUILD_ROLES(guildID), roles)
  336. err = json.Unmarshal(body, &st)
  337. return
  338. }
  339. // GuildRoleDelete deletes an existing role.
  340. func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) {
  341. _, err = s.Request("DELETE", GUILD_ROLE(guildID, roleID), nil)
  342. return
  343. }
  344. // ------------------------------------------------------------------------------------------------
  345. // Functions specific to Discord Channels
  346. // ------------------------------------------------------------------------------------------------
  347. // Channel returns a Channel strucutre of a specific Channel.
  348. // channelID : The ID of the Channel you want returend.
  349. func (s *Session) Channel(channelID string) (st *Channel, err error) {
  350. body, err := s.Request("GET", CHANNEL(channelID), nil)
  351. err = json.Unmarshal(body, &st)
  352. return
  353. }
  354. // ChannelEdit edits the given channel
  355. // channelID : The ID of a Channel
  356. // name : The new name to assign the channel.
  357. func (s *Session) ChannelEdit(channelID, name string) (st *Channel, err error) {
  358. data := struct {
  359. Name string `json:"name"`
  360. }{name}
  361. body, err := s.Request("PATCH", CHANNEL(channelID), data)
  362. err = json.Unmarshal(body, &st)
  363. return
  364. }
  365. // ChannelDelete deletes the given channel
  366. // channelID : The ID of a Channel
  367. func (s *Session) ChannelDelete(channelID string) (st *Channel, err error) {
  368. body, err := s.Request("DELETE", CHANNEL(channelID), nil)
  369. err = json.Unmarshal(body, &st)
  370. return
  371. }
  372. // ChannelTyping broadcasts to all members that authenticated user is typing in
  373. // the given channel.
  374. // channelID : The ID of a Channel
  375. func (s *Session) ChannelTyping(channelID string) (err error) {
  376. _, err = s.Request("POST", CHANNEL_TYPING(channelID), nil)
  377. return
  378. }
  379. // ChannelMessages returns an array of Message structures for messaages within
  380. // a given channel.
  381. // channelID : The ID of a Channel.
  382. // limit : The number messages that can be returned.
  383. // beforeID : If provided all messages returned will be before given ID.
  384. // afterID : If provided all messages returned will be after given ID.
  385. func (s *Session) ChannelMessages(channelID string, limit int, beforeID int, afterID int) (st []Message, err error) {
  386. uri := CHANNEL_MESSAGES(channelID)
  387. v := url.Values{}
  388. if limit > 0 {
  389. v.Set("limit", strconv.Itoa(limit))
  390. }
  391. if afterID > 0 {
  392. v.Set("after", strconv.Itoa(afterID))
  393. }
  394. if beforeID > 0 {
  395. v.Set("before", strconv.Itoa(beforeID))
  396. }
  397. if len(v) > 0 {
  398. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  399. }
  400. body, err := s.Request("GET", uri, nil)
  401. if err != nil {
  402. return
  403. }
  404. err = json.Unmarshal(body, &st)
  405. return
  406. }
  407. // ChannelMessageAck acknowledges and marks the given message as read
  408. // channeld : The ID of a Channel
  409. // messageID : the ID of a Message
  410. func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
  411. _, err = s.Request("POST", CHANNEL_MESSAGE_ACK(channelID, messageID), nil)
  412. return
  413. }
  414. // ChannelMessageSend sends a message to the given channel.
  415. // channelID : The ID of a Channel.
  416. // content : The message to send.
  417. // NOTE, mention and tts parameters may be added in 2.x branch.
  418. func (s *Session) ChannelMessageSend(channelID string, content string) (st *Message, err error) {
  419. // TODO: nonce string ?
  420. data := struct {
  421. Content string `json:"content"`
  422. TTS bool `json:"tts"`
  423. }{content, false}
  424. // Send the message to the given channel
  425. response, err := s.Request("POST", CHANNEL_MESSAGES(channelID), data)
  426. err = json.Unmarshal(response, &st)
  427. return
  428. }
  429. // ChannelMessageEdit edits an existing message, replacing it entirely with
  430. // the given content.
  431. // channeld : The ID of a Channel
  432. // messageID : the ID of a Message
  433. func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (st *Message, err error) {
  434. data := struct {
  435. Content string `json:"content"`
  436. }{content}
  437. response, err := s.Request("PATCH", CHANNEL_MESSAGE(channelID, messageID), data)
  438. err = json.Unmarshal(response, &st)
  439. return
  440. }
  441. // ChannelMessageDelete deletes a message from the Channel.
  442. func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) {
  443. _, err = s.Request("DELETE", CHANNEL_MESSAGE(channelID, messageID), nil)
  444. return
  445. }
  446. // ChannelInvites returns an array of Invite structures for the given channel
  447. // channelID : The ID of a Channel
  448. func (s *Session) ChannelInvites(channelID string) (st []*Invite, err error) {
  449. body, err := s.Request("GET", CHANNEL_INVITES(channelID), nil)
  450. err = json.Unmarshal(body, &st)
  451. return
  452. }
  453. // ChannelInviteCreate creates a new invite for the given channel.
  454. // channelID : The ID of a Channel
  455. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  456. // and XkcdPass defined.
  457. func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st *Invite, err error) {
  458. data := struct {
  459. MaxAge int `json:"max_age"`
  460. MaxUses int `json:"max_uses"`
  461. Temporary bool `json:"temporary"`
  462. XKCDPass bool `json:"xkcdpass"`
  463. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  464. body, err := s.Request("POST", CHANNEL_INVITES(channelID), data)
  465. err = json.Unmarshal(body, &st)
  466. return
  467. }
  468. // ChannelPermissionSet creates a Permission Override for the given channel.
  469. // NOTE: This func name may changed. Using Set instead of Create because
  470. // you can both create a new override or update an override with this function.
  471. func (s *Session) ChannelPermissionSet(channelID, targetID, targetType string, allow, deny int) (err error) {
  472. data := struct {
  473. ID string `json:"id"`
  474. Type string `json:"type"`
  475. Allow int `json:"allow"`
  476. Deny int `json:"deny"`
  477. }{targetID, targetType, allow, deny}
  478. _, err = s.Request("PUT", CHANNEL_PERMISSION(channelID, targetID), data)
  479. return
  480. }
  481. // ChannelPermissionDelete deletes a specific permission override for the given channel.
  482. // NOTE: Name of this func may change.
  483. func (s *Session) ChannelPermissionDelete(channelID, targetID string) (err error) {
  484. _, err = s.Request("DELETE", CHANNEL_PERMISSION(channelID, targetID), nil)
  485. return
  486. }
  487. // ------------------------------------------------------------------------------------------------
  488. // Functions specific to Discord Invites
  489. // ------------------------------------------------------------------------------------------------
  490. // Invite returns an Invite structure of the given invite
  491. // inviteID : The invite code (or maybe xkcdpass?)
  492. func (s *Session) Invite(inviteID string) (st *Invite, err error) {
  493. body, err := s.Request("GET", INVITE(inviteID), nil)
  494. err = json.Unmarshal(body, &st)
  495. return
  496. }
  497. // InviteDelete deletes an existing invite
  498. // inviteID : the code (or maybe xkcdpass?) of an invite
  499. func (s *Session) InviteDelete(inviteID string) (st *Invite, err error) {
  500. body, err := s.Request("DELETE", INVITE(inviteID), nil)
  501. err = json.Unmarshal(body, &st)
  502. return
  503. }
  504. // InviteAccept accepts an Invite to a Guild or Channel
  505. // inviteID : The invite code (or maybe xkcdpass?)
  506. func (s *Session) InviteAccept(inviteID string) (st *Invite, err error) {
  507. body, err := s.Request("POST", INVITE(inviteID), nil)
  508. err = json.Unmarshal(body, &st)
  509. return
  510. }
  511. // ------------------------------------------------------------------------------------------------
  512. // Functions specific to Discord Voice
  513. // ------------------------------------------------------------------------------------------------
  514. // VoiceRegions returns the voice server regions
  515. func (s *Session) VoiceRegions() (st []*VoiceRegion, err error) {
  516. body, err := s.Request("GET", VOICE_REGIONS, nil)
  517. err = json.Unmarshal(body, &st)
  518. return
  519. }
  520. // VoiceICE returns the voice server ICE information
  521. func (s *Session) VoiceICE() (st *VoiceICE, err error) {
  522. body, err := s.Request("GET", VOICE_ICE, nil)
  523. err = json.Unmarshal(body, &st)
  524. return
  525. }
  526. // ------------------------------------------------------------------------------------------------
  527. // Functions specific to Discord Websockets
  528. // ------------------------------------------------------------------------------------------------
  529. // Gateway returns the a websocket Gateway address
  530. func (s *Session) Gateway() (gateway string, err error) {
  531. response, err := s.Request("GET", GATEWAY, nil)
  532. var temp map[string]interface{}
  533. err = json.Unmarshal(response, &temp)
  534. gateway = temp["url"].(string)
  535. return
  536. }