restapi.go 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  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. "time"
  26. )
  27. // ErrJSONUnmarshal is returned for JSON Unmarshall errors.
  28. var ErrJSONUnmarshal = errors.New("json unmarshal")
  29. // Request is the same as RequestWithBucketID but the bucket id is the same as the urlStr
  30. func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) {
  31. return s.RequestWithBucketID(method, urlStr, data, strings.SplitN(urlStr, "?", 2)[0])
  32. }
  33. // RequestWithBucketID makes a (GET/POST/...) Requests to Discord REST API with JSON data.
  34. func (s *Session) RequestWithBucketID(method, urlStr string, data interface{}, bucketID string) (response []byte, err error) {
  35. var body []byte
  36. if data != nil {
  37. body, err = json.Marshal(data)
  38. if err != nil {
  39. return
  40. }
  41. }
  42. return s.request(method, urlStr, "application/json", body, bucketID, 0)
  43. }
  44. // request makes a (GET/POST/...) Requests to Discord REST API.
  45. // Sequence is the sequence number, if it fails with a 502 it will
  46. // retry with sequence+1 until it either succeeds or sequence >= session.MaxRestRetries
  47. func (s *Session) request(method, urlStr, contentType string, b []byte, bucketID string, sequence int) (response []byte, err error) {
  48. if bucketID == "" {
  49. bucketID = strings.SplitN(urlStr, "?", 2)[0]
  50. }
  51. bucket := s.ratelimiter.LockBucket(bucketID)
  52. if s.Debug {
  53. log.Printf("API REQUEST %8s :: %s\n", method, urlStr)
  54. log.Printf("API REQUEST PAYLOAD :: [%s]\n", string(b))
  55. }
  56. req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(b))
  57. if err != nil {
  58. bucket.Release(nil)
  59. return
  60. }
  61. // Not used on initial login..
  62. // TODO: Verify if a login, otherwise complain about no-token
  63. if s.Token != "" {
  64. req.Header.Set("authorization", s.Token)
  65. }
  66. req.Header.Set("Content-Type", contentType)
  67. // TODO: Make a configurable static variable.
  68. req.Header.Set("User-Agent", fmt.Sprintf("DiscordBot (https://github.com/bwmarrin/discordgo, v%s)", VERSION))
  69. if s.Debug {
  70. for k, v := range req.Header {
  71. log.Printf("API REQUEST HEADER :: [%s] = %+v\n", k, v)
  72. }
  73. }
  74. resp, err := s.Client.Do(req)
  75. if err != nil {
  76. bucket.Release(nil)
  77. return
  78. }
  79. defer func() {
  80. err2 := resp.Body.Close()
  81. if err2 != nil {
  82. log.Println("error closing resp body")
  83. }
  84. }()
  85. err = bucket.Release(resp.Header)
  86. if err != nil {
  87. return
  88. }
  89. response, err = ioutil.ReadAll(resp.Body)
  90. if err != nil {
  91. return
  92. }
  93. if s.Debug {
  94. log.Printf("API RESPONSE STATUS :: %s\n", resp.Status)
  95. for k, v := range resp.Header {
  96. log.Printf("API RESPONSE HEADER :: [%s] = %+v\n", k, v)
  97. }
  98. log.Printf("API RESPONSE BODY :: [%s]\n\n\n", response)
  99. }
  100. switch resp.StatusCode {
  101. case http.StatusOK:
  102. case http.StatusCreated:
  103. case http.StatusNoContent:
  104. // TODO check for 401 response, invalidate token if we get one.
  105. case http.StatusBadGateway:
  106. // Retry sending request if possible
  107. if sequence < s.MaxRestRetries {
  108. s.log(LogInformational, "%s Failed (%s), Retrying...", urlStr, resp.Status)
  109. response, err = s.request(method, urlStr, contentType, b, bucketID, sequence+1)
  110. } else {
  111. err = fmt.Errorf("Exceeded Max retries HTTP %s, %s", resp.Status, response)
  112. }
  113. case 429: // TOO MANY REQUESTS - Rate limiting
  114. rl := TooManyRequests{}
  115. err = json.Unmarshal(response, &rl)
  116. if err != nil {
  117. s.log(LogError, "rate limit unmarshal error, %s", err)
  118. return
  119. }
  120. s.log(LogInformational, "Rate Limiting %s, retry in %d", urlStr, rl.RetryAfter)
  121. s.handleEvent(rateLimitEventType, RateLimit{TooManyRequests: &rl, URL: urlStr})
  122. time.Sleep(rl.RetryAfter * time.Millisecond)
  123. // we can make the above smarter
  124. // this method can cause longer delays than required
  125. response, err = s.request(method, urlStr, contentType, b, bucketID, sequence)
  126. default: // Error condition
  127. err = newRestError(req, resp, response)
  128. }
  129. return
  130. }
  131. func unmarshal(data []byte, v interface{}) error {
  132. err := json.Unmarshal(data, v)
  133. if err != nil {
  134. return ErrJSONUnmarshal
  135. }
  136. return nil
  137. }
  138. // ------------------------------------------------------------------------------------------------
  139. // Functions specific to Discord Sessions
  140. // ------------------------------------------------------------------------------------------------
  141. // Login asks the Discord server for an authentication token.
  142. func (s *Session) Login(email, password string) (err error) {
  143. data := struct {
  144. Email string `json:"email"`
  145. Password string `json:"password"`
  146. }{email, password}
  147. response, err := s.RequestWithBucketID("POST", EndpointLogin, data, EndpointLogin)
  148. if err != nil {
  149. return
  150. }
  151. temp := struct {
  152. Token string `json:"token"`
  153. MFA bool `json:"mfa"`
  154. }{}
  155. err = unmarshal(response, &temp)
  156. if err != nil {
  157. return
  158. }
  159. s.Token = temp.Token
  160. s.MFA = temp.MFA
  161. return
  162. }
  163. // Register sends a Register request to Discord, and returns the authentication token
  164. // Note that this account is temporary and should be verified for future use.
  165. // Another option is to save the authentication token external, but this isn't recommended.
  166. func (s *Session) Register(username string) (token string, err error) {
  167. data := struct {
  168. Username string `json:"username"`
  169. }{username}
  170. response, err := s.RequestWithBucketID("POST", EndpointRegister, data, EndpointRegister)
  171. if err != nil {
  172. return
  173. }
  174. temp := struct {
  175. Token string `json:"token"`
  176. }{}
  177. err = unmarshal(response, &temp)
  178. if err != nil {
  179. return
  180. }
  181. token = temp.Token
  182. return
  183. }
  184. // Logout sends a logout request to Discord.
  185. // This does not seem to actually invalidate the token. So you can still
  186. // make API calls even after a Logout. So, it seems almost pointless to
  187. // even use.
  188. func (s *Session) Logout() (err error) {
  189. // _, err = s.Request("POST", LOGOUT, fmt.Sprintf(`{"token": "%s"}`, s.Token))
  190. if s.Token == "" {
  191. return
  192. }
  193. data := struct {
  194. Token string `json:"token"`
  195. }{s.Token}
  196. _, err = s.RequestWithBucketID("POST", EndpointLogout, data, EndpointLogout)
  197. return
  198. }
  199. // ------------------------------------------------------------------------------------------------
  200. // Functions specific to Discord Users
  201. // ------------------------------------------------------------------------------------------------
  202. // User returns the user details of the given userID
  203. // userID : A user ID or "@me" which is a shortcut of current user ID
  204. func (s *Session) User(userID string) (st *User, err error) {
  205. body, err := s.RequestWithBucketID("GET", EndpointUser(userID), nil, EndpointUsers)
  206. if err != nil {
  207. return
  208. }
  209. err = unmarshal(body, &st)
  210. return
  211. }
  212. // UserAvatar is deprecated. Please use UserAvatarDecode
  213. // userID : A user ID or "@me" which is a shortcut of current user ID
  214. func (s *Session) UserAvatar(userID string) (img image.Image, err error) {
  215. u, err := s.User(userID)
  216. if err != nil {
  217. return
  218. }
  219. img, err = s.UserAvatarDecode(u)
  220. return
  221. }
  222. // UserAvatarDecode returns an image.Image of a user's Avatar
  223. // user : The user which avatar should be retrieved
  224. func (s *Session) UserAvatarDecode(u *User) (img image.Image, err error) {
  225. body, err := s.RequestWithBucketID("GET", EndpointUserAvatar(u.ID, u.Avatar), nil, EndpointUserAvatar("", ""))
  226. if err != nil {
  227. return
  228. }
  229. img, _, err = image.Decode(bytes.NewReader(body))
  230. return
  231. }
  232. // UserUpdate updates a users settings.
  233. func (s *Session) UserUpdate(email, password, username, avatar, newPassword string) (st *User, err error) {
  234. // NOTE: Avatar must be either the hash/id of existing Avatar or
  235. // data:image/png;base64,BASE64_STRING_OF_NEW_AVATAR_PNG
  236. // to set a new avatar.
  237. // If left blank, avatar will be set to null/blank
  238. data := struct {
  239. Email string `json:"email"`
  240. Password string `json:"password"`
  241. Username string `json:"username,omitempty"`
  242. Avatar string `json:"avatar,omitempty"`
  243. NewPassword string `json:"new_password,omitempty"`
  244. }{email, password, username, avatar, newPassword}
  245. body, err := s.RequestWithBucketID("PATCH", EndpointUser("@me"), data, EndpointUsers)
  246. if err != nil {
  247. return
  248. }
  249. err = unmarshal(body, &st)
  250. return
  251. }
  252. // UserSettings returns the settings for a given user
  253. func (s *Session) UserSettings() (st *Settings, err error) {
  254. body, err := s.RequestWithBucketID("GET", EndpointUserSettings("@me"), nil, EndpointUserSettings(""))
  255. if err != nil {
  256. return
  257. }
  258. err = unmarshal(body, &st)
  259. return
  260. }
  261. // UserUpdateStatus update the user status
  262. // status : The new status (Actual valid status are 'online','idle','dnd','invisible')
  263. func (s *Session) UserUpdateStatus(status Status) (st *Settings, err error) {
  264. if status == StatusOffline {
  265. err = errors.New("You can't set your Status to offline")
  266. return
  267. }
  268. data := struct {
  269. Status Status `json:"status"`
  270. }{status}
  271. body, err := s.RequestWithBucketID("PATCH", EndpointUserSettings("@me"), data, EndpointUserSettings(""))
  272. if err != nil {
  273. return
  274. }
  275. err = unmarshal(body, &st)
  276. return
  277. }
  278. // UserChannels returns an array of Channel structures for all private
  279. // channels.
  280. func (s *Session) UserChannels() (st []*Channel, err error) {
  281. body, err := s.RequestWithBucketID("GET", EndpointUserChannels("@me"), nil, EndpointUserChannels(""))
  282. if err != nil {
  283. return
  284. }
  285. err = unmarshal(body, &st)
  286. return
  287. }
  288. // UserChannelCreate creates a new User (Private) Channel with another User
  289. // recipientID : A user ID for the user to which this channel is opened with.
  290. func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) {
  291. data := struct {
  292. RecipientID string `json:"recipient_id"`
  293. }{recipientID}
  294. body, err := s.RequestWithBucketID("POST", EndpointUserChannels("@me"), data, EndpointUserChannels(""))
  295. if err != nil {
  296. return
  297. }
  298. err = unmarshal(body, &st)
  299. return
  300. }
  301. // UserGuilds returns an array of UserGuild structures for all guilds.
  302. // limit : The number guilds that can be returned. (max 100)
  303. // beforeID : If provided all guilds returned will be before given ID.
  304. // afterID : If provided all guilds returned will be after given ID.
  305. func (s *Session) UserGuilds(limit int, beforeID, afterID string) (st []*UserGuild, err error) {
  306. v := url.Values{}
  307. if limit > 0 {
  308. v.Set("limit", strconv.Itoa(limit))
  309. }
  310. if afterID != "" {
  311. v.Set("after", afterID)
  312. }
  313. if beforeID != "" {
  314. v.Set("before", beforeID)
  315. }
  316. uri := EndpointUserGuilds("@me")
  317. if len(v) > 0 {
  318. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  319. }
  320. body, err := s.RequestWithBucketID("GET", uri, nil, EndpointUserGuilds(""))
  321. if err != nil {
  322. return
  323. }
  324. err = unmarshal(body, &st)
  325. return
  326. }
  327. // UserGuildSettingsEdit Edits the users notification settings for a guild
  328. // guildID : The ID of the guild to edit the settings on
  329. // settings : The settings to update
  330. func (s *Session) UserGuildSettingsEdit(guildID string, settings *UserGuildSettingsEdit) (st *UserGuildSettings, err error) {
  331. body, err := s.RequestWithBucketID("PATCH", EndpointUserGuildSettings("@me", guildID), settings, EndpointUserGuildSettings("", guildID))
  332. if err != nil {
  333. return
  334. }
  335. err = unmarshal(body, &st)
  336. return
  337. }
  338. // UserChannelPermissions returns the permission of a user in a channel.
  339. // userID : The ID of the user to calculate permissions for.
  340. // channelID : The ID of the channel to calculate permission for.
  341. //
  342. // NOTE: This function is now deprecated and will be removed in the future.
  343. // Please see the same function inside state.go
  344. func (s *Session) UserChannelPermissions(userID, channelID string) (apermissions int, err error) {
  345. // Try to just get permissions from state.
  346. apermissions, err = s.State.UserChannelPermissions(userID, channelID)
  347. if err == nil {
  348. return
  349. }
  350. // Otherwise try get as much data from state as possible, falling back to the network.
  351. channel, err := s.State.Channel(channelID)
  352. if err != nil || channel == nil {
  353. channel, err = s.Channel(channelID)
  354. if err != nil {
  355. return
  356. }
  357. }
  358. guild, err := s.State.Guild(channel.GuildID)
  359. if err != nil || guild == nil {
  360. guild, err = s.Guild(channel.GuildID)
  361. if err != nil {
  362. return
  363. }
  364. }
  365. if userID == guild.OwnerID {
  366. apermissions = PermissionAll
  367. return
  368. }
  369. member, err := s.State.Member(guild.ID, userID)
  370. if err != nil || member == nil {
  371. member, err = s.GuildMember(guild.ID, userID)
  372. if err != nil {
  373. return
  374. }
  375. }
  376. return memberPermissions(guild, channel, member), nil
  377. }
  378. // Calculates the permissions for a member.
  379. // https://support.discordapp.com/hc/en-us/articles/206141927-How-is-the-permission-hierarchy-structured-
  380. func memberPermissions(guild *Guild, channel *Channel, member *Member) (apermissions int) {
  381. userID := member.User.ID
  382. if userID == guild.OwnerID {
  383. apermissions = PermissionAll
  384. return
  385. }
  386. for _, role := range guild.Roles {
  387. if role.ID == guild.ID {
  388. apermissions |= role.Permissions
  389. break
  390. }
  391. }
  392. for _, role := range guild.Roles {
  393. for _, roleID := range member.Roles {
  394. if role.ID == roleID {
  395. apermissions |= role.Permissions
  396. break
  397. }
  398. }
  399. }
  400. if apermissions&PermissionAdministrator == PermissionAdministrator {
  401. apermissions |= PermissionAll
  402. }
  403. // Apply @everyone overrides from the channel.
  404. for _, overwrite := range channel.PermissionOverwrites {
  405. if guild.ID == overwrite.ID {
  406. apermissions &= ^overwrite.Deny
  407. apermissions |= overwrite.Allow
  408. break
  409. }
  410. }
  411. denies := 0
  412. allows := 0
  413. // Member overwrites can override role overrides, so do two passes
  414. for _, overwrite := range channel.PermissionOverwrites {
  415. for _, roleID := range member.Roles {
  416. if overwrite.Type == "role" && roleID == overwrite.ID {
  417. denies |= overwrite.Deny
  418. allows |= overwrite.Allow
  419. break
  420. }
  421. }
  422. }
  423. apermissions &= ^denies
  424. apermissions |= allows
  425. for _, overwrite := range channel.PermissionOverwrites {
  426. if overwrite.Type == "member" && overwrite.ID == userID {
  427. apermissions &= ^overwrite.Deny
  428. apermissions |= overwrite.Allow
  429. break
  430. }
  431. }
  432. if apermissions&PermissionAdministrator == PermissionAdministrator {
  433. apermissions |= PermissionAllChannel
  434. }
  435. return apermissions
  436. }
  437. // ------------------------------------------------------------------------------------------------
  438. // Functions specific to Discord Guilds
  439. // ------------------------------------------------------------------------------------------------
  440. // Guild returns a Guild structure of a specific Guild.
  441. // guildID : The ID of a Guild
  442. func (s *Session) Guild(guildID string) (st *Guild, err error) {
  443. if s.StateEnabled {
  444. // Attempt to grab the guild from State first.
  445. st, err = s.State.Guild(guildID)
  446. if err == nil {
  447. return
  448. }
  449. }
  450. body, err := s.RequestWithBucketID("GET", EndpointGuild(guildID), nil, EndpointGuild(guildID))
  451. if err != nil {
  452. return
  453. }
  454. err = unmarshal(body, &st)
  455. return
  456. }
  457. // GuildCreate creates a new Guild
  458. // name : A name for the Guild (2-100 characters)
  459. func (s *Session) GuildCreate(name string) (st *Guild, err error) {
  460. data := struct {
  461. Name string `json:"name"`
  462. }{name}
  463. body, err := s.RequestWithBucketID("POST", EndpointGuilds, data, EndpointGuilds)
  464. if err != nil {
  465. return
  466. }
  467. err = unmarshal(body, &st)
  468. return
  469. }
  470. // GuildEdit edits a new Guild
  471. // guildID : The ID of a Guild
  472. // g : A GuildParams struct with the values Name, Region and VerificationLevel defined.
  473. func (s *Session) GuildEdit(guildID string, g GuildParams) (st *Guild, err error) {
  474. // Bounds checking for VerificationLevel, interval: [0, 3]
  475. if g.VerificationLevel != nil {
  476. val := *g.VerificationLevel
  477. if val < 0 || val > 3 {
  478. err = errors.New("VerificationLevel out of bounds, should be between 0 and 3")
  479. return
  480. }
  481. }
  482. //Bounds checking for regions
  483. if g.Region != "" {
  484. isValid := false
  485. regions, _ := s.VoiceRegions()
  486. for _, r := range regions {
  487. if g.Region == r.ID {
  488. isValid = true
  489. }
  490. }
  491. if !isValid {
  492. var valid []string
  493. for _, r := range regions {
  494. valid = append(valid, r.ID)
  495. }
  496. err = fmt.Errorf("Region not a valid region (%q)", valid)
  497. return
  498. }
  499. }
  500. body, err := s.RequestWithBucketID("PATCH", EndpointGuild(guildID), g, EndpointGuild(guildID))
  501. if err != nil {
  502. return
  503. }
  504. err = unmarshal(body, &st)
  505. return
  506. }
  507. // GuildDelete deletes a Guild.
  508. // guildID : The ID of a Guild
  509. func (s *Session) GuildDelete(guildID string) (st *Guild, err error) {
  510. body, err := s.RequestWithBucketID("DELETE", EndpointGuild(guildID), nil, EndpointGuild(guildID))
  511. if err != nil {
  512. return
  513. }
  514. err = unmarshal(body, &st)
  515. return
  516. }
  517. // GuildLeave leaves a Guild.
  518. // guildID : The ID of a Guild
  519. func (s *Session) GuildLeave(guildID string) (err error) {
  520. _, err = s.RequestWithBucketID("DELETE", EndpointUserGuild("@me", guildID), nil, EndpointUserGuild("", guildID))
  521. return
  522. }
  523. // GuildBans returns an array of User structures for all bans of a
  524. // given guild.
  525. // guildID : The ID of a Guild.
  526. func (s *Session) GuildBans(guildID string) (st []*GuildBan, err error) {
  527. body, err := s.RequestWithBucketID("GET", EndpointGuildBans(guildID), nil, EndpointGuildBans(guildID))
  528. if err != nil {
  529. return
  530. }
  531. err = unmarshal(body, &st)
  532. return
  533. }
  534. // GuildBanCreate bans the given user from the given guild.
  535. // guildID : The ID of a Guild.
  536. // userID : The ID of a User
  537. // days : The number of days of previous comments to delete.
  538. func (s *Session) GuildBanCreate(guildID, userID string, days int) (err error) {
  539. return s.GuildBanCreateWithReason(guildID, userID, "", days)
  540. }
  541. // GuildBanCreateWithReason bans the given user from the given guild also providing a reaso.
  542. // guildID : The ID of a Guild.
  543. // userID : The ID of a User
  544. // reason : The reason for this ban
  545. // days : The number of days of previous comments to delete.
  546. func (s *Session) GuildBanCreateWithReason(guildID, userID, reason string, days int) (err error) {
  547. uri := EndpointGuildBan(guildID, userID)
  548. queryParams := url.Values{}
  549. if days > 0 {
  550. queryParams.Set("delete-message-days", strconv.Itoa(days))
  551. }
  552. if reason != "" {
  553. queryParams.Set("reason", reason)
  554. }
  555. if len(queryParams) > 0 {
  556. uri += "?" + queryParams.Encode()
  557. }
  558. _, err = s.RequestWithBucketID("PUT", uri, nil, EndpointGuildBan(guildID, ""))
  559. return
  560. }
  561. // GuildBanDelete removes the given user from the guild bans
  562. // guildID : The ID of a Guild.
  563. // userID : The ID of a User
  564. func (s *Session) GuildBanDelete(guildID, userID string) (err error) {
  565. _, err = s.RequestWithBucketID("DELETE", EndpointGuildBan(guildID, userID), nil, EndpointGuildBan(guildID, ""))
  566. return
  567. }
  568. // GuildMembers returns a list of members for a guild.
  569. // guildID : The ID of a Guild.
  570. // after : The id of the member to return members after
  571. // limit : max number of members to return (max 1000)
  572. func (s *Session) GuildMembers(guildID string, after string, limit int) (st []*Member, err error) {
  573. uri := EndpointGuildMembers(guildID)
  574. v := url.Values{}
  575. if after != "" {
  576. v.Set("after", after)
  577. }
  578. if limit > 0 {
  579. v.Set("limit", strconv.Itoa(limit))
  580. }
  581. if len(v) > 0 {
  582. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  583. }
  584. body, err := s.RequestWithBucketID("GET", uri, nil, EndpointGuildMembers(guildID))
  585. if err != nil {
  586. return
  587. }
  588. err = unmarshal(body, &st)
  589. return
  590. }
  591. // GuildMember returns a member of a guild.
  592. // guildID : The ID of a Guild.
  593. // userID : The ID of a User
  594. func (s *Session) GuildMember(guildID, userID string) (st *Member, err error) {
  595. body, err := s.RequestWithBucketID("GET", EndpointGuildMember(guildID, userID), nil, EndpointGuildMember(guildID, ""))
  596. if err != nil {
  597. return
  598. }
  599. err = unmarshal(body, &st)
  600. return
  601. }
  602. // GuildMemberDelete removes the given user from the given guild.
  603. // guildID : The ID of a Guild.
  604. // userID : The ID of a User
  605. func (s *Session) GuildMemberDelete(guildID, userID string) (err error) {
  606. _, err = s.RequestWithBucketID("DELETE", EndpointGuildMember(guildID, userID), nil, EndpointGuildMember(guildID, ""))
  607. return
  608. }
  609. // GuildMemberEdit edits the roles of a member.
  610. // guildID : The ID of a Guild.
  611. // userID : The ID of a User.
  612. // roles : A list of role ID's to set on the member.
  613. func (s *Session) GuildMemberEdit(guildID, userID string, roles []string) (err error) {
  614. data := struct {
  615. Roles []string `json:"roles"`
  616. }{roles}
  617. _, err = s.RequestWithBucketID("PATCH", EndpointGuildMember(guildID, userID), data, EndpointGuildMember(guildID, ""))
  618. if err != nil {
  619. return
  620. }
  621. return
  622. }
  623. // GuildMemberMove moves a guild member from one voice channel to another/none
  624. // guildID : The ID of a Guild.
  625. // userID : The ID of a User.
  626. // channelID : The ID of a channel to move user to, or null?
  627. // NOTE : I am not entirely set on the name of this function and it may change
  628. // prior to the final 1.0.0 release of Discordgo
  629. func (s *Session) GuildMemberMove(guildID, userID, channelID string) (err error) {
  630. data := struct {
  631. ChannelID string `json:"channel_id"`
  632. }{channelID}
  633. _, err = s.RequestWithBucketID("PATCH", EndpointGuildMember(guildID, userID), data, EndpointGuildMember(guildID, ""))
  634. if err != nil {
  635. return
  636. }
  637. return
  638. }
  639. // GuildMemberNickname updates the nickname of a guild member
  640. // guildID : The ID of a guild
  641. // userID : The ID of a user
  642. // userID : The ID of a user or "@me" which is a shortcut of the current user ID
  643. func (s *Session) GuildMemberNickname(guildID, userID, nickname string) (err error) {
  644. data := struct {
  645. Nick string `json:"nick"`
  646. }{nickname}
  647. if userID == "@me" {
  648. userID += "/nick"
  649. }
  650. _, err = s.RequestWithBucketID("PATCH", EndpointGuildMember(guildID, userID), data, EndpointGuildMember(guildID, ""))
  651. return
  652. }
  653. // GuildMemberRoleAdd adds the specified role to a given member
  654. // guildID : The ID of a Guild.
  655. // userID : The ID of a User.
  656. // roleID : The ID of a Role to be assigned to the user.
  657. func (s *Session) GuildMemberRoleAdd(guildID, userID, roleID string) (err error) {
  658. _, err = s.RequestWithBucketID("PUT", EndpointGuildMemberRole(guildID, userID, roleID), nil, EndpointGuildMemberRole(guildID, "", ""))
  659. return
  660. }
  661. // GuildMemberRoleRemove removes the specified role to a given member
  662. // guildID : The ID of a Guild.
  663. // userID : The ID of a User.
  664. // roleID : The ID of a Role to be removed from the user.
  665. func (s *Session) GuildMemberRoleRemove(guildID, userID, roleID string) (err error) {
  666. _, err = s.RequestWithBucketID("DELETE", EndpointGuildMemberRole(guildID, userID, roleID), nil, EndpointGuildMemberRole(guildID, "", ""))
  667. return
  668. }
  669. // GuildChannels returns an array of Channel structures for all channels of a
  670. // given guild.
  671. // guildID : The ID of a Guild.
  672. func (s *Session) GuildChannels(guildID string) (st []*Channel, err error) {
  673. body, err := s.request("GET", EndpointGuildChannels(guildID), "", nil, EndpointGuildChannels(guildID), 0)
  674. if err != nil {
  675. return
  676. }
  677. err = unmarshal(body, &st)
  678. return
  679. }
  680. // GuildChannelCreate creates a new channel in the given guild
  681. // guildID : The ID of a Guild.
  682. // name : Name of the channel (2-100 chars length)
  683. // ctype : Tpye of the channel (voice or text)
  684. func (s *Session) GuildChannelCreate(guildID, name, ctype string) (st *Channel, err error) {
  685. data := struct {
  686. Name string `json:"name"`
  687. Type string `json:"type"`
  688. }{name, ctype}
  689. body, err := s.RequestWithBucketID("POST", EndpointGuildChannels(guildID), data, EndpointGuildChannels(guildID))
  690. if err != nil {
  691. return
  692. }
  693. err = unmarshal(body, &st)
  694. return
  695. }
  696. // GuildChannelsReorder updates the order of channels in a guild
  697. // guildID : The ID of a Guild.
  698. // channels : Updated channels.
  699. func (s *Session) GuildChannelsReorder(guildID string, channels []*Channel) (err error) {
  700. _, err = s.RequestWithBucketID("PATCH", EndpointGuildChannels(guildID), channels, EndpointGuildChannels(guildID))
  701. return
  702. }
  703. // GuildInvites returns an array of Invite structures for the given guild
  704. // guildID : The ID of a Guild.
  705. func (s *Session) GuildInvites(guildID string) (st []*Invite, err error) {
  706. body, err := s.RequestWithBucketID("GET", EndpointGuildInvites(guildID), nil, EndpointGuildInivtes(guildID))
  707. if err != nil {
  708. return
  709. }
  710. err = unmarshal(body, &st)
  711. return
  712. }
  713. // GuildRoles returns all roles for a given guild.
  714. // guildID : The ID of a Guild.
  715. func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
  716. body, err := s.RequestWithBucketID("GET", EndpointGuildRoles(guildID), nil, EndpointGuildRoles(guildID))
  717. if err != nil {
  718. return
  719. }
  720. err = unmarshal(body, &st)
  721. return // TODO return pointer
  722. }
  723. // GuildRoleCreate returns a new Guild Role.
  724. // guildID: The ID of a Guild.
  725. func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
  726. body, err := s.RequestWithBucketID("POST", EndpointGuildRoles(guildID), nil, EndpointGuildRoles(guildID))
  727. if err != nil {
  728. return
  729. }
  730. err = unmarshal(body, &st)
  731. return
  732. }
  733. // GuildRoleEdit updates an existing Guild Role with new values
  734. // guildID : The ID of a Guild.
  735. // roleID : The ID of a Role.
  736. // name : The name of the Role.
  737. // color : The color of the role (decimal, not hex).
  738. // hoist : Whether to display the role's users separately.
  739. // perm : The permissions for the role.
  740. // mention : Whether this role is mentionable
  741. func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int, mention bool) (st *Role, err error) {
  742. // Prevent sending a color int that is too big.
  743. if color > 0xFFFFFF {
  744. err = fmt.Errorf("color value cannot be larger than 0xFFFFFF")
  745. }
  746. data := struct {
  747. Name string `json:"name"` // The role's name (overwrites existing)
  748. Color int `json:"color"` // The color the role should have (as a decimal, not hex)
  749. Hoist bool `json:"hoist"` // Whether to display the role's users separately
  750. Permissions int `json:"permissions"` // The overall permissions number of the role (overwrites existing)
  751. Mentionable bool `json:"mentionable"` // Whether this role is mentionable
  752. }{name, color, hoist, perm, mention}
  753. body, err := s.RequestWithBucketID("PATCH", EndpointGuildRole(guildID, roleID), data, EndpointGuildRole(guildID, ""))
  754. if err != nil {
  755. return
  756. }
  757. err = unmarshal(body, &st)
  758. return
  759. }
  760. // GuildRoleReorder reoders guild roles
  761. // guildID : The ID of a Guild.
  762. // roles : A list of ordered roles.
  763. func (s *Session) GuildRoleReorder(guildID string, roles []*Role) (st []*Role, err error) {
  764. body, err := s.RequestWithBucketID("PATCH", EndpointGuildRoles(guildID), roles, EndpointGuildRoles(guildID))
  765. if err != nil {
  766. return
  767. }
  768. err = unmarshal(body, &st)
  769. return
  770. }
  771. // GuildRoleDelete deletes an existing role.
  772. // guildID : The ID of a Guild.
  773. // roleID : The ID of a Role.
  774. func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) {
  775. _, err = s.RequestWithBucketID("DELETE", EndpointGuildRole(guildID, roleID), nil, EndpointGuildRole(guildID, ""))
  776. return
  777. }
  778. // GuildPruneCount Returns the number of members that would be removed in a prune operation.
  779. // Requires 'KICK_MEMBER' permission.
  780. // guildID : The ID of a Guild.
  781. // days : The number of days to count prune for (1 or more).
  782. func (s *Session) GuildPruneCount(guildID string, days uint32) (count uint32, err error) {
  783. count = 0
  784. if days <= 0 {
  785. err = errors.New("the number of days should be more than or equal to 1")
  786. return
  787. }
  788. p := struct {
  789. Pruned uint32 `json:"pruned"`
  790. }{}
  791. uri := EndpointGuildPrune(guildID) + fmt.Sprintf("?days=%d", days)
  792. body, err := s.RequestWithBucketID("GET", uri, nil, EndpointGuildPrune(guildID))
  793. err = unmarshal(body, &p)
  794. if err != nil {
  795. return
  796. }
  797. count = p.Pruned
  798. return
  799. }
  800. // GuildPrune Begin as prune operation. Requires the 'KICK_MEMBERS' permission.
  801. // Returns an object with one 'pruned' key indicating the number of members that were removed in the prune operation.
  802. // guildID : The ID of a Guild.
  803. // days : The number of days to count prune for (1 or more).
  804. func (s *Session) GuildPrune(guildID string, days uint32) (count uint32, err error) {
  805. count = 0
  806. if days <= 0 {
  807. err = errors.New("the number of days should be more than or equal to 1")
  808. return
  809. }
  810. data := struct {
  811. days uint32
  812. }{days}
  813. p := struct {
  814. Pruned uint32 `json:"pruned"`
  815. }{}
  816. body, err := s.RequestWithBucketID("POST", EndpointGuildPrune(guildID), data, EndpointGuildPrune(guildID))
  817. if err != nil {
  818. return
  819. }
  820. err = unmarshal(body, &p)
  821. if err != nil {
  822. return
  823. }
  824. count = p.Pruned
  825. return
  826. }
  827. // GuildIntegrations returns an array of Integrations for a guild.
  828. // guildID : The ID of a Guild.
  829. func (s *Session) GuildIntegrations(guildID string) (st []*GuildIntegration, err error) {
  830. body, err := s.RequestWithBucketID("GET", EndpointGuildIntegrations(guildID), nil, EndpointGuildIntegrations(guildID))
  831. if err != nil {
  832. return
  833. }
  834. err = unmarshal(body, &st)
  835. return
  836. }
  837. // GuildIntegrationCreate creates a Guild Integration.
  838. // guildID : The ID of a Guild.
  839. // integrationType : The Integration type.
  840. // integrationID : The ID of an integration.
  841. func (s *Session) GuildIntegrationCreate(guildID, integrationType, integrationID string) (err error) {
  842. data := struct {
  843. Type string `json:"type"`
  844. ID string `json:"id"`
  845. }{integrationType, integrationID}
  846. _, err = s.RequestWithBucketID("POST", EndpointGuildIntegrations(guildID), data, EndpointGuildIntegrations(guildID))
  847. return
  848. }
  849. // GuildIntegrationEdit edits a Guild Integration.
  850. // guildID : The ID of a Guild.
  851. // integrationType : The Integration type.
  852. // integrationID : The ID of an integration.
  853. // expireBehavior : The behavior when an integration subscription lapses (see the integration object documentation).
  854. // expireGracePeriod : Period (in seconds) where the integration will ignore lapsed subscriptions.
  855. // enableEmoticons : Whether emoticons should be synced for this integration (twitch only currently).
  856. func (s *Session) GuildIntegrationEdit(guildID, integrationID string, expireBehavior, expireGracePeriod int, enableEmoticons bool) (err error) {
  857. data := struct {
  858. ExpireBehavior int `json:"expire_behavior"`
  859. ExpireGracePeriod int `json:"expire_grace_period"`
  860. EnableEmoticons bool `json:"enable_emoticons"`
  861. }{expireBehavior, expireGracePeriod, enableEmoticons}
  862. _, err = s.RequestWithBucketID("PATCH", EndpointGuildIntegration(guildID, integrationID), data, EndpointGuildIntegration(guildID, ""))
  863. return
  864. }
  865. // GuildIntegrationDelete removes the given integration from the Guild.
  866. // guildID : The ID of a Guild.
  867. // integrationID : The ID of an integration.
  868. func (s *Session) GuildIntegrationDelete(guildID, integrationID string) (err error) {
  869. _, err = s.RequestWithBucketID("DELETE", EndpointGuildIntegration(guildID, integrationID), nil, EndpointGuildIntegration(guildID, ""))
  870. return
  871. }
  872. // GuildIntegrationSync syncs an integration.
  873. // guildID : The ID of a Guild.
  874. // integrationID : The ID of an integration.
  875. func (s *Session) GuildIntegrationSync(guildID, integrationID string) (err error) {
  876. _, err = s.RequestWithBucketID("POST", EndpointGuildIntegrationSync(guildID, integrationID), nil, EndpointGuildIntegration(guildID, ""))
  877. return
  878. }
  879. // GuildIcon returns an image.Image of a guild icon.
  880. // guildID : The ID of a Guild.
  881. func (s *Session) GuildIcon(guildID string) (img image.Image, err error) {
  882. g, err := s.Guild(guildID)
  883. if err != nil {
  884. return
  885. }
  886. if g.Icon == "" {
  887. err = errors.New("guild does not have an icon set")
  888. return
  889. }
  890. body, err := s.RequestWithBucketID("GET", EndpointGuildIcon(guildID, g.Icon), nil, EndpointGuildIcon(guildID, ""))
  891. if err != nil {
  892. return
  893. }
  894. img, _, err = image.Decode(bytes.NewReader(body))
  895. return
  896. }
  897. // GuildSplash returns an image.Image of a guild splash image.
  898. // guildID : The ID of a Guild.
  899. func (s *Session) GuildSplash(guildID string) (img image.Image, err error) {
  900. g, err := s.Guild(guildID)
  901. if err != nil {
  902. return
  903. }
  904. if g.Splash == "" {
  905. err = errors.New("guild does not have a splash set")
  906. return
  907. }
  908. body, err := s.RequestWithBucketID("GET", EndpointGuildSplash(guildID, g.Splash), nil, EndpointGuildSplash(guildID, ""))
  909. if err != nil {
  910. return
  911. }
  912. img, _, err = image.Decode(bytes.NewReader(body))
  913. return
  914. }
  915. // GuildEmbed returns the embed for a Guild.
  916. // guildID : The ID of a Guild.
  917. func (s *Session) GuildEmbed(guildID string) (st *GuildEmbed, err error) {
  918. body, err := s.RequestWithBucketID("GET", EndpointGuildEmbed(guildID), nil, EndpointGuildEmbed(guildID))
  919. if err != nil {
  920. return
  921. }
  922. err = unmarshal(body, &st)
  923. return
  924. }
  925. // GuildEmbedEdit returns the embed for a Guild.
  926. // guildID : The ID of a Guild.
  927. func (s *Session) GuildEmbedEdit(guildID string, enabled bool, channelID string) (err error) {
  928. data := GuildEmbed{enabled, channelID}
  929. _, err = s.RequestWithBucketID("PATCH", EndpointGuildEmbed(guildID), data, EndpointGuildEmbed(guildID))
  930. return
  931. }
  932. // ------------------------------------------------------------------------------------------------
  933. // Functions specific to Discord Channels
  934. // ------------------------------------------------------------------------------------------------
  935. // Channel returns a Channel strucutre of a specific Channel.
  936. // channelID : The ID of the Channel you want returned.
  937. func (s *Session) Channel(channelID string) (st *Channel, err error) {
  938. body, err := s.RequestWithBucketID("GET", EndpointChannel(channelID), nil, EndpointChannel(channelID))
  939. if err != nil {
  940. return
  941. }
  942. err = unmarshal(body, &st)
  943. return
  944. }
  945. // ChannelEdit edits the given channel
  946. // channelID : The ID of a Channel
  947. // name : The new name to assign the channel.
  948. func (s *Session) ChannelEdit(channelID, name string) (st *Channel, err error) {
  949. data := struct {
  950. Name string `json:"name"`
  951. }{name}
  952. body, err := s.RequestWithBucketID("PATCH", EndpointChannel(channelID), data, EndpointChannel(channelID))
  953. if err != nil {
  954. return
  955. }
  956. err = unmarshal(body, &st)
  957. return
  958. }
  959. // ChannelDelete deletes the given channel
  960. // channelID : The ID of a Channel
  961. func (s *Session) ChannelDelete(channelID string) (st *Channel, err error) {
  962. body, err := s.RequestWithBucketID("DELETE", EndpointChannel(channelID), nil, EndpointChannel(channelID))
  963. if err != nil {
  964. return
  965. }
  966. err = unmarshal(body, &st)
  967. return
  968. }
  969. // ChannelTyping broadcasts to all members that authenticated user is typing in
  970. // the given channel.
  971. // channelID : The ID of a Channel
  972. func (s *Session) ChannelTyping(channelID string) (err error) {
  973. _, err = s.RequestWithBucketID("POST", EndpointChannelTyping(channelID), nil, EndpointChannelTyping(channelID))
  974. return
  975. }
  976. // ChannelMessages returns an array of Message structures for messages within
  977. // a given channel.
  978. // channelID : The ID of a Channel.
  979. // limit : The number messages that can be returned. (max 100)
  980. // beforeID : If provided all messages returned will be before given ID.
  981. // afterID : If provided all messages returned will be after given ID.
  982. // aroundID : If provided all messages returned will be around given ID.
  983. func (s *Session) ChannelMessages(channelID string, limit int, beforeID, afterID, aroundID string) (st []*Message, err error) {
  984. uri := EndpointChannelMessages(channelID)
  985. v := url.Values{}
  986. if limit > 0 {
  987. v.Set("limit", strconv.Itoa(limit))
  988. }
  989. if afterID != "" {
  990. v.Set("after", afterID)
  991. }
  992. if beforeID != "" {
  993. v.Set("before", beforeID)
  994. }
  995. if aroundID != "" {
  996. v.Set("around", aroundID)
  997. }
  998. if len(v) > 0 {
  999. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  1000. }
  1001. body, err := s.RequestWithBucketID("GET", uri, nil, EndpointChannelMessages(channelID))
  1002. if err != nil {
  1003. return
  1004. }
  1005. err = unmarshal(body, &st)
  1006. return
  1007. }
  1008. // ChannelMessage gets a single message by ID from a given channel.
  1009. // channeld : The ID of a Channel
  1010. // messageID : the ID of a Message
  1011. func (s *Session) ChannelMessage(channelID, messageID string) (st *Message, err error) {
  1012. response, err := s.RequestWithBucketID("GET", EndpointChannelMessage(channelID, messageID), nil, EndpointChannelMessage(channelID, ""))
  1013. if err != nil {
  1014. return
  1015. }
  1016. err = unmarshal(response, &st)
  1017. return
  1018. }
  1019. // ChannelMessageAck acknowledges and marks the given message as read
  1020. // channeld : The ID of a Channel
  1021. // messageID : the ID of a Message
  1022. // lastToken : token returned by last ack
  1023. func (s *Session) ChannelMessageAck(channelID, messageID, lastToken string) (st *Ack, err error) {
  1024. body, err := s.RequestWithBucketID("POST", EndpointChannelMessageAck(channelID, messageID), &Ack{Token: lastToken}, EndpointChannelMessageAck(channelID, ""))
  1025. if err != nil {
  1026. return
  1027. }
  1028. err = unmarshal(body, &st)
  1029. return
  1030. }
  1031. // ChannelMessageSend sends a message to the given channel.
  1032. // channelID : The ID of a Channel.
  1033. // content : The message to send.
  1034. func (s *Session) ChannelMessageSend(channelID string, content string) (*Message, error) {
  1035. return s.ChannelMessageSendComplex(channelID, &MessageSend{
  1036. Content: content,
  1037. })
  1038. }
  1039. // ChannelMessageSendComplex sends a message to the given channel.
  1040. // channelID : The ID of a Channel.
  1041. // data : The message struct to send.
  1042. func (s *Session) ChannelMessageSendComplex(channelID string, data *MessageSend) (st *Message, err error) {
  1043. if data.Embed != nil && data.Embed.Type == "" {
  1044. data.Embed.Type = "rich"
  1045. }
  1046. endpoint := EndpointChannelMessages(channelID)
  1047. var response []byte
  1048. if data.File != nil {
  1049. body := &bytes.Buffer{}
  1050. bodywriter := multipart.NewWriter(body)
  1051. // What's a better way of doing this? Reflect? Generator? I'm open to suggestions
  1052. if data.Content != "" {
  1053. if err = bodywriter.WriteField("content", data.Content); err != nil {
  1054. return
  1055. }
  1056. }
  1057. if data.Embed != nil {
  1058. var embed []byte
  1059. embed, err = json.Marshal(data.Embed)
  1060. if err != nil {
  1061. return
  1062. }
  1063. err = bodywriter.WriteField("embed", string(embed))
  1064. if err != nil {
  1065. return
  1066. }
  1067. }
  1068. if data.Tts {
  1069. if err = bodywriter.WriteField("tts", "true"); err != nil {
  1070. return
  1071. }
  1072. }
  1073. var writer io.Writer
  1074. writer, err = bodywriter.CreateFormFile("file", data.File.Name)
  1075. if err != nil {
  1076. return
  1077. }
  1078. _, err = io.Copy(writer, data.File.Reader)
  1079. if err != nil {
  1080. return
  1081. }
  1082. err = bodywriter.Close()
  1083. if err != nil {
  1084. return
  1085. }
  1086. response, err = s.request("POST", endpoint, bodywriter.FormDataContentType(), body.Bytes(), endpoint, 0)
  1087. } else {
  1088. response, err = s.RequestWithBucketID("POST", endpoint, data, endpoint)
  1089. }
  1090. if err != nil {
  1091. return
  1092. }
  1093. err = unmarshal(response, &st)
  1094. return
  1095. }
  1096. // ChannelMessageSendTTS sends a message to the given channel with Text to Speech.
  1097. // channelID : The ID of a Channel.
  1098. // content : The message to send.
  1099. func (s *Session) ChannelMessageSendTTS(channelID string, content string) (*Message, error) {
  1100. return s.ChannelMessageSendComplex(channelID, &MessageSend{
  1101. Content: content,
  1102. Tts: true,
  1103. })
  1104. }
  1105. // ChannelMessageSendEmbed sends a message to the given channel with embedded data.
  1106. // channelID : The ID of a Channel.
  1107. // embed : The embed data to send.
  1108. func (s *Session) ChannelMessageSendEmbed(channelID string, embed *MessageEmbed) (*Message, error) {
  1109. return s.ChannelMessageSendComplex(channelID, &MessageSend{
  1110. Embed: embed,
  1111. })
  1112. }
  1113. // ChannelMessageEdit edits an existing message, replacing it entirely with
  1114. // the given content.
  1115. // channelID : The ID of a Channel
  1116. // messageID : The ID of a Message
  1117. // content : The contents of the message
  1118. func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (*Message, error) {
  1119. return s.ChannelMessageEditComplex(NewMessageEdit(channelID, messageID).SetContent(content))
  1120. }
  1121. // ChannelMessageEditComplex edits an existing message, replacing it entirely with
  1122. // the given MessageEdit struct
  1123. func (s *Session) ChannelMessageEditComplex(m *MessageEdit) (st *Message, err error) {
  1124. if m.Embed != nil && m.Embed.Type == "" {
  1125. m.Embed.Type = "rich"
  1126. }
  1127. response, err := s.RequestWithBucketID("PATCH", EndpointChannelMessage(m.Channel, m.ID), m, EndpointChannelMessage(m.Channel, ""))
  1128. if err != nil {
  1129. return
  1130. }
  1131. err = unmarshal(response, &st)
  1132. return
  1133. }
  1134. // ChannelMessageEditEmbed edits an existing message with embedded data.
  1135. // channelID : The ID of a Channel
  1136. // messageID : The ID of a Message
  1137. // embed : The embed data to send
  1138. func (s *Session) ChannelMessageEditEmbed(channelID, messageID string, embed *MessageEmbed) (*Message, error) {
  1139. return s.ChannelMessageEditComplex(NewMessageEdit(channelID, messageID).SetEmbed(embed))
  1140. }
  1141. // ChannelMessageDelete deletes a message from the Channel.
  1142. func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) {
  1143. _, err = s.RequestWithBucketID("DELETE", EndpointChannelMessage(channelID, messageID), nil, EndpointChannelMessage(channelID, ""))
  1144. return
  1145. }
  1146. // ChannelMessagesBulkDelete bulk deletes the messages from the channel for the provided messageIDs.
  1147. // If only one messageID is in the slice call channelMessageDelete funciton.
  1148. // If the slice is empty do nothing.
  1149. // channelID : The ID of the channel for the messages to delete.
  1150. // messages : The IDs of the messages to be deleted. A slice of string IDs. A maximum of 100 messages.
  1151. func (s *Session) ChannelMessagesBulkDelete(channelID string, messages []string) (err error) {
  1152. if len(messages) == 0 {
  1153. return
  1154. }
  1155. if len(messages) == 1 {
  1156. err = s.ChannelMessageDelete(channelID, messages[0])
  1157. return
  1158. }
  1159. if len(messages) > 100 {
  1160. messages = messages[:100]
  1161. }
  1162. data := struct {
  1163. Messages []string `json:"messages"`
  1164. }{messages}
  1165. _, err = s.RequestWithBucketID("POST", EndpointChannelMessagesBulkDelete(channelID), data, EndpointChannelMessagesBulkDelete(channelID))
  1166. return
  1167. }
  1168. // ChannelMessagePin pins a message within a given channel.
  1169. // channelID: The ID of a channel.
  1170. // messageID: The ID of a message.
  1171. func (s *Session) ChannelMessagePin(channelID, messageID string) (err error) {
  1172. _, err = s.RequestWithBucketID("PUT", EndpointChannelMessagePin(channelID, messageID), nil, EndpointChannelMessagePin(channelID, ""))
  1173. return
  1174. }
  1175. // ChannelMessageUnpin unpins a message within a given channel.
  1176. // channelID: The ID of a channel.
  1177. // messageID: The ID of a message.
  1178. func (s *Session) ChannelMessageUnpin(channelID, messageID string) (err error) {
  1179. _, err = s.RequestWithBucketID("DELETE", EndpointChannelMessagePin(channelID, messageID), nil, EndpointChannelMessagePin(channelID, ""))
  1180. return
  1181. }
  1182. // ChannelMessagesPinned returns an array of Message structures for pinned messages
  1183. // within a given channel
  1184. // channelID : The ID of a Channel.
  1185. func (s *Session) ChannelMessagesPinned(channelID string) (st []*Message, err error) {
  1186. body, err := s.RequestWithBucketID("GET", EndpointChannelMessagesPins(channelID), nil, EndpointChannelMessagesPins(channelID))
  1187. if err != nil {
  1188. return
  1189. }
  1190. err = unmarshal(body, &st)
  1191. return
  1192. }
  1193. // ChannelFileSend sends a file to the given channel.
  1194. // channelID : The ID of a Channel.
  1195. // name: The name of the file.
  1196. // io.Reader : A reader for the file contents.
  1197. func (s *Session) ChannelFileSend(channelID, name string, r io.Reader) (*Message, error) {
  1198. return s.ChannelMessageSendComplex(channelID, &MessageSend{File: &File{Name: name, Reader: r}})
  1199. }
  1200. // ChannelFileSendWithMessage sends a file to the given channel with an message.
  1201. // DEPRECATED. Use ChannelMessageSendComplex instead.
  1202. // channelID : The ID of a Channel.
  1203. // content: Optional Message content.
  1204. // name: The name of the file.
  1205. // io.Reader : A reader for the file contents.
  1206. func (s *Session) ChannelFileSendWithMessage(channelID, content string, name string, r io.Reader) (*Message, error) {
  1207. return s.ChannelMessageSendComplex(channelID, &MessageSend{File: &File{Name: name, Reader: r}, Content: content})
  1208. }
  1209. // ChannelInvites returns an array of Invite structures for the given channel
  1210. // channelID : The ID of a Channel
  1211. func (s *Session) ChannelInvites(channelID string) (st []*Invite, err error) {
  1212. body, err := s.RequestWithBucketID("GET", EndpointChannelInvites(channelID), nil, EndpointChannelInvites(channelID))
  1213. if err != nil {
  1214. return
  1215. }
  1216. err = unmarshal(body, &st)
  1217. return
  1218. }
  1219. // ChannelInviteCreate creates a new invite for the given channel.
  1220. // channelID : The ID of a Channel
  1221. // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
  1222. // and XkcdPass defined.
  1223. func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st *Invite, err error) {
  1224. data := struct {
  1225. MaxAge int `json:"max_age"`
  1226. MaxUses int `json:"max_uses"`
  1227. Temporary bool `json:"temporary"`
  1228. XKCDPass string `json:"xkcdpass"`
  1229. }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
  1230. body, err := s.RequestWithBucketID("POST", EndpointChannelInvites(channelID), data, EndpointChannelInvites(channelID))
  1231. if err != nil {
  1232. return
  1233. }
  1234. err = unmarshal(body, &st)
  1235. return
  1236. }
  1237. // ChannelPermissionSet creates a Permission Override for the given channel.
  1238. // NOTE: This func name may changed. Using Set instead of Create because
  1239. // you can both create a new override or update an override with this function.
  1240. func (s *Session) ChannelPermissionSet(channelID, targetID, targetType string, allow, deny int) (err error) {
  1241. data := struct {
  1242. ID string `json:"id"`
  1243. Type string `json:"type"`
  1244. Allow int `json:"allow"`
  1245. Deny int `json:"deny"`
  1246. }{targetID, targetType, allow, deny}
  1247. _, err = s.RequestWithBucketID("PUT", EndpointChannelPermission(channelID, targetID), data, EndpointChannelPermission(channelID, ""))
  1248. return
  1249. }
  1250. // ChannelPermissionDelete deletes a specific permission override for the given channel.
  1251. // NOTE: Name of this func may change.
  1252. func (s *Session) ChannelPermissionDelete(channelID, targetID string) (err error) {
  1253. _, err = s.RequestWithBucketID("DELETE", EndpointChannelPermission(channelID, targetID), nil, EndpointChannelPermission(channelID, ""))
  1254. return
  1255. }
  1256. // ------------------------------------------------------------------------------------------------
  1257. // Functions specific to Discord Invites
  1258. // ------------------------------------------------------------------------------------------------
  1259. // Invite returns an Invite structure of the given invite
  1260. // inviteID : The invite code (or maybe xkcdpass?)
  1261. func (s *Session) Invite(inviteID string) (st *Invite, err error) {
  1262. body, err := s.RequestWithBucketID("GET", EndpointInvite(inviteID), nil, EndpointInvite(""))
  1263. if err != nil {
  1264. return
  1265. }
  1266. err = unmarshal(body, &st)
  1267. return
  1268. }
  1269. // InviteDelete deletes an existing invite
  1270. // inviteID : the code (or maybe xkcdpass?) of an invite
  1271. func (s *Session) InviteDelete(inviteID string) (st *Invite, err error) {
  1272. body, err := s.RequestWithBucketID("DELETE", EndpointInvite(inviteID), nil, EndpointInvite(""))
  1273. if err != nil {
  1274. return
  1275. }
  1276. err = unmarshal(body, &st)
  1277. return
  1278. }
  1279. // InviteAccept accepts an Invite to a Guild or Channel
  1280. // inviteID : The invite code (or maybe xkcdpass?)
  1281. func (s *Session) InviteAccept(inviteID string) (st *Invite, err error) {
  1282. body, err := s.RequestWithBucketID("POST", EndpointInvite(inviteID), nil, EndpointInvite(""))
  1283. if err != nil {
  1284. return
  1285. }
  1286. err = unmarshal(body, &st)
  1287. return
  1288. }
  1289. // ------------------------------------------------------------------------------------------------
  1290. // Functions specific to Discord Voice
  1291. // ------------------------------------------------------------------------------------------------
  1292. // VoiceRegions returns the voice server regions
  1293. func (s *Session) VoiceRegions() (st []*VoiceRegion, err error) {
  1294. body, err := s.RequestWithBucketID("GET", EndpointVoiceRegions, nil, EndpointVoiceRegions)
  1295. if err != nil {
  1296. return
  1297. }
  1298. err = unmarshal(body, &st)
  1299. return
  1300. }
  1301. // VoiceICE returns the voice server ICE information
  1302. func (s *Session) VoiceICE() (st *VoiceICE, err error) {
  1303. body, err := s.RequestWithBucketID("GET", EndpointVoiceIce, nil, EndpointVoiceIce)
  1304. if err != nil {
  1305. return
  1306. }
  1307. err = unmarshal(body, &st)
  1308. return
  1309. }
  1310. // ------------------------------------------------------------------------------------------------
  1311. // Functions specific to Discord Websockets
  1312. // ------------------------------------------------------------------------------------------------
  1313. // Gateway returns the websocket Gateway address
  1314. func (s *Session) Gateway() (gateway string, err error) {
  1315. response, err := s.RequestWithBucketID("GET", EndpointGateway, nil, EndpointGateway)
  1316. if err != nil {
  1317. return
  1318. }
  1319. temp := struct {
  1320. URL string `json:"url"`
  1321. }{}
  1322. err = unmarshal(response, &temp)
  1323. if err != nil {
  1324. return
  1325. }
  1326. gateway = temp.URL
  1327. // Ensure the gateway always has a trailing slash.
  1328. // MacOS will fail to connect if we add query params without a trailing slash on the base domain.
  1329. if !strings.HasSuffix(gateway, "/") {
  1330. gateway += "/"
  1331. }
  1332. return
  1333. }
  1334. // Functions specific to Webhooks
  1335. // WebhookCreate returns a new Webhook.
  1336. // channelID: The ID of a Channel.
  1337. // name : The name of the webhook.
  1338. // avatar : The avatar of the webhook.
  1339. func (s *Session) WebhookCreate(channelID, name, avatar string) (st *Webhook, err error) {
  1340. data := struct {
  1341. Name string `json:"name"`
  1342. Avatar string `json:"avatar,omitempty"`
  1343. }{name, avatar}
  1344. body, err := s.RequestWithBucketID("POST", EndpointChannelWebhooks(channelID), data, EndpointChannelWebhooks(channelID))
  1345. if err != nil {
  1346. return
  1347. }
  1348. err = unmarshal(body, &st)
  1349. return
  1350. }
  1351. // ChannelWebhooks returns all webhooks for a given channel.
  1352. // channelID: The ID of a channel.
  1353. func (s *Session) ChannelWebhooks(channelID string) (st []*Webhook, err error) {
  1354. body, err := s.RequestWithBucketID("GET", EndpointChannelWebhooks(channelID), nil, EndpointChannelWebhooks(channelID))
  1355. if err != nil {
  1356. return
  1357. }
  1358. err = unmarshal(body, &st)
  1359. return
  1360. }
  1361. // GuildWebhooks returns all webhooks for a given guild.
  1362. // guildID: The ID of a Guild.
  1363. func (s *Session) GuildWebhooks(guildID string) (st []*Webhook, err error) {
  1364. body, err := s.RequestWithBucketID("GET", EndpointGuildWebhooks(guildID), nil, EndpointGuildWebhooks(guildID))
  1365. if err != nil {
  1366. return
  1367. }
  1368. err = unmarshal(body, &st)
  1369. return
  1370. }
  1371. // Webhook returns a webhook for a given ID
  1372. // webhookID: The ID of a webhook.
  1373. func (s *Session) Webhook(webhookID string) (st *Webhook, err error) {
  1374. body, err := s.RequestWithBucketID("GET", EndpointWebhook(webhookID), nil, EndpointWebhooks)
  1375. if err != nil {
  1376. return
  1377. }
  1378. err = unmarshal(body, &st)
  1379. return
  1380. }
  1381. // WebhookWithToken returns a webhook for a given ID
  1382. // webhookID: The ID of a webhook.
  1383. // token : The auth token for the webhook.
  1384. func (s *Session) WebhookWithToken(webhookID, token string) (st *Webhook, err error) {
  1385. body, err := s.RequestWithBucketID("GET", EndpointWebhookToken(webhookID, token), nil, EndpointWebhookToken("", ""))
  1386. if err != nil {
  1387. return
  1388. }
  1389. err = unmarshal(body, &st)
  1390. return
  1391. }
  1392. // WebhookEdit updates an existing Webhook.
  1393. // webhookID: The ID of a webhook.
  1394. // name : The name of the webhook.
  1395. // avatar : The avatar of the webhook.
  1396. func (s *Session) WebhookEdit(webhookID, name, avatar string) (st *Role, err error) {
  1397. data := struct {
  1398. Name string `json:"name,omitempty"`
  1399. Avatar string `json:"avatar,omitempty"`
  1400. }{name, avatar}
  1401. body, err := s.RequestWithBucketID("PATCH", EndpointWebhook(webhookID), data, EndpointWebhooks)
  1402. if err != nil {
  1403. return
  1404. }
  1405. err = unmarshal(body, &st)
  1406. return
  1407. }
  1408. // WebhookEditWithToken updates an existing Webhook with an auth token.
  1409. // webhookID: The ID of a webhook.
  1410. // token : The auth token for the webhook.
  1411. // name : The name of the webhook.
  1412. // avatar : The avatar of the webhook.
  1413. func (s *Session) WebhookEditWithToken(webhookID, token, name, avatar string) (st *Role, err error) {
  1414. data := struct {
  1415. Name string `json:"name,omitempty"`
  1416. Avatar string `json:"avatar,omitempty"`
  1417. }{name, avatar}
  1418. body, err := s.RequestWithBucketID("PATCH", EndpointWebhookToken(webhookID, token), data, EndpointWebhookToken("", ""))
  1419. if err != nil {
  1420. return
  1421. }
  1422. err = unmarshal(body, &st)
  1423. return
  1424. }
  1425. // WebhookDelete deletes a webhook for a given ID
  1426. // webhookID: The ID of a webhook.
  1427. func (s *Session) WebhookDelete(webhookID string) (st *Webhook, err error) {
  1428. body, err := s.RequestWithBucketID("DELETE", EndpointWebhook(webhookID), nil, EndpointWebhooks)
  1429. if err != nil {
  1430. return
  1431. }
  1432. err = unmarshal(body, &st)
  1433. return
  1434. }
  1435. // WebhookDeleteWithToken deletes a webhook for a given ID with an auth token.
  1436. // webhookID: The ID of a webhook.
  1437. // token : The auth token for the webhook.
  1438. func (s *Session) WebhookDeleteWithToken(webhookID, token string) (st *Webhook, err error) {
  1439. body, err := s.RequestWithBucketID("DELETE", EndpointWebhookToken(webhookID, token), nil, EndpointWebhookToken("", ""))
  1440. if err != nil {
  1441. return
  1442. }
  1443. err = unmarshal(body, &st)
  1444. return
  1445. }
  1446. // WebhookExecute executes a webhook.
  1447. // webhookID: The ID of a webhook.
  1448. // token : The auth token for the webhook
  1449. func (s *Session) WebhookExecute(webhookID, token string, wait bool, data *WebhookParams) (err error) {
  1450. uri := EndpointWebhookToken(webhookID, token)
  1451. if wait {
  1452. uri += "?wait=true"
  1453. }
  1454. _, err = s.RequestWithBucketID("POST", uri, data, EndpointWebhookToken("", ""))
  1455. return
  1456. }
  1457. // MessageReactionAdd creates an emoji reaction to a message.
  1458. // channelID : The channel ID.
  1459. // messageID : The message ID.
  1460. // emojiID : Either the unicode emoji for the reaction, or a guild emoji identifier.
  1461. func (s *Session) MessageReactionAdd(channelID, messageID, emojiID string) error {
  1462. _, err := s.RequestWithBucketID("PUT", EndpointMessageReaction(channelID, messageID, emojiID, "@me"), nil, EndpointMessageReaction(channelID, "", "", ""))
  1463. return err
  1464. }
  1465. // MessageReactionRemove deletes an emoji reaction to a message.
  1466. // channelID : The channel ID.
  1467. // messageID : The message ID.
  1468. // emojiID : Either the unicode emoji for the reaction, or a guild emoji identifier.
  1469. // userID : @me or ID of the user to delete the reaction for.
  1470. func (s *Session) MessageReactionRemove(channelID, messageID, emojiID, userID string) error {
  1471. _, err := s.RequestWithBucketID("DELETE", EndpointMessageReaction(channelID, messageID, emojiID, userID), nil, EndpointMessageReaction(channelID, "", "", ""))
  1472. return err
  1473. }
  1474. // MessageReactions gets all the users reactions for a specific emoji.
  1475. // channelID : The channel ID.
  1476. // messageID : The message ID.
  1477. // emojiID : Either the unicode emoji for the reaction, or a guild emoji identifier.
  1478. // limit : max number of users to return (max 100)
  1479. func (s *Session) MessageReactions(channelID, messageID, emojiID string, limit int) (st []*User, err error) {
  1480. uri := EndpointMessageReactions(channelID, messageID, emojiID)
  1481. v := url.Values{}
  1482. if limit > 0 {
  1483. v.Set("limit", strconv.Itoa(limit))
  1484. }
  1485. if len(v) > 0 {
  1486. uri = fmt.Sprintf("%s?%s", uri, v.Encode())
  1487. }
  1488. body, err := s.RequestWithBucketID("GET", uri, nil, EndpointMessageReaction(channelID, "", "", ""))
  1489. if err != nil {
  1490. return
  1491. }
  1492. err = unmarshal(body, &st)
  1493. return
  1494. }
  1495. // ------------------------------------------------------------------------------------------------
  1496. // Functions specific to user notes
  1497. // ------------------------------------------------------------------------------------------------
  1498. // UserNoteSet sets the note for a specific user.
  1499. func (s *Session) UserNoteSet(userID string, message string) (err error) {
  1500. data := struct {
  1501. Note string `json:"note"`
  1502. }{message}
  1503. _, err = s.RequestWithBucketID("PUT", EndpointUserNotes(userID), data, EndpointUserNotes(""))
  1504. return
  1505. }
  1506. // ------------------------------------------------------------------------------------------------
  1507. // Functions specific to Discord Relationships (Friends list)
  1508. // ------------------------------------------------------------------------------------------------
  1509. // RelationshipsGet returns an array of all the relationships of the user.
  1510. func (s *Session) RelationshipsGet() (r []*Relationship, err error) {
  1511. body, err := s.RequestWithBucketID("GET", EndpointRelationships(), nil, EndpointRelationships())
  1512. if err != nil {
  1513. return
  1514. }
  1515. err = unmarshal(body, &r)
  1516. return
  1517. }
  1518. // relationshipCreate creates a new relationship. (I.e. send or accept a friend request, block a user.)
  1519. // relationshipType : 1 = friend, 2 = blocked, 3 = incoming friend req, 4 = sent friend req
  1520. func (s *Session) relationshipCreate(userID string, relationshipType int) (err error) {
  1521. data := struct {
  1522. Type int `json:"type"`
  1523. }{relationshipType}
  1524. _, err = s.RequestWithBucketID("PUT", EndpointRelationship(userID), data, EndpointRelationships())
  1525. return
  1526. }
  1527. // RelationshipFriendRequestSend sends a friend request to a user.
  1528. // userID: ID of the user.
  1529. func (s *Session) RelationshipFriendRequestSend(userID string) (err error) {
  1530. err = s.relationshipCreate(userID, 4)
  1531. return
  1532. }
  1533. // RelationshipFriendRequestAccept accepts a friend request from a user.
  1534. // userID: ID of the user.
  1535. func (s *Session) RelationshipFriendRequestAccept(userID string) (err error) {
  1536. err = s.relationshipCreate(userID, 1)
  1537. return
  1538. }
  1539. // RelationshipUserBlock blocks a user.
  1540. // userID: ID of the user.
  1541. func (s *Session) RelationshipUserBlock(userID string) (err error) {
  1542. err = s.relationshipCreate(userID, 2)
  1543. return
  1544. }
  1545. // RelationshipDelete removes the relationship with a user.
  1546. // userID: ID of the user.
  1547. func (s *Session) RelationshipDelete(userID string) (err error) {
  1548. _, err = s.RequestWithBucketID("DELETE", EndpointRelationship(userID), nil, EndpointRelationships())
  1549. return
  1550. }
  1551. // RelationshipsMutualGet returns an array of all the users both @me and the given user is friends with.
  1552. // userID: ID of the user.
  1553. func (s *Session) RelationshipsMutualGet(userID string) (mf []*User, err error) {
  1554. body, err := s.RequestWithBucketID("GET", EndpointRelationshipsMutual(userID), nil, EndpointRelationshipsMutual(userID))
  1555. if err != nil {
  1556. return
  1557. }
  1558. err = unmarshal(body, &mf)
  1559. return
  1560. }