restapi.go 57 KB

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