restapi.go 21 KB

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