restapi.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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. "log"
  20. "mime/multipart"
  21. "net/http"
  22. "net/url"
  23. "strconv"
  24. "strings"
  25. "sync"
  26. "time"
  27. )
  28. // ErrJSONUnmarshal is returned for JSON Unmarshall errors.
  29. var ErrJSONUnmarshal = errors.New("json unmarshal")
  30. // Request makes a (GET/POST/...) Requests to Discord REST API with JSON data.
  31. // All the other Discord REST Calls in this file use this function.
  32. func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) {
  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, 0)
  41. }
  42. // request makes a (GET/POST/...) Requests to Discord REST API.
  43. // Sequence is the sequence number, if it fails with a 502 it will
  44. // retry with sequence+1 until it either succeeds or sequence >= session.MaxRestRetries
  45. func (s *Session) request(method, urlStr, contentType string, b []byte, sequence int) (response []byte, err error) {
  46. // rate limit mutex for this url
  47. // TODO: review for performance improvements
  48. // ideally we just ignore endpoints that we've never
  49. // received a 429 on. But this simple method works and
  50. // is a lot less complex :) It also might even be more
  51. // performat due to less checks and maps.
  52. var mu *sync.Mutex
  53. s.rateLimit.Lock()
  54. if s.rateLimit.url == nil {
  55. s.rateLimit.url = make(map[string]*sync.Mutex)
  56. }
  57. bu := strings.Split(urlStr, "?")
  58. mu, _ = s.rateLimit.url[bu[0]]
  59. if mu == nil {
  60. mu = new(sync.Mutex)
  61. s.rateLimit.url[bu[0]] = mu
  62. }
  63. s.rateLimit.Unlock()
  64. mu.Lock() // lock this URL for ratelimiting
  65. if s.Debug {
  66. log.Printf("API REQUEST %8s :: %s\n", method, urlStr)
  67. log.Printf("API REQUEST PAYLOAD :: [%s]\n", string(b))
  68. }
  69. req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(b))
  70. if err != nil {
  71. return
  72. }
  73. // Not used on initial login..
  74. // TODO: Verify if a login, otherwise complain about no-token
  75. if s.Token != "" {
  76. req.Header.Set("authorization", s.Token)
  77. }
  78. req.Header.Set("Content-Type", contentType)
  79. // TODO: Make a configurable static variable.
  80. req.Header.Set("User-Agent", fmt.Sprintf("DiscordBot (https://github.com/bwmarrin/discordgo, v%s)", VERSION))
  81. if s.Debug {
  82. for k, v := range req.Header {
  83. log.Printf("API REQUEST HEADER :: [%s] = %+v\n", k, v)
  84. }
  85. }
  86. client := &http.Client{Timeout: (20 * time.Second)}
  87. resp, err := client.Do(req)
  88. mu.Unlock() // unlock ratelimit mutex
  89. if err != nil {
  90. return
  91. }
  92. defer func() {
  93. err2 := resp.Body.Close()
  94. if err2 != nil {
  95. log.Println("error closing resp body")
  96. }
  97. }()
  98. response, err = ioutil.ReadAll(resp.Body)
  99. if err != nil {
  100. return
  101. }
  102. if s.Debug {
  103. log.Printf("API RESPONSE STATUS :: %s\n", resp.Status)
  104. for k, v := range resp.Header {
  105. log.Printf("API RESPONSE HEADER :: [%s] = %+v\n", k, v)
  106. }
  107. log.Printf("API RESPONSE BODY :: [%s]\n\n\n", response)
  108. }
  109. switch resp.StatusCode {
  110. case http.StatusOK:
  111. case http.StatusCreated:
  112. case http.StatusNoContent:
  113. // TODO check for 401 response, invalidate token if we get one.
  114. case http.StatusBadGateway:
  115. // Retry sending request if possible
  116. if sequence < s.MaxRestRetries {
  117. s.log(LogInformational, "%s Failed (%s), Retrying...", urlStr, resp.Status)
  118. response, err = s.request(method, urlStr, contentType, b, sequence+1)
  119. } else {
  120. err = fmt.Errorf("Exceeded Max retries HTTP %s, %s", resp.Status, response)
  121. }
  122. case 429: // TOO MANY REQUESTS - Rate limiting
  123. mu.Lock() // lock URL ratelimit mutex
  124. rl := TooManyRequests{}
  125. err = json.Unmarshal(response, &rl)
  126. if err != nil {
  127. s.log(LogError, "rate limit unmarshal error, %s", err)
  128. mu.Unlock()
  129. return
  130. }
  131. s.log(LogInformational, "Rate Limiting %s, retry in %d", urlStr, rl.RetryAfter)
  132. s.handle(RateLimit{TooManyRequests: &rl, URL: urlStr})
  133. time.Sleep(rl.RetryAfter * time.Millisecond)
  134. // we can make the above smarter
  135. // this method can cause longer delays than required
  136. mu.Unlock() // we have to unlock here
  137. response, err = s.request(method, urlStr, contentType, b, sequence)
  138. default: // Error condition
  139. err = fmt.Errorf("HTTP %s, %s", resp.Status, response)
  140. }
  141. return
  142. }
  143. func unmarshal(data []byte, v interface{}) error {
  144. err := json.Unmarshal(data, v)
  145. if err != nil {
  146. return ErrJSONUnmarshal
  147. }
  148. return nil
  149. }
  150. // ------------------------------------------------------------------------------------------------
  151. // Functions specific to Discord Sessions
  152. // ------------------------------------------------------------------------------------------------
  153. // Login asks the Discord server for an authentication token.
  154. func (s *Session) Login(email, password string) (err error) {
  155. data := struct {
  156. Email string `json:"email"`
  157. Password string `json:"password"`
  158. }{email, password}
  159. response, err := s.Request("POST", EndpointLogin, data)
  160. if err != nil {
  161. return
  162. }
  163. temp := struct {
  164. Token string `json:"token"`
  165. }{}
  166. err = unmarshal(response, &temp)
  167. if err != nil {
  168. return
  169. }
  170. s.Token = temp.Token
  171. return
  172. }
  173. // Register sends a Register request to Discord, and returns the authentication token
  174. // Note that this account is temporary and should be verified for future use.
  175. // Another option is to save the authentication token external, but this isn't recommended.
  176. func (s *Session) Register(username string) (token string, err error) {
  177. data := struct {
  178. Username string `json:"username"`
  179. }{username}
  180. response, err := s.Request("POST", EndpointRegister, data)
  181. if err != nil {
  182. return
  183. }
  184. temp := struct {
  185. Token string `json:"token"`
  186. }{}
  187. err = unmarshal(response, &temp)
  188. if err != nil {
  189. return
  190. }
  191. token = temp.Token
  192. return
  193. }
  194. // Logout sends a logout request to Discord.
  195. // This does not seem to actually invalidate the token. So you can still
  196. // make API calls even after a Logout. So, it seems almost pointless to
  197. // even use.
  198. func (s *Session) Logout() (err error) {
  199. // _, err = s.Request("POST", LOGOUT, fmt.Sprintf(`{"token": "%s"}`, s.Token))
  200. if s.Token == "" {
  201. return
  202. }
  203. data := struct {
  204. Token string `json:"token"`
  205. }{s.Token}
  206. _, err = s.Request("POST", EndpointLogout, data)
  207. return
  208. }
  209. // ------------------------------------------------------------------------------------------------
  210. // Functions specific to Discord Users
  211. // ------------------------------------------------------------------------------------------------
  212. // User returns the user details of the given userID
  213. // userID : A user ID or "@me" which is a shortcut of current user ID
  214. func (s *Session) User(userID string) (st *User, err error) {
  215. body, err := s.Request("GET", EndpointUser(userID), nil)
  216. if err != nil {
  217. return
  218. }
  219. err = unmarshal(body, &st)
  220. return
  221. }
  222. // UserAvatar returns an image.Image of a users Avatar.
  223. // userID : A user ID or "@me" which is a shortcut of current user ID
  224. func (s *Session) UserAvatar(userID string) (img image.Image, err error) {
  225. u, err := s.User(userID)
  226. if err != nil {
  227. return
  228. }
  229. body, err := s.Request("GET", EndpointUserAvatar(userID, u.Avatar), nil)
  230. if err != nil {
  231. return
  232. }
  233. img, _, err = image.Decode(bytes.NewReader(body))
  234. return
  235. }
  236. // UserUpdate updates a users settings.
  237. func (s *Session) UserUpdate(email, password, username, avatar, newPassword string) (st *User, err error) {
  238. // NOTE: Avatar must be either the hash/id of existing Avatar or
  239. // data:image/png;base64,BASE64_STRING_OF_NEW_AVATAR_PNG
  240. // to set a new avatar.
  241. // If left blank, avatar will be set to null/blank
  242. data := struct {
  243. Email string `json:"email"`
  244. Password string `json:"password"`
  245. Username string `json:"username"`
  246. Avatar string `json:"avatar,omitempty"`
  247. NewPassword string `json:"new_password,omitempty"`
  248. }{email, password, username, avatar, newPassword}
  249. body, err := s.Request("PATCH", EndpointUser("@me"), data)
  250. if err != nil {
  251. return
  252. }
  253. err = unmarshal(body, &st)
  254. return
  255. }
  256. // UserSettings returns the settings for a given user
  257. func (s *Session) UserSettings() (st *Settings, err error) {
  258. body, err := s.Request("GET", EndpointUserSettings("@me"), nil)
  259. if err != nil {
  260. return
  261. }
  262. err = unmarshal(body, &st)
  263. return
  264. }
  265. // UserChannels returns an array of Channel structures for all private
  266. // channels.
  267. func (s *Session) UserChannels() (st []*Channel, err error) {
  268. body, err := s.Request("GET", EndpointUserChannels("@me"), nil)
  269. if err != nil {
  270. return
  271. }
  272. err = unmarshal(body, &st)
  273. return
  274. }
  275. // UserChannelCreate creates a new User (Private) Channel with another User
  276. // recipientID : A user ID for the user to which this channel is opened with.
  277. func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) {
  278. data := struct {
  279. RecipientID string `json:"recipient_id"`
  280. }{recipientID}
  281. body, err := s.Request("POST", EndpointUserChannels("@me"), data)
  282. if err != nil {
  283. return
  284. }
  285. err = unmarshal(body, &st)
  286. return
  287. }
  288. // UserGuilds returns an array of UserGuild structures for all guilds.
  289. func (s *Session) UserGuilds() (st []*UserGuild, err error) {
  290. body, err := s.Request("GET", EndpointUserGuilds("@me"), nil)
  291. if err != nil {
  292. return
  293. }
  294. err = unmarshal(body, &st)
  295. return
  296. }
  297. // UserGuildSettingsEdit Edits the users notification settings for a guild
  298. // guildID : The ID of the guild to edit the settings on
  299. // settings : The settings to update
  300. func (s *Session) UserGuildSettingsEdit(guildID string, settings *UserGuildSettingsEdit) (st *UserGuildSettings, err error) {
  301. body, err := s.Request("PATCH", EndpointUserGuildSettings("@me", guildID), settings)
  302. if err != nil {
  303. return
  304. }
  305. err = unmarshal(body, &st)
  306. return
  307. }
  308. // UserChannelPermissions returns the permission of a user in a channel.
  309. // userID : The ID of the user to calculate permissions for.
  310. // channelID : The ID of the channel to calculate permission for.
  311. //
  312. // NOTE: This function is now deprecated and will be removed in the future.
  313. // Please see the same function inside state.go
  314. func (s *Session) UserChannelPermissions(userID, channelID string) (apermissions int, err error) {
  315. channel, err := s.State.Channel(channelID)
  316. if err != nil || channel == nil {
  317. channel, err = s.Channel(channelID)
  318. if err != nil {
  319. return
  320. }
  321. }
  322. guild, err := s.State.Guild(channel.GuildID)
  323. if err != nil || guild == nil {
  324. guild, err = s.Guild(channel.GuildID)
  325. if err != nil {
  326. return
  327. }
  328. }
  329. if userID == guild.OwnerID {
  330. apermissions = PermissionAll
  331. return
  332. }
  333. member, err := s.State.Member(guild.ID, userID)
  334. if err != nil || member == nil {
  335. member, err = s.GuildMember(guild.ID, userID)
  336. if err != nil {
  337. return
  338. }
  339. }
  340. for _, role := range guild.Roles {
  341. if role.ID == guild.ID {
  342. apermissions |= role.Permissions
  343. break
  344. }
  345. }
  346. for _, role := range guild.Roles {
  347. for _, roleID := range member.Roles {
  348. if role.ID == roleID {
  349. apermissions |= role.Permissions
  350. break
  351. }
  352. }
  353. }
  354. if apermissions&PermissionAdministrator > 0 {
  355. apermissions |= PermissionAll
  356. }
  357. // Member overwrites can override role overrides, so do two passes
  358. for _, overwrite := range channel.PermissionOverwrites {
  359. for _, roleID := range member.Roles {
  360. if overwrite.Type == "role" && roleID == overwrite.ID {
  361. apermissions &= ^overwrite.Deny
  362. apermissions |= overwrite.Allow
  363. break
  364. }
  365. }
  366. }
  367. for _, overwrite := range channel.PermissionOverwrites {
  368. if overwrite.Type == "member" && overwrite.ID == userID {
  369. apermissions &= ^overwrite.Deny
  370. apermissions |= overwrite.Allow
  371. break
  372. }
  373. }
  374. if apermissions&PermissionManageRoles > 0 {
  375. apermissions |= PermissionAllChannel
  376. }
  377. return
  378. }
  379. // ------------------------------------------------------------------------------------------------
  380. // Functions specific to Discord Guilds
  381. // ------------------------------------------------------------------------------------------------
  382. // Guild returns a Guild structure of a specific Guild.
  383. // guildID : The ID of a Guild
  384. func (s *Session) Guild(guildID string) (st *Guild, err error) {
  385. if s.StateEnabled {
  386. // Attempt to grab the guild from State first.
  387. st, err = s.State.Guild(guildID)
  388. if err == nil {
  389. return
  390. }
  391. }
  392. body, err := s.Request("GET", EndpointGuild(guildID), nil)
  393. if err != nil {
  394. return
  395. }
  396. err = unmarshal(body, &st)
  397. return
  398. }
  399. // GuildCreate creates a new Guild
  400. // name : A name for the Guild (2-100 characters)
  401. func (s *Session) GuildCreate(name string) (st *Guild, err error) {
  402. data := struct {
  403. Name string `json:"name"`
  404. }{name}
  405. body, err := s.Request("POST", EndpointGuilds, data)
  406. if err != nil {
  407. return
  408. }
  409. err = unmarshal(body, &st)
  410. return
  411. }
  412. // GuildEdit edits a new Guild
  413. // guildID : The ID of a Guild
  414. // g : A GuildParams struct with the values Name, Region and VerificationLevel defined.
  415. func (s *Session) GuildEdit(guildID string, g GuildParams) (st *Guild, err error) {
  416. // Bounds checking for VerificationLevel, interval: [0, 3]
  417. if g.VerificationLevel != nil {
  418. val := *g.VerificationLevel
  419. if val < 0 || val > 3 {
  420. err = errors.New("VerificationLevel out of bounds, should be between 0 and 3")
  421. return
  422. }
  423. }
  424. //Bounds checking for regions
  425. if g.Region != "" {
  426. isValid := false
  427. regions, _ := s.VoiceRegions()
  428. for _, r := range regions {
  429. if g.Region == r.ID {
  430. isValid = true
  431. }
  432. }
  433. if !isValid {
  434. var valid []string
  435. for _, r := range regions {
  436. valid = append(valid, r.ID)
  437. }
  438. err = fmt.Errorf("Region not a valid region (%q)", valid)
  439. return
  440. }
  441. }
  442. data := struct {
  443. Name string `json:"name,omitempty"`
  444. Region string `json:"region,omitempty"`
  445. VerificationLevel *VerificationLevel `json:"verification_level,omitempty"`
  446. }{g.Name, g.Region, g.VerificationLevel}
  447. body, err := s.Request("PATCH", EndpointGuild(guildID), data)
  448. if err != nil {
  449. return
  450. }
  451. err = unmarshal(body, &st)
  452. return
  453. }
  454. // GuildDelete deletes a Guild.
  455. // guildID : The ID of a Guild
  456. func (s *Session) GuildDelete(guildID string) (st *Guild, err error) {
  457. body, err := s.Request("DELETE", EndpointGuild(guildID), nil)
  458. if err != nil {
  459. return
  460. }
  461. err = unmarshal(body, &st)
  462. return
  463. }
  464. // GuildLeave leaves a Guild.
  465. // guildID : The ID of a Guild
  466. func (s *Session) GuildLeave(guildID string) (err error) {
  467. _, err = s.Request("DELETE", EndpointUserGuild("@me", guildID), nil)
  468. return
  469. }
  470. // GuildBans returns an array of User structures for all bans of a
  471. // given guild.
  472. // guildID : The ID of a Guild.
  473. func (s *Session) GuildBans(guildID string) (st []*User, err error) {
  474. body, err := s.Request("GET", EndpointGuildBans(guildID), nil)
  475. if err != nil {
  476. return
  477. }
  478. err = unmarshal(body, &st)
  479. return
  480. }
  481. // GuildBanCreate bans the given user from the given guild.
  482. // guildID : The ID of a Guild.
  483. // userID : The ID of a User
  484. // days : The number of days of previous comments to delete.
  485. func (s *Session) GuildBanCreate(guildID, userID string, days int) (err error) {
  486. uri := EndpointGuildBan(guildID, userID)
  487. if days > 0 {
  488. uri = fmt.Sprintf("%s?delete-message-days=%d", uri, days)
  489. }
  490. _, err = s.Request("PUT", uri, nil)
  491. return
  492. }
  493. // GuildBanDelete removes the given user from the guild bans
  494. // guildID : The ID of a Guild.
  495. // userID : The ID of a User
  496. func (s *Session) GuildBanDelete(guildID, userID string) (err error) {
  497. _, err = s.Request("DELETE", EndpointGuildBan(guildID, userID), nil)
  498. return
  499. }
  500. // GuildMembers returns a list of members for a guild.
  501. // guildID : The ID of a Guild.
  502. // after : The id of the member to return members after
  503. // limit : max number of members to return (max 1000)
  504. func (s *Session) GuildMembers(guildID string, after string, limit int) (st []*Member, err error) {
  505. uri := EndpointGuildMembers(guildID)
  506. v := url.Values{}
  507. if after != "" {
  508. v.Set("after", after)
  509. }
  510. if limit > 0 {
  511. v.Set("limit", strconv.Itoa(limit))
  512. }
  513. if len(v) > 0 {
  514. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  515. }
  516. body, err := s.Request("GET", uri, nil)
  517. if err != nil {
  518. return
  519. }
  520. err = unmarshal(body, &st)
  521. return
  522. }
  523. // GuildMember returns a member of a guild.
  524. // guildID : The ID of a Guild.
  525. // userID : The ID of a User
  526. func (s *Session) GuildMember(guildID, userID string) (st *Member, err error) {
  527. body, err := s.Request("GET", EndpointGuildMember(guildID, userID), nil)
  528. if err != nil {
  529. return
  530. }
  531. err = unmarshal(body, &st)
  532. return
  533. }
  534. // GuildMemberDelete removes the given user from the given guild.
  535. // guildID : The ID of a Guild.
  536. // userID : The ID of a User
  537. func (s *Session) GuildMemberDelete(guildID, userID string) (err error) {
  538. _, err = s.Request("DELETE", EndpointGuildMember(guildID, userID), nil)
  539. return
  540. }
  541. // GuildMemberEdit edits the roles of a member.
  542. // guildID : The ID of a Guild.
  543. // userID : The ID of a User.
  544. // roles : A list of role ID's to set on the member.
  545. func (s *Session) GuildMemberEdit(guildID, userID string, roles []string) (err error) {
  546. data := struct {
  547. Roles []string `json:"roles"`
  548. }{roles}
  549. _, err = s.Request("PATCH", EndpointGuildMember(guildID, userID), data)
  550. if err != nil {
  551. return
  552. }
  553. return
  554. }
  555. // GuildMemberMove moves a guild member from one voice channel to another/none
  556. // guildID : The ID of a Guild.
  557. // userID : The ID of a User.
  558. // channelID : The ID of a channel to move user to, or null?
  559. // NOTE : I am not entirely set on the name of this function and it may change
  560. // prior to the final 1.0.0 release of Discordgo
  561. func (s *Session) GuildMemberMove(guildID, userID, channelID string) (err error) {
  562. data := struct {
  563. ChannelID string `json:"channel_id"`
  564. }{channelID}
  565. _, err = s.Request("PATCH", EndpointGuildMember(guildID, userID), data)
  566. if err != nil {
  567. return
  568. }
  569. return
  570. }
  571. // GuildMemberNickname updates the nickname of a guild member
  572. // guildID : The ID of a guild
  573. // userID : The ID of a user
  574. func (s *Session) GuildMemberNickname(guildID, userID, nickname string) (err error) {
  575. data := struct {
  576. Nick string `json:"nick"`
  577. }{nickname}
  578. _, err = s.Request("PATCH", EndpointGuildMember(guildID, userID), data)
  579. return
  580. }
  581. // GuildChannels returns an array of Channel structures for all channels of a
  582. // given guild.
  583. // guildID : The ID of a Guild.
  584. func (s *Session) GuildChannels(guildID string) (st []*Channel, err error) {
  585. body, err := s.request("GET", EndpointGuildChannels(guildID), "", nil, 0)
  586. if err != nil {
  587. return
  588. }
  589. err = unmarshal(body, &st)
  590. return
  591. }
  592. // GuildChannelCreate creates a new channel in the given guild
  593. // guildID : The ID of a Guild.
  594. // name : Name of the channel (2-100 chars length)
  595. // ctype : Tpye of the channel (voice or text)
  596. func (s *Session) GuildChannelCreate(guildID, name, ctype string) (st *Channel, err error) {
  597. data := struct {
  598. Name string `json:"name"`
  599. Type string `json:"type"`
  600. }{name, ctype}
  601. body, err := s.Request("POST", EndpointGuildChannels(guildID), data)
  602. if err != nil {
  603. return
  604. }
  605. err = unmarshal(body, &st)
  606. return
  607. }
  608. // GuildChannelsReorder updates the order of channels in a guild
  609. // guildID : The ID of a Guild.
  610. // channels : Updated channels.
  611. func (s *Session) GuildChannelsReorder(guildID string, channels []*Channel) (err error) {
  612. _, err = s.Request("PATCH", EndpointGuildChannels(guildID), channels)
  613. return
  614. }
  615. // GuildInvites returns an array of Invite structures for the given guild
  616. // guildID : The ID of a Guild.
  617. func (s *Session) GuildInvites(guildID string) (st []*Invite, err error) {
  618. body, err := s.Request("GET", EndpointGuildInvites(guildID), nil)
  619. if err != nil {
  620. return
  621. }
  622. err = unmarshal(body, &st)
  623. return
  624. }
  625. // GuildRoles returns all roles for a given guild.
  626. // guildID : The ID of a Guild.
  627. func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
  628. body, err := s.Request("GET", EndpointGuildRoles(guildID), nil)
  629. if err != nil {
  630. return
  631. }
  632. err = unmarshal(body, &st)
  633. return // TODO return pointer
  634. }
  635. // GuildRoleCreate returns a new Guild Role.
  636. // guildID: The ID of a Guild.
  637. func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
  638. body, err := s.Request("POST", EndpointGuildRoles(guildID), nil)
  639. if err != nil {
  640. return
  641. }
  642. err = unmarshal(body, &st)
  643. return
  644. }
  645. // GuildRoleEdit updates an existing Guild Role with new values
  646. // guildID : The ID of a Guild.
  647. // roleID : The ID of a Role.
  648. // name : The name of the Role.
  649. // color : The color of the role (decimal, not hex).
  650. // hoist : Whether to display the role's users separately.
  651. // perm : The permissions for the role.
  652. // mention : Whether this role is mentionable
  653. func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int, mention bool) (st *Role, err error) {
  654. // Prevent sending a color int that is too big.
  655. if color > 0xFFFFFF {
  656. err = fmt.Errorf("color value cannot be larger than 0xFFFFFF")
  657. }
  658. data := struct {
  659. Name string `json:"name"` // The role's name (overwrites existing)
  660. Color int `json:"color"` // The color the role should have (as a decimal, not hex)
  661. Hoist bool `json:"hoist"` // Whether to display the role's users separately
  662. Permissions int `json:"permissions"` // The overall permissions number of the role (overwrites existing)
  663. Mentionable bool `json:"mentionable"` // Whether this role is mentionable
  664. }{name, color, hoist, perm, mention}
  665. body, err := s.Request("PATCH", EndpointGuildRole(guildID, roleID), data)
  666. if err != nil {
  667. return
  668. }
  669. err = unmarshal(body, &st)
  670. return
  671. }
  672. // GuildRoleReorder reoders guild roles
  673. // guildID : The ID of a Guild.
  674. // roles : A list of ordered roles.
  675. func (s *Session) GuildRoleReorder(guildID string, roles []*Role) (st []*Role, err error) {
  676. body, err := s.Request("PATCH", EndpointGuildRoles(guildID), roles)
  677. if err != nil {
  678. return
  679. }
  680. err = unmarshal(body, &st)
  681. return
  682. }
  683. // GuildRoleDelete deletes an existing role.
  684. // guildID : The ID of a Guild.
  685. // roleID : The ID of a Role.
  686. func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) {
  687. _, err = s.Request("DELETE", EndpointGuildRole(guildID, roleID), nil)
  688. return
  689. }
  690. // GuildIntegrations returns an array of Integrations for a guild.
  691. // guildID : The ID of a Guild.
  692. func (s *Session) GuildIntegrations(guildID string) (st []*GuildIntegration, err error) {
  693. body, err := s.Request("GET", EndpointGuildIntegrations(guildID), nil)
  694. if err != nil {
  695. return
  696. }
  697. err = unmarshal(body, &st)
  698. return
  699. }
  700. // GuildIntegrationCreate creates a Guild Integration.
  701. // guildID : The ID of a Guild.
  702. // integrationType : The Integration type.
  703. // integrationID : The ID of an integration.
  704. func (s *Session) GuildIntegrationCreate(guildID, integrationType, integrationID string) (err error) {
  705. data := struct {
  706. Type string `json:"type"`
  707. ID string `json:"id"`
  708. }{integrationType, integrationID}
  709. _, err = s.Request("POST", EndpointGuildIntegrations(guildID), data)
  710. return
  711. }
  712. // GuildIntegrationEdit edits a Guild Integration.
  713. // guildID : The ID of a Guild.
  714. // integrationType : The Integration type.
  715. // integrationID : The ID of an integration.
  716. // expireBehavior : The behavior when an integration subscription lapses (see the integration object documentation).
  717. // expireGracePeriod : Period (in seconds) where the integration will ignore lapsed subscriptions.
  718. // enableEmoticons : Whether emoticons should be synced for this integration (twitch only currently).
  719. func (s *Session) GuildIntegrationEdit(guildID, integrationID string, expireBehavior, expireGracePeriod int, enableEmoticons bool) (err error) {
  720. data := struct {
  721. ExpireBehavior int `json:"expire_behavior"`
  722. ExpireGracePeriod int `json:"expire_grace_period"`
  723. EnableEmoticons bool `json:"enable_emoticons"`
  724. }{expireBehavior, expireGracePeriod, enableEmoticons}
  725. _, err = s.Request("PATCH", EndpointGuildIntegration(guildID, integrationID), data)
  726. return
  727. }
  728. // GuildIntegrationDelete removes the given integration from the Guild.
  729. // guildID : The ID of a Guild.
  730. // integrationID : The ID of an integration.
  731. func (s *Session) GuildIntegrationDelete(guildID, integrationID string) (err error) {
  732. _, err = s.Request("DELETE", EndpointGuildIntegration(guildID, integrationID), nil)
  733. return
  734. }
  735. // GuildIntegrationSync syncs an integration.
  736. // guildID : The ID of a Guild.
  737. // integrationID : The ID of an integration.
  738. func (s *Session) GuildIntegrationSync(guildID, integrationID string) (err error) {
  739. _, err = s.Request("POST", EndpointGuildIntegrationSync(guildID, integrationID), nil)
  740. return
  741. }
  742. // GuildIcon returns an image.Image of a guild icon.
  743. // guildID : The ID of a Guild.
  744. func (s *Session) GuildIcon(guildID string) (img image.Image, err error) {
  745. g, err := s.Guild(guildID)
  746. if err != nil {
  747. return
  748. }
  749. if g.Icon == "" {
  750. err = errors.New("Guild does not have an icon set.")
  751. return
  752. }
  753. body, err := s.Request("GET", EndpointGuildIcon(guildID, g.Icon), nil)
  754. if err != nil {
  755. return
  756. }
  757. img, _, err = image.Decode(bytes.NewReader(body))
  758. return
  759. }
  760. // GuildSplash returns an image.Image of a guild splash image.
  761. // guildID : The ID of a Guild.
  762. func (s *Session) GuildSplash(guildID string) (img image.Image, err error) {
  763. g, err := s.Guild(guildID)
  764. if err != nil {
  765. return
  766. }
  767. if g.Splash == "" {
  768. err = errors.New("Guild does not have a splash set.")
  769. return
  770. }
  771. body, err := s.Request("GET", EndpointGuildSplash(guildID, g.Splash), nil)
  772. if err != nil {
  773. return
  774. }
  775. img, _, err = image.Decode(bytes.NewReader(body))
  776. return
  777. }
  778. // GuildEmbed returns the embed for a Guild.
  779. // guildID : The ID of a Guild.
  780. func (s *Session) GuildEmbed(guildID string) (st *GuildEmbed, err error) {
  781. body, err := s.Request("GET", EndpointGuildEmbed(guildID), nil)
  782. if err != nil {
  783. return
  784. }
  785. err = unmarshal(body, &st)
  786. return
  787. }
  788. // GuildEmbedEdit returns the embed for a Guild.
  789. // guildID : The ID of a Guild.
  790. func (s *Session) GuildEmbedEdit(guildID string, enabled bool, channelID string) (err error) {
  791. data := GuildEmbed{enabled, channelID}
  792. _, err = s.Request("PATCH", EndpointGuildEmbed(guildID), data)
  793. return
  794. }
  795. // ------------------------------------------------------------------------------------------------
  796. // Functions specific to Discord Channels
  797. // ------------------------------------------------------------------------------------------------
  798. // Channel returns a Channel strucutre of a specific Channel.
  799. // channelID : The ID of the Channel you want returned.
  800. func (s *Session) Channel(channelID string) (st *Channel, err error) {
  801. body, err := s.Request("GET", EndpointChannel(channelID), nil)
  802. if err != nil {
  803. return
  804. }
  805. err = unmarshal(body, &st)
  806. return
  807. }
  808. // ChannelEdit edits the given channel
  809. // channelID : The ID of a Channel
  810. // name : The new name to assign the channel.
  811. func (s *Session) ChannelEdit(channelID, name string) (st *Channel, err error) {
  812. data := struct {
  813. Name string `json:"name"`
  814. }{name}
  815. body, err := s.Request("PATCH", EndpointChannel(channelID), data)
  816. if err != nil {
  817. return
  818. }
  819. err = unmarshal(body, &st)
  820. return
  821. }
  822. // ChannelDelete deletes the given channel
  823. // channelID : The ID of a Channel
  824. func (s *Session) ChannelDelete(channelID string) (st *Channel, err error) {
  825. body, err := s.Request("DELETE", EndpointChannel(channelID), nil)
  826. if err != nil {
  827. return
  828. }
  829. err = unmarshal(body, &st)
  830. return
  831. }
  832. // ChannelTyping broadcasts to all members that authenticated user is typing in
  833. // the given channel.
  834. // channelID : The ID of a Channel
  835. func (s *Session) ChannelTyping(channelID string) (err error) {
  836. _, err = s.Request("POST", EndpointChannelTyping(channelID), nil)
  837. return
  838. }
  839. // ChannelMessages returns an array of Message structures for messages within
  840. // a given channel.
  841. // channelID : The ID of a Channel.
  842. // limit : The number messages that can be returned. (max 100)
  843. // beforeID : If provided all messages returned will be before given ID.
  844. // afterID : If provided all messages returned will be after given ID.
  845. func (s *Session) ChannelMessages(channelID string, limit int, beforeID, afterID string) (st []*Message, err error) {
  846. uri := EndpointChannelMessages(channelID)
  847. v := url.Values{}
  848. if limit > 0 {
  849. v.Set("limit", strconv.Itoa(limit))
  850. }
  851. if afterID != "" {
  852. v.Set("after", afterID)
  853. }
  854. if beforeID != "" {
  855. v.Set("before", beforeID)
  856. }
  857. if len(v) > 0 {
  858. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  859. }
  860. body, err := s.Request("GET", uri, nil)
  861. if err != nil {
  862. return
  863. }
  864. err = unmarshal(body, &st)
  865. return
  866. }
  867. // ChannelMessage gets a single message by ID from a given channel.
  868. // channeld : The ID of a Channel
  869. // messageID : the ID of a Message
  870. func (s *Session) ChannelMessage(channelID, messageID string) (st *Message, err error) {
  871. response, err := s.Request("GET", EndpointChannelMessage(channelID, messageID), nil)
  872. if err != nil {
  873. return
  874. }
  875. err = unmarshal(response, &st)
  876. return
  877. }
  878. // ChannelMessageAck acknowledges and marks the given message as read
  879. // channeld : The ID of a Channel
  880. // messageID : the ID of a Message
  881. func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
  882. _, err = s.request("POST", EndpointChannelMessageAck(channelID, messageID), "", nil, 0)
  883. return
  884. }
  885. // channelMessageSend sends a message to the given channel.
  886. // channelID : The ID of a Channel.
  887. // content : The message to send.
  888. // tts : Whether to send the message with TTS.
  889. func (s *Session) channelMessageSend(channelID, content string, tts bool) (st *Message, err error) {
  890. // TODO: nonce string ?
  891. data := struct {
  892. Content string `json:"content"`
  893. TTS bool `json:"tts"`
  894. }{content, tts}
  895. // Send the message to the given channel
  896. response, err := s.Request("POST", EndpointChannelMessages(channelID), data)
  897. if err != nil {
  898. return
  899. }
  900. err = unmarshal(response, &st)
  901. return
  902. }
  903. // ChannelMessageSend sends a message to the given channel.
  904. // channelID : The ID of a Channel.
  905. // content : The message to send.
  906. func (s *Session) ChannelMessageSend(channelID string, content string) (st *Message, err error) {
  907. return s.channelMessageSend(channelID, content, false)
  908. }
  909. // ChannelMessageSendTTS sends a message to the given channel with Text to Speech.
  910. // channelID : The ID of a Channel.
  911. // content : The message to send.
  912. func (s *Session) ChannelMessageSendTTS(channelID string, content string) (st *Message, err error) {
  913. return s.channelMessageSend(channelID, content, true)
  914. }
  915. // ChannelMessageEdit edits an existing message, replacing it entirely with
  916. // the given content.
  917. // channeld : The ID of a Channel
  918. // messageID : the ID of a Message
  919. func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (st *Message, err error) {
  920. data := struct {
  921. Content string `json:"content"`
  922. }{content}
  923. response, err := s.Request("PATCH", EndpointChannelMessage(channelID, messageID), data)
  924. if err != nil {
  925. return
  926. }
  927. err = unmarshal(response, &st)
  928. return
  929. }
  930. // ChannelMessageDelete deletes a message from the Channel.
  931. func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) {
  932. _, err = s.Request("DELETE", EndpointChannelMessage(channelID, messageID), nil)
  933. return
  934. }
  935. // ChannelMessagesBulkDelete bulk deletes the messages from the channel for the provided messageIDs.
  936. // If only one messageID is in the slice call channelMessageDelete funciton.
  937. // If the slice is empty do nothing.
  938. // channelID : The ID of the channel for the messages to delete.
  939. // messages : The IDs of the messages to be deleted. A slice of string IDs. A maximum of 100 messages.
  940. func (s *Session) ChannelMessagesBulkDelete(channelID string, messages []string) (err error) {
  941. if len(messages) == 0 {
  942. return
  943. }
  944. if len(messages) == 1 {
  945. err = s.ChannelMessageDelete(channelID, messages[0])
  946. return
  947. }
  948. if len(messages) > 100 {
  949. messages = messages[:100]
  950. }
  951. data := struct {
  952. Messages []string `json:"messages"`
  953. }{messages}
  954. _, err = s.Request("POST", EndpointChannelMessagesBulkDelete(channelID), data)
  955. return
  956. }
  957. // ChannelMessagePin pins a message within a given channel.
  958. // channelID: The ID of a channel.
  959. // messageID: The ID of a message.
  960. func (s *Session) ChannelMessagePin(channelID, messageID string) (err error) {
  961. _, err = s.Request("PUT", EndpointChannelMessagePin(channelID, messageID), nil)
  962. return
  963. }
  964. // ChannelMessageUnpin unpins a message within a given channel.
  965. // channelID: The ID of a channel.
  966. // messageID: The ID of a message.
  967. func (s *Session) ChannelMessageUnpin(channelID, messageID string) (err error) {
  968. _, err = s.Request("DELETE", EndpointChannelMessagePin(channelID, messageID), nil)
  969. return
  970. }
  971. // ChannelMessagesPinned returns an array of Message structures for pinned messages
  972. // within a given channel
  973. // channelID : The ID of a Channel.
  974. func (s *Session) ChannelMessagesPinned(channelID string) (st []*Message, err error) {
  975. body, err := s.Request("GET", EndpointChannelMessagesPins(channelID), nil)
  976. if err != nil {
  977. return
  978. }
  979. err = unmarshal(body, &st)
  980. return
  981. }
  982. // ChannelFileSend sends a file to the given channel.
  983. // channelID : The ID of a Channel.
  984. // name: The name of the file.
  985. // io.Reader : A reader for the file contents.
  986. func (s *Session) ChannelFileSend(channelID, name string, r io.Reader) (st *Message, err error) {
  987. return s.ChannelFileSendWithMessage(channelID, "", name, r)
  988. }
  989. // ChannelFileSendWithMessage sends a file to the given channel with an message.
  990. // channelID : The ID of a Channel.
  991. // content: Optional Message content.
  992. // name: The name of the file.
  993. // io.Reader : A reader for the file contents.
  994. func (s *Session) ChannelFileSendWithMessage(channelID, content string, name string, r io.Reader) (st *Message, err error) {
  995. body := &bytes.Buffer{}
  996. bodywriter := multipart.NewWriter(body)
  997. if len(content) != 0 {
  998. if err := bodywriter.WriteField("content", content); err != nil {
  999. return nil, err
  1000. }
  1001. }
  1002. writer, err := bodywriter.CreateFormFile("file", name)
  1003. if err != nil {
  1004. return nil, err
  1005. }
  1006. _, err = io.Copy(writer, r)
  1007. if err != nil {
  1008. return
  1009. }
  1010. err = bodywriter.Close()
  1011. if err != nil {
  1012. return
  1013. }
  1014. response, err := s.request("POST", EndpointChannelMessages(channelID), bodywriter.FormDataContentType(), body.Bytes(), 0)
  1015. if err != nil {
  1016. return
  1017. }
  1018. err = unmarshal(response, &st)
  1019. return
  1020. }
  1021. // ChannelInvites returns an array of Invite structures for the given channel
  1022. // channelID : The ID of a Channel
  1023. func (s *Session) ChannelInvites(channelID string) (st []*Invite, err error) {
  1024. body, err := s.Request("GET", EndpointChannelInvites(channelID), nil)
  1025. if err != nil {
  1026. return
  1027. }
  1028. err = unmarshal(body, &st)
  1029. return
  1030. }
  1031. // ChannelInviteCreate creates a new invite for the given channel.
  1032. // channelID : The ID of a Channel
  1033. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  1034. // and XkcdPass defined.
  1035. func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st *Invite, err error) {
  1036. data := struct {
  1037. MaxAge int `json:"max_age"`
  1038. MaxUses int `json:"max_uses"`
  1039. Temporary bool `json:"temporary"`
  1040. XKCDPass string `json:"xkcdpass"`
  1041. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  1042. body, err := s.Request("POST", EndpointChannelInvites(channelID), data)
  1043. if err != nil {
  1044. return
  1045. }
  1046. err = unmarshal(body, &st)
  1047. return
  1048. }
  1049. // ChannelPermissionSet creates a Permission Override for the given channel.
  1050. // NOTE: This func name may changed. Using Set instead of Create because
  1051. // you can both create a new override or update an override with this function.
  1052. func (s *Session) ChannelPermissionSet(channelID, targetID, targetType string, allow, deny int) (err error) {
  1053. data := struct {
  1054. ID string `json:"id"`
  1055. Type string `json:"type"`
  1056. Allow int `json:"allow"`
  1057. Deny int `json:"deny"`
  1058. }{targetID, targetType, allow, deny}
  1059. _, err = s.Request("PUT", EndpointChannelPermission(channelID, targetID), data)
  1060. return
  1061. }
  1062. // ChannelPermissionDelete deletes a specific permission override for the given channel.
  1063. // NOTE: Name of this func may change.
  1064. func (s *Session) ChannelPermissionDelete(channelID, targetID string) (err error) {
  1065. _, err = s.Request("DELETE", EndpointChannelPermission(channelID, targetID), nil)
  1066. return
  1067. }
  1068. // ------------------------------------------------------------------------------------------------
  1069. // Functions specific to Discord Invites
  1070. // ------------------------------------------------------------------------------------------------
  1071. // Invite returns an Invite structure of the given invite
  1072. // inviteID : The invite code (or maybe xkcdpass?)
  1073. func (s *Session) Invite(inviteID string) (st *Invite, err error) {
  1074. body, err := s.Request("GET", EndpointInvite(inviteID), nil)
  1075. if err != nil {
  1076. return
  1077. }
  1078. err = unmarshal(body, &st)
  1079. return
  1080. }
  1081. // InviteDelete deletes an existing invite
  1082. // inviteID : the code (or maybe xkcdpass?) of an invite
  1083. func (s *Session) InviteDelete(inviteID string) (st *Invite, err error) {
  1084. body, err := s.Request("DELETE", EndpointInvite(inviteID), nil)
  1085. if err != nil {
  1086. return
  1087. }
  1088. err = unmarshal(body, &st)
  1089. return
  1090. }
  1091. // InviteAccept accepts an Invite to a Guild or Channel
  1092. // inviteID : The invite code (or maybe xkcdpass?)
  1093. func (s *Session) InviteAccept(inviteID string) (st *Invite, err error) {
  1094. body, err := s.Request("POST", EndpointInvite(inviteID), nil)
  1095. if err != nil {
  1096. return
  1097. }
  1098. err = unmarshal(body, &st)
  1099. return
  1100. }
  1101. // ------------------------------------------------------------------------------------------------
  1102. // Functions specific to Discord Voice
  1103. // ------------------------------------------------------------------------------------------------
  1104. // VoiceRegions returns the voice server regions
  1105. func (s *Session) VoiceRegions() (st []*VoiceRegion, err error) {
  1106. body, err := s.Request("GET", EndpointVoiceRegions, nil)
  1107. if err != nil {
  1108. return
  1109. }
  1110. err = unmarshal(body, &st)
  1111. return
  1112. }
  1113. // VoiceICE returns the voice server ICE information
  1114. func (s *Session) VoiceICE() (st *VoiceICE, err error) {
  1115. body, err := s.Request("GET", EndpointVoiceIce, nil)
  1116. if err != nil {
  1117. return
  1118. }
  1119. err = unmarshal(body, &st)
  1120. return
  1121. }
  1122. // ------------------------------------------------------------------------------------------------
  1123. // Functions specific to Discord Websockets
  1124. // ------------------------------------------------------------------------------------------------
  1125. // Gateway returns the a websocket Gateway address
  1126. func (s *Session) Gateway() (gateway string, err error) {
  1127. response, err := s.Request("GET", EndpointGateway, nil)
  1128. if err != nil {
  1129. return
  1130. }
  1131. temp := struct {
  1132. URL string `json:"url"`
  1133. }{}
  1134. err = unmarshal(response, &temp)
  1135. if err != nil {
  1136. return
  1137. }
  1138. gateway = temp.URL
  1139. return
  1140. }
  1141. // Functions specific to Webhooks
  1142. // WebhookCreate returns a new Webhook.
  1143. // channelID: The ID of a Channel.
  1144. // name : The name of the webhook.
  1145. // avatar : The avatar of the webhook.
  1146. func (s *Session) WebhookCreate(channelID, name, avatar string) (st *Webhook, err error) {
  1147. data := struct {
  1148. Name string `json:"name"`
  1149. Avatar string `json:"avatar,omitempty"`
  1150. }{name, avatar}
  1151. body, err := s.Request("POST", EndpointChannelWebhooks(channelID), data)
  1152. if err != nil {
  1153. return
  1154. }
  1155. err = unmarshal(body, &st)
  1156. return
  1157. }
  1158. // ChannelWebhooks returns all webhooks for a given channel.
  1159. // channelID: The ID of a channel.
  1160. func (s *Session) ChannelWebhooks(channelID string) (st []*Webhook, err error) {
  1161. body, err := s.Request("GET", EndpointChannelWebhooks(channelID), nil)
  1162. if err != nil {
  1163. return
  1164. }
  1165. err = unmarshal(body, &st)
  1166. return
  1167. }
  1168. // GuildWebhooks returns all webhooks for a given guild.
  1169. // guildID: The ID of a Guild.
  1170. func (s *Session) GuildWebhooks(guildID string) (st []*Webhook, err error) {
  1171. body, err := s.Request("GET", EndpointGuildWebhooks(guildID), nil)
  1172. if err != nil {
  1173. return
  1174. }
  1175. err = unmarshal(body, &st)
  1176. return
  1177. }
  1178. // Webhook returns a webhook for a given ID
  1179. // webhookID: The ID of a webhook.
  1180. func (s *Session) Webhook(webhookID string) (st *Webhook, err error) {
  1181. body, err := s.Request("GET", EndpointWebhook(webhookID), nil)
  1182. if err != nil {
  1183. return
  1184. }
  1185. err = unmarshal(body, &st)
  1186. return
  1187. }
  1188. // WebhookWithToken returns a webhook for a given ID
  1189. // webhookID: The ID of a webhook.
  1190. // token : The auth token for the webhook.
  1191. func (s *Session) WebhookWithToken(webhookID, token string) (st *Webhook, err error) {
  1192. body, err := s.Request("GET", EndpointWebhookToken(webhookID, token), nil)
  1193. if err != nil {
  1194. return
  1195. }
  1196. err = unmarshal(body, &st)
  1197. return
  1198. }
  1199. // WebhookEdit updates an existing Webhook.
  1200. // webhookID: The ID of a webhook.
  1201. // name : The name of the webhook.
  1202. // avatar : The avatar of the webhook.
  1203. func (s *Session) WebhookEdit(webhookID, name, avatar string) (st *Role, err error) {
  1204. data := struct {
  1205. Name string `json:"name,omitempty"`
  1206. Avatar string `json:"avatar,omitempty"`
  1207. }{name, avatar}
  1208. body, err := s.Request("PATCH", EndpointWebhook(webhookID), data)
  1209. if err != nil {
  1210. return
  1211. }
  1212. err = unmarshal(body, &st)
  1213. return
  1214. }
  1215. // WebhookEditWithToken updates an existing Webhook with an auth token.
  1216. // webhookID: The ID of a webhook.
  1217. // token : The auth token for the webhook.
  1218. // name : The name of the webhook.
  1219. // avatar : The avatar of the webhook.
  1220. func (s *Session) WebhookEditWithToken(webhookID, token, name, avatar string) (st *Role, err error) {
  1221. data := struct {
  1222. Name string `json:"name,omitempty"`
  1223. Avatar string `json:"avatar,omitempty"`
  1224. }{name, avatar}
  1225. body, err := s.Request("PATCH", EndpointWebhookToken(webhookID, token), data)
  1226. if err != nil {
  1227. return
  1228. }
  1229. err = unmarshal(body, &st)
  1230. return
  1231. }
  1232. // WebhookDelete deletes a webhook for a given ID
  1233. // webhookID: The ID of a webhook.
  1234. func (s *Session) WebhookDelete(webhookID string) (st *Webhook, err error) {
  1235. body, err := s.Request("DELETE", EndpointWebhook(webhookID), nil)
  1236. if err != nil {
  1237. return
  1238. }
  1239. err = unmarshal(body, &st)
  1240. return
  1241. }
  1242. // WebhookDeleteWithToken deletes a webhook for a given ID with an auth token.
  1243. // webhookID: The ID of a webhook.
  1244. // token : The auth token for the webhook.
  1245. func (s *Session) WebhookDeleteWithToken(webhookID, token string) (st *Webhook, err error) {
  1246. body, err := s.Request("DELETE", EndpointWebhookToken(webhookID, token), nil)
  1247. if err != nil {
  1248. return
  1249. }
  1250. err = unmarshal(body, &st)
  1251. return
  1252. }
  1253. // WebhookExecute executes a webhook.
  1254. // webhookID: The ID of a webhook.
  1255. // token : The auth token for the bebhook
  1256. func (s *Session) WebhookExecute(webhookID, token string, wait bool, data *WebhookParams) (err error) {
  1257. uri := EndpointWebhookToken(webhookID, token)
  1258. if wait {
  1259. uri += "?wait=true"
  1260. }
  1261. fmt.Println(uri)
  1262. _, err = s.Request("POST", uri, data)
  1263. return
  1264. }