12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241 |
- // Discordgo - Discord bindings for Go
- // Available at https://github.com/bwmarrin/discordgo
- // Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- // This file contains functions for interacting with the Discord REST/JSON API
- // at the lowest level.
- package discordgo
- import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "image"
- _ "image/jpeg" // For JPEG decoding
- _ "image/png" // For PNG decoding
- "io"
- "io/ioutil"
- "log"
- "mime/multipart"
- "net/http"
- "net/url"
- "strconv"
- "time"
- )
- // ErrJSONUnmarshal is returned for JSON Unmarshall errors.
- var ErrJSONUnmarshal = errors.New("json unmarshal")
- // Request makes a (GET/POST/...) Requests to Discord REST API with JSON data.
- // All the other Discord REST Calls in this file use this function.
- func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) {
- var body []byte
- if data != nil {
- body, err = json.Marshal(data)
- if err != nil {
- return
- }
- }
- return s.request(method, urlStr, "application/json", body)
- }
- // request makes a (GET/POST/...) Requests to Discord REST API.
- func (s *Session) request(method, urlStr, contentType string, b []byte) (response []byte, err error) {
- if s.Debug {
- log.Printf("API REQUEST %8s :: %s\n", method, urlStr)
- log.Printf("API REQUEST PAYLOAD :: [%s]\n", string(b))
- }
- req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(b))
- if err != nil {
- return
- }
- // Not used on initial login..
- // TODO: Verify if a login, otherwise complain about no-token
- if s.Token != "" {
- req.Header.Set("authorization", s.Token)
- }
- req.Header.Set("Content-Type", contentType)
- // TODO: Make a configurable static variable.
- req.Header.Set("User-Agent", fmt.Sprintf("DiscordBot (https://github.com/bwmarrin/discordgo, v%s)", VERSION))
- if s.Debug {
- for k, v := range req.Header {
- log.Printf("API REQUEST HEADER :: [%s] = %+v\n", k, v)
- }
- }
- client := &http.Client{Timeout: (20 * time.Second)}
- resp, err := client.Do(req)
- if err != nil {
- return
- }
- defer func() {
- err := resp.Body.Close()
- if err != nil {
- log.Println("error closing resp body")
- }
- }()
- response, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- if s.Debug {
- log.Printf("API RESPONSE STATUS :: %s\n", resp.Status)
- for k, v := range resp.Header {
- log.Printf("API RESPONSE HEADER :: [%s] = %+v\n", k, v)
- }
- log.Printf("API RESPONSE BODY :: [%s]\n\n\n", response)
- }
- switch resp.StatusCode {
- case http.StatusOK:
- case http.StatusCreated:
- case http.StatusNoContent:
- // TODO check for 401 response, invalidate token if we get one.
- case 429: // TOO MANY REQUESTS - Rate limiting
- rl := RateLimit{}
- err = json.Unmarshal(response, &rl)
- if err != nil {
- err = fmt.Errorf("Request unmarshal rate limit error : %+v", err)
- return
- }
- time.Sleep(rl.RetryAfter)
- response, err = s.request(method, urlStr, contentType, b)
- default: // Error condition
- err = fmt.Errorf("HTTP %s, %s", resp.Status, response)
- }
- return
- }
- func unmarshal(data []byte, v interface{}) error {
- err := json.Unmarshal(data, v)
- if err != nil {
- return ErrJSONUnmarshal
- }
- return nil
- }
- // ------------------------------------------------------------------------------------------------
- // Functions specific to Discord Sessions
- // ------------------------------------------------------------------------------------------------
- // Login asks the Discord server for an authentication token.
- func (s *Session) Login(email, password string) (err error) {
- data := struct {
- Email string `json:"email"`
- Password string `json:"password"`
- }{email, password}
- response, err := s.Request("POST", LOGIN, data)
- if err != nil {
- return
- }
- temp := struct {
- Token string `json:"token"`
- }{}
- err = unmarshal(response, &temp)
- if err != nil {
- return
- }
- s.Token = temp.Token
- return
- }
- // Register sends a Register request to Discord, and returns the authentication token
- // Note that this account is temporary and should be verified for future use.
- // Another option is to save the authentication token external, but this isn't recommended.
- func (s *Session) Register(username string) (token string, err error) {
- data := struct {
- Username string `json:"username"`
- }{username}
- response, err := s.Request("POST", REGISTER, data)
- if err != nil {
- return
- }
- temp := struct {
- Token string `json:"token"`
- }{}
- err = unmarshal(response, &temp)
- if err != nil {
- return
- }
- token = temp.Token
- return
- }
- // Logout sends a logout request to Discord.
- // This does not seem to actually invalidate the token. So you can still
- // make API calls even after a Logout. So, it seems almost pointless to
- // even use.
- func (s *Session) Logout() (err error) {
- // _, err = s.Request("POST", LOGOUT, fmt.Sprintf(`{"token": "%s"}`, s.Token))
- if s.Token == "" {
- return
- }
- data := struct {
- Token string `json:"token"`
- }{s.Token}
- _, err = s.Request("POST", LOGOUT, data)
- return
- }
- // ------------------------------------------------------------------------------------------------
- // Functions specific to Discord Users
- // ------------------------------------------------------------------------------------------------
- // User returns the user details of the given userID
- // userID : A user ID or "@me" which is a shortcut of current user ID
- func (s *Session) User(userID string) (st *User, err error) {
- body, err := s.Request("GET", USER(userID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // UserAvatar returns an image.Image of a users Avatar.
- // userID : A user ID or "@me" which is a shortcut of current user ID
- func (s *Session) UserAvatar(userID string) (img image.Image, err error) {
- u, err := s.User(userID)
- if err != nil {
- return
- }
- body, err := s.Request("GET", USER_AVATAR(userID, u.Avatar), nil)
- if err != nil {
- return
- }
- img, _, err = image.Decode(bytes.NewReader(body))
- return
- }
- // UserUpdate updates a users settings.
- func (s *Session) UserUpdate(email, password, username, avatar, newPassword string) (st *User, err error) {
- // NOTE: Avatar must be either the hash/id of existing Avatar or
- // data:image/png;base64,BASE64_STRING_OF_NEW_AVATAR_PNG
- // to set a new avatar.
- // If left blank, avatar will be set to null/blank
- data := struct {
- Email string `json:"email"`
- Password string `json:"password"`
- Username string `json:"username"`
- Avatar string `json:"avatar,omitempty"`
- NewPassword string `json:"new_password,omitempty"`
- }{email, password, username, avatar, newPassword}
- body, err := s.Request("PATCH", USER("@me"), data)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // UserSettings returns the settings for a given user
- func (s *Session) UserSettings() (st *Settings, err error) {
- body, err := s.Request("GET", USER_SETTINGS("@me"), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // UserChannels returns an array of Channel structures for all private
- // channels.
- func (s *Session) UserChannels() (st []*Channel, err error) {
- body, err := s.Request("GET", USER_CHANNELS("@me"), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // UserChannelCreate creates a new User (Private) Channel with another User
- // recipientID : A user ID for the user to which this channel is opened with.
- func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) {
- data := struct {
- RecipientID string `json:"recipient_id"`
- }{recipientID}
- body, err := s.Request("POST", USER_CHANNELS("@me"), data)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // UserGuilds returns an array of Guild structures for all guilds.
- func (s *Session) UserGuilds() (st []*Guild, err error) {
- body, err := s.Request("GET", USER_GUILDS("@me"), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // UserChannelPermissions returns the permission of a user in a channel.
- // userID : The ID of the user to calculate permissions for.
- // channelID : The ID of the channel to calculate permission for.
- func (s *Session) UserChannelPermissions(userID, channelID string) (apermissions int, err error) {
- channel, err := s.Channel(channelID)
- if err != nil {
- return
- }
- guild, err := s.Guild(channel.GuildID)
- if err != nil {
- return
- }
- if userID == guild.OwnerID {
- apermissions = PermissionAll
- return
- }
- member, err := s.GuildMember(guild.ID, userID)
- if err != nil {
- return
- }
- for _, role := range guild.Roles {
- for _, roleID := range member.Roles {
- if role.ID == roleID {
- apermissions |= role.Permissions
- break
- }
- }
- }
- if apermissions&PermissionManageRoles > 0 {
- apermissions |= PermissionAll
- }
- // Member overwrites can override role overrides, so do two passes
- for _, overwrite := range channel.PermissionOverwrites {
- for _, roleID := range member.Roles {
- if overwrite.Type == "role" && roleID == overwrite.ID {
- apermissions &= ^overwrite.Deny
- apermissions |= overwrite.Allow
- break
- }
- }
- }
- for _, overwrite := range channel.PermissionOverwrites {
- if overwrite.Type == "member" && overwrite.ID == userID {
- apermissions &= ^overwrite.Deny
- apermissions |= overwrite.Allow
- break
- }
- }
- if apermissions&PermissionManageRoles > 0 {
- apermissions |= PermissionAllChannel
- }
- return
- }
- // ------------------------------------------------------------------------------------------------
- // Functions specific to Discord Guilds
- // ------------------------------------------------------------------------------------------------
- // Guild returns a Guild structure of a specific Guild.
- // guildID : The ID of a Guild
- func (s *Session) Guild(guildID string) (st *Guild, err error) {
- if s.StateEnabled {
- // Attempt to grab the guild from State first.
- st, err = s.State.Guild(guildID)
- if err == nil {
- return
- }
- }
- body, err := s.Request("GET", GUILD(guildID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildCreate creates a new Guild
- // name : A name for the Guild (2-100 characters)
- func (s *Session) GuildCreate(name string) (st *Guild, err error) {
- data := struct {
- Name string `json:"name"`
- }{name}
- body, err := s.Request("POST", GUILDS, data)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildEdit edits a new Guild
- // guildID : The ID of a Guild
- // g : A GuildParams struct with the values Name, Region and VerificationLevel defined.
- func (s *Session) GuildEdit(guildID string, g GuildParams) (st *Guild, err error) {
- // Bounds checking for VerificationLevel, interval: [0, 3]
- if g.VerificationLevel != nil {
- val := *g.VerificationLevel
- if val < 0 || val > 3 {
- err = errors.New("VerificationLevel out of bounds, should be between 0 and 3")
- return
- }
- }
- //Bounds checking for regions
- if g.Region != "" {
- isValid := false
- regions, _ := s.VoiceRegions()
- for _, r := range regions {
- if g.Region == r.ID {
- isValid = true
- }
- }
- if !isValid {
- var valid []string
- for _, r := range regions {
- valid = append(valid, r.ID)
- }
- err = fmt.Errorf("Region not a valid region (%q)", valid)
- return
- }
- }
- data := struct {
- Name string `json:"name,omitempty"`
- Region string `json:"region,omitempty"`
- VerificationLevel *VerificationLevel `json:"verification_level,omitempty"`
- }{g.Name, g.Region, g.VerificationLevel}
- body, err := s.Request("PATCH", GUILD(guildID), data)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildDelete deletes a Guild.
- // guildID : The ID of a Guild
- func (s *Session) GuildDelete(guildID string) (st *Guild, err error) {
- body, err := s.Request("DELETE", GUILD(guildID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildLeave leaves a Guild.
- // guildID : The ID of a Guild
- func (s *Session) GuildLeave(guildID string) (err error) {
- _, err = s.Request("DELETE", USER_GUILD("@me", guildID), nil)
- return
- }
- // GuildBans returns an array of User structures for all bans of a
- // given guild.
- // guildID : The ID of a Guild.
- func (s *Session) GuildBans(guildID string) (st []*User, err error) {
- body, err := s.Request("GET", GUILD_BANS(guildID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildBanCreate bans the given user from the given guild.
- // guildID : The ID of a Guild.
- // userID : The ID of a User
- // days : The number of days of previous comments to delete.
- func (s *Session) GuildBanCreate(guildID, userID string, days int) (err error) {
- uri := GUILD_BAN(guildID, userID)
- if days > 0 {
- uri = fmt.Sprintf("%s?delete-message-days=%d", uri, days)
- }
- _, err = s.Request("PUT", uri, nil)
- return
- }
- // GuildBanDelete removes the given user from the guild bans
- // guildID : The ID of a Guild.
- // userID : The ID of a User
- func (s *Session) GuildBanDelete(guildID, userID string) (err error) {
- _, err = s.Request("DELETE", GUILD_BAN(guildID, userID), nil)
- return
- }
- // GuildMembers returns a list of members for a guild.
- // guildID : The ID of a Guild.
- // offset : A number of members to skip
- // limit : max number of members to return (max 1000)
- func (s *Session) GuildMembers(guildID string, offset, limit int) (st []*Member, err error) {
- uri := GUILD_MEMBERS(guildID)
- v := url.Values{}
- if offset > 0 {
- v.Set("offset", strconv.Itoa(offset))
- }
- if limit > 0 {
- v.Set("limit", strconv.Itoa(limit))
- }
- if len(v) > 0 {
- uri = fmt.Sprintf("%s?%s", uri, v.Encode())
- }
- body, err := s.Request("GET", uri, nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildMember returns a member of a guild.
- // guildID : The ID of a Guild.
- // userID : The ID of a User
- func (s *Session) GuildMember(guildID, userID string) (st *Member, err error) {
- body, err := s.Request("GET", GUILD_MEMBER(guildID, userID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildMemberDelete removes the given user from the given guild.
- // guildID : The ID of a Guild.
- // userID : The ID of a User
- func (s *Session) GuildMemberDelete(guildID, userID string) (err error) {
- _, err = s.Request("DELETE", GUILD_MEMBER(guildID, userID), nil)
- return
- }
- // GuildMemberEdit edits the roles of a member.
- // guildID : The ID of a Guild.
- // userID : The ID of a User.
- // roles : A list of role ID's to set on the member.
- func (s *Session) GuildMemberEdit(guildID, userID string, roles []string) (err error) {
- data := struct {
- Roles []string `json:"roles"`
- }{roles}
- _, err = s.Request("PATCH", GUILD_MEMBER(guildID, userID), data)
- if err != nil {
- return
- }
- return
- }
- // GuildMemberMove moves a guild member from one voice channel to another/none
- // guildID : The ID of a Guild.
- // userID : The ID of a User.
- // channelID : The ID of a channel to move user to, or null?
- // NOTE : I am not entirely set on the name of this function and it may change
- // prior to the final 1.0.0 release of Discordgo
- func (s *Session) GuildMemberMove(guildID, userID, channelID string) (err error) {
- data := struct {
- ChannelID string `json:"channel_id"`
- }{channelID}
- _, err = s.Request("PATCH", GUILD_MEMBER(guildID, userID), data)
- if err != nil {
- return
- }
- return
- }
- // GuildChannels returns an array of Channel structures for all channels of a
- // given guild.
- // guildID : The ID of a Guild.
- func (s *Session) GuildChannels(guildID string) (st []*Channel, err error) {
- body, err := s.Request("GET", GUILD_CHANNELS(guildID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildChannelCreate creates a new channel in the given guild
- // guildID : The ID of a Guild.
- // name : Name of the channel (2-100 chars length)
- // ctype : Tpye of the channel (voice or text)
- func (s *Session) GuildChannelCreate(guildID, name, ctype string) (st *Channel, err error) {
- data := struct {
- Name string `json:"name"`
- Type string `json:"type"`
- }{name, ctype}
- body, err := s.Request("POST", GUILD_CHANNELS(guildID), data)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildInvites returns an array of Invite structures for the given guild
- // guildID : The ID of a Guild.
- func (s *Session) GuildInvites(guildID string) (st []*Invite, err error) {
- body, err := s.Request("GET", GUILD_INVITES(guildID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildRoles returns all roles for a given guild.
- // guildID : The ID of a Guild.
- func (s *Session) GuildRoles(guildID string) (st []*Role, err error) {
- body, err := s.Request("GET", GUILD_ROLES(guildID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return // TODO return pointer
- }
- // GuildRoleCreate returns a new Guild Role.
- // guildID: The ID of a Guild.
- func (s *Session) GuildRoleCreate(guildID string) (st *Role, err error) {
- body, err := s.Request("POST", GUILD_ROLES(guildID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildRoleEdit updates an existing Guild Role with new values
- // guildID : The ID of a Guild.
- // roleID : The ID of a Role.
- // name : The name of the Role.
- // color : The color of the role (decimal, not hex).
- // hoist : Whether to display the role's users separately.
- // perm : The permissions for the role.
- func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int) (st *Role, err error) {
- data := struct {
- Name string `json:"name"` // The color the role should have (as a decimal, not hex)
- Color int `json:"color"` // Whether to display the role's users separately
- Hoist bool `json:"hoist"` // The role's name (overwrites existing)
- Permissions int `json:"permissions"` // The overall permissions number of the role (overwrites existing)
- }{name, color, hoist, perm}
- body, err := s.Request("PATCH", GUILD_ROLE(guildID, roleID), data)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildRoleReorder reoders guild roles
- // guildID : The ID of a Guild.
- // roles : A list of ordered roles.
- func (s *Session) GuildRoleReorder(guildID string, roles []*Role) (st []*Role, err error) {
- body, err := s.Request("PATCH", GUILD_ROLES(guildID), roles)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildRoleDelete deletes an existing role.
- // guildID : The ID of a Guild.
- // roleID : The ID of a Role.
- func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) {
- _, err = s.Request("DELETE", GUILD_ROLE(guildID, roleID), nil)
- return
- }
- // GuildIntegrations returns an array of Integrations for a guild.
- // guildID : The ID of a Guild.
- func (s *Session) GuildIntegrations(guildID string) (st []*GuildIntegration, err error) {
- body, err := s.Request("GET", GUILD_INTEGRATIONS(guildID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildIntegrationCreate creates a Guild Integration.
- // guildID : The ID of a Guild.
- // integrationType : The Integration type.
- // integrationID : The ID of an integration.
- func (s *Session) GuildIntegrationCreate(guildID, integrationType, integrationID string) (err error) {
- data := struct {
- Type string `json:"type"`
- Id string `json:"id"`
- }{integrationType, integrationID}
- _, err = s.Request("POST", GUILD_INTEGRATIONS(guildID), data)
- return
- }
- // GuildIntegrationEdit edits a Guild Integration.
- // guildID : The ID of a Guild.
- // integrationType : The Integration type.
- // integrationID : The ID of an integration.
- // expireBehavior : The behavior when an integration subscription lapses (see the integration object documentation).
- // expireGracePeriod : Period (in seconds) where the integration will ignore lapsed subscriptions.
- // enableEmoticons : Whether emoticons should be synced for this integration (twitch only currently).
- func (s *Session) GuildIntegrationEdit(guildID, integrationID string, expireBehavior, expireGracePeriod int, enableEmoticons bool) (err error) {
- data := struct {
- ExpireBehavior int `json:"expire_behavior"`
- ExpireGracePeriod int `json:"expire_grace_period"`
- EnableEmoticons bool `json:"enable_emoticons"`
- }{expireBehavior, expireGracePeriod, enableEmoticons}
- _, err = s.Request("PATCH", GUILD_INTEGRATION(guildID, integrationID), data)
- return
- }
- // GuildIntegrationDelete removes the given integration from the Guild.
- // guildID : The ID of a Guild.
- // integrationID : The ID of an integration.
- func (s *Session) GuildIntegrationDelete(guildID, integrationID string) (err error) {
- _, err = s.Request("DELETE", GUILD_INTEGRATION(guildID, integrationID), nil)
- return
- }
- // GuildIntegrationSync syncs an integration.
- // guildID : The ID of a Guild.
- // integrationID : The ID of an integration.
- func (s *Session) GuildIntegrationSync(guildID, integrationID string) (err error) {
- _, err = s.Request("POST", GUILD_INTEGRATION_SYNC(guildID, integrationID), nil)
- return
- }
- // GuildIcon returns an image.Image of a guild icon.
- // guildID : The ID of a Guild.
- func (s *Session) GuildIcon(guildID string) (img image.Image, err error) {
- g, err := s.Guild(guildID)
- if err != nil {
- return
- }
- if g.Icon == "" {
- err = errors.New("Guild does not have an icon set.")
- return
- }
- body, err := s.Request("GET", GUILD_ICON(guildID, g.Icon), nil)
- if err != nil {
- return
- }
- img, _, err = image.Decode(bytes.NewReader(body))
- return
- }
- // GuildSplash returns an image.Image of a guild splash image.
- // guildID : The ID of a Guild.
- func (s *Session) GuildSplash(guildID string) (img image.Image, err error) {
- g, err := s.Guild(guildID)
- if err != nil {
- return
- }
- if g.Splash == "" {
- err = errors.New("Guild does not have a splash set.")
- return
- }
- body, err := s.Request("GET", GUILD_SPLASH(guildID, g.Splash), nil)
- if err != nil {
- return
- }
- img, _, err = image.Decode(bytes.NewReader(body))
- return
- }
- // GuildEmbed returns the embed for a Guild.
- // guildID : The ID of a Guild.
- func (s *Session) GuildEmbed(guildID string) (st *GuildEmbed, err error) {
- body, err := s.Request("GET", GUILD_EMBED(guildID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // GuildEmbedEdit returns the embed for a Guild.
- // guildID : The ID of a Guild.
- func (s *Session) GuildEmbedEdit(guildID string, enabled bool, channelID string) (err error) {
- data := GuildEmbed{enabled, channelID}
- _, err = s.Request("PATCH", GUILD_EMBED(guildID), data)
- return
- }
- // ------------------------------------------------------------------------------------------------
- // Functions specific to Discord Channels
- // ------------------------------------------------------------------------------------------------
- // Channel returns a Channel strucutre of a specific Channel.
- // channelID : The ID of the Channel you want returned.
- func (s *Session) Channel(channelID string) (st *Channel, err error) {
- body, err := s.Request("GET", CHANNEL(channelID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // ChannelEdit edits the given channel
- // channelID : The ID of a Channel
- // name : The new name to assign the channel.
- func (s *Session) ChannelEdit(channelID, name string) (st *Channel, err error) {
- data := struct {
- Name string `json:"name"`
- }{name}
- body, err := s.Request("PATCH", CHANNEL(channelID), data)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // ChannelDelete deletes the given channel
- // channelID : The ID of a Channel
- func (s *Session) ChannelDelete(channelID string) (st *Channel, err error) {
- body, err := s.Request("DELETE", CHANNEL(channelID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // ChannelTyping broadcasts to all members that authenticated user is typing in
- // the given channel.
- // channelID : The ID of a Channel
- func (s *Session) ChannelTyping(channelID string) (err error) {
- _, err = s.Request("POST", CHANNEL_TYPING(channelID), nil)
- return
- }
- // ChannelMessages returns an array of Message structures for messages within
- // a given channel.
- // channelID : The ID of a Channel.
- // limit : The number messages that can be returned. (max 100)
- // beforeID : If provided all messages returned will be before given ID.
- // afterID : If provided all messages returned will be after given ID.
- func (s *Session) ChannelMessages(channelID string, limit int, beforeID, afterID string) (st []*Message, err error) {
- uri := CHANNEL_MESSAGES(channelID)
- v := url.Values{}
- if limit > 0 {
- v.Set("limit", strconv.Itoa(limit))
- }
- if afterID != "" {
- v.Set("after", afterID)
- }
- if beforeID != "" {
- v.Set("before", beforeID)
- }
- if len(v) > 0 {
- uri = fmt.Sprintf("%s?%s", uri, v.Encode())
- }
- body, err := s.Request("GET", uri, nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // ChannelMessageAck acknowledges and marks the given message as read
- // channeld : The ID of a Channel
- // messageID : the ID of a Message
- func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
- _, err = s.Request("POST", CHANNEL_MESSAGE_ACK(channelID, messageID), nil)
- return
- }
- // channelMessageSend sends a message to the given channel.
- // channelID : The ID of a Channel.
- // content : The message to send.
- // tts : Whether to send the message with TTS.
- func (s *Session) channelMessageSend(channelID, content string, tts bool) (st *Message, err error) {
- // TODO: nonce string ?
- data := struct {
- Content string `json:"content"`
- TTS bool `json:"tts"`
- }{content, tts}
- // Send the message to the given channel
- response, err := s.Request("POST", CHANNEL_MESSAGES(channelID), data)
- if err != nil {
- return
- }
- err = unmarshal(response, &st)
- return
- }
- // ChannelMessageSend sends a message to the given channel.
- // channelID : The ID of a Channel.
- // content : The message to send.
- func (s *Session) ChannelMessageSend(channelID string, content string) (st *Message, err error) {
- return s.channelMessageSend(channelID, content, false)
- }
- // ChannelMessageSendTTS sends a message to the given channel with Text to Speech.
- // channelID : The ID of a Channel.
- // content : The message to send.
- func (s *Session) ChannelMessageSendTTS(channelID string, content string) (st *Message, err error) {
- return s.channelMessageSend(channelID, content, true)
- }
- // ChannelMessageEdit edits an existing message, replacing it entirely with
- // the given content.
- // channeld : The ID of a Channel
- // messageID : the ID of a Message
- func (s *Session) ChannelMessageEdit(channelID, messageID, content string) (st *Message, err error) {
- data := struct {
- Content string `json:"content"`
- }{content}
- response, err := s.Request("PATCH", CHANNEL_MESSAGE(channelID, messageID), data)
- if err != nil {
- return
- }
- err = unmarshal(response, &st)
- return
- }
- // ChannelMessageDelete deletes a message from the Channel.
- func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) {
- _, err = s.Request("DELETE", CHANNEL_MESSAGE(channelID, messageID), nil)
- return
- }
- // ChannelFileSend sends a file to the given channel.
- // channelID : The ID of a Channel.
- // io.Reader : A reader for the file contents.
- func (s *Session) ChannelFileSend(channelID, name string, r io.Reader) (st *Message, err error) {
- body := &bytes.Buffer{}
- bodywriter := multipart.NewWriter(body)
- writer, err := bodywriter.CreateFormFile("file", name)
- if err != nil {
- return nil, err
- }
- _, err = io.Copy(writer, r)
- if err != nil {
- return
- }
- err = bodywriter.Close()
- if err != nil {
- return
- }
- response, err := s.request("POST", CHANNEL_MESSAGES(channelID), bodywriter.FormDataContentType(), body.Bytes())
- if err != nil {
- return
- }
- err = unmarshal(response, &st)
- return
- }
- // ChannelInvites returns an array of Invite structures for the given channel
- // channelID : The ID of a Channel
- func (s *Session) ChannelInvites(channelID string) (st []*Invite, err error) {
- body, err := s.Request("GET", CHANNEL_INVITES(channelID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // ChannelInviteCreate creates a new invite for the given channel.
- // channelID : The ID of a Channel
- // i : An Invite struct with the values MaxAge, MaxUses, Temporary,
- // and XkcdPass defined.
- func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st *Invite, err error) {
- data := struct {
- MaxAge int `json:"max_age"`
- MaxUses int `json:"max_uses"`
- Temporary bool `json:"temporary"`
- XKCDPass string `json:"xkcdpass"`
- }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
- body, err := s.Request("POST", CHANNEL_INVITES(channelID), data)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // ChannelPermissionSet creates a Permission Override for the given channel.
- // NOTE: This func name may changed. Using Set instead of Create because
- // you can both create a new override or update an override with this function.
- func (s *Session) ChannelPermissionSet(channelID, targetID, targetType string, allow, deny int) (err error) {
- data := struct {
- ID string `json:"id"`
- Type string `json:"type"`
- Allow int `json:"allow"`
- Deny int `json:"deny"`
- }{targetID, targetType, allow, deny}
- _, err = s.Request("PUT", CHANNEL_PERMISSION(channelID, targetID), data)
- return
- }
- // ChannelPermissionDelete deletes a specific permission override for the given channel.
- // NOTE: Name of this func may change.
- func (s *Session) ChannelPermissionDelete(channelID, targetID string) (err error) {
- _, err = s.Request("DELETE", CHANNEL_PERMISSION(channelID, targetID), nil)
- return
- }
- // ------------------------------------------------------------------------------------------------
- // Functions specific to Discord Invites
- // ------------------------------------------------------------------------------------------------
- // Invite returns an Invite structure of the given invite
- // inviteID : The invite code (or maybe xkcdpass?)
- func (s *Session) Invite(inviteID string) (st *Invite, err error) {
- body, err := s.Request("GET", INVITE(inviteID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // InviteDelete deletes an existing invite
- // inviteID : the code (or maybe xkcdpass?) of an invite
- func (s *Session) InviteDelete(inviteID string) (st *Invite, err error) {
- body, err := s.Request("DELETE", INVITE(inviteID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // InviteAccept accepts an Invite to a Guild or Channel
- // inviteID : The invite code (or maybe xkcdpass?)
- func (s *Session) InviteAccept(inviteID string) (st *Invite, err error) {
- body, err := s.Request("POST", INVITE(inviteID), nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // ------------------------------------------------------------------------------------------------
- // Functions specific to Discord Voice
- // ------------------------------------------------------------------------------------------------
- // VoiceRegions returns the voice server regions
- func (s *Session) VoiceRegions() (st []*VoiceRegion, err error) {
- body, err := s.Request("GET", VOICE_REGIONS, nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // VoiceICE returns the voice server ICE information
- func (s *Session) VoiceICE() (st *VoiceICE, err error) {
- body, err := s.Request("GET", VOICE_ICE, nil)
- if err != nil {
- return
- }
- err = unmarshal(body, &st)
- return
- }
- // ------------------------------------------------------------------------------------------------
- // Functions specific to Discord Websockets
- // ------------------------------------------------------------------------------------------------
- // Gateway returns the a websocket Gateway address
- func (s *Session) Gateway() (gateway string, err error) {
- response, err := s.Request("GET", GATEWAY, nil)
- if err != nil {
- return
- }
- temp := struct {
- URL string `json:"url"`
- }{}
- err = unmarshal(response, &temp)
- if err != nil {
- return
- }
- gateway = temp.URL
- return
- }
|