restapi.go 57 KB

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