123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- /******************************************************************************
- * A Discord API for Golang.
- * See discord.go for more information.
- *
- * This file contains functions for interacting with the Discord HTTP REST API
- * at the lowest level.
- */
- package discordgo
- import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "io/ioutil"
- "net/http"
- "time"
- )
- // Constants of all known Discord API Endpoints
- // Please let me know if you know of any others.
- const (
- DISCORD = "http://discordapp.com"
- API = DISCORD + "/api/"
- GUILDS = API + "guilds/"
- CHANNELS = API + "channels/"
- USERS = API + "users/"
- GATEWAY = API + "gateway"
- AUTH = API + "auth/"
- LOGIN = AUTH + "login"
- LOGOUT = AUTH + "logout"
- VERIFY = AUTH + "verify"
- VERIFY_RESEND = AUTH + "verify/resend"
- FORGOT_PASSWORD = AUTH + "forgot"
- RESET_PASSWORD = AUTH + "reset"
- REGISTER = AUTH + "register"
- VOICE = API + "/voice/"
- REGIONS = VOICE + "regions"
- ICE = VOICE + "ice"
- TUTORIAL = API + "tutorial/"
- TUTORIAL_INDICATORS = TUTORIAL + "indicators"
- INVITE = API + "invite"
- TRACK = API + "track"
- SSO = API + "sso"
- REPORT = API + "report"
- INTEGRATIONS = API + "integrations"
- )
- // Almost like the constants above :) Except can't be constants
- var (
- USER = func(userId string) string { return USERS + userId }
- USER_AVATAR = func(userId, hash string) string { return USERS + userId + "/avatars/" + hash + ".jpg" }
- USER_SETTINGS = func(userId string) string { return USERS + userId + "/settings" }
- USER_GUILDS = func(userId string) string { return USERS + userId + "/guilds" }
- USER_CHANNELS = func(userId string) string { return USERS + userId + "/channels" }
- USER_DEVICES = func(userId string) string { return USERS + userId + "/devices" }
- USER_CONNECTIONS = func(userId string) string { return USERS + userId + "/connections" }
- GUILD = func(guildId string) string { return GUILDS + guildId }
- GUILD_CHANNELS = func(guildId string) string { return GUILDS + guildId + "/channels" }
- GUILD_MEMBERS = func(guildId string) string { return GUILDS + guildId + "/members" }
- GUILD_INTEGRATIONS = func(guildId string) string { return GUILDS + guildId + "/integrations" }
- GUILD_BANS = func(guildId string) string { return GUILDS + guildId + "/bans" }
- GUILD_ROLES = func(guildId string) string { return GUILDS + guildId + "/roles" }
- GUILD_INVITES = func(guildId string) string { return GUILDS + guildId + "/invites" }
- GUILD_EMBED = func(guildId string) string { return GUILDS + guildId + "/embed" }
- GUILD_PRUNE = func(guildId string) string { return GUILDS + guildId + "/prune" }
- GUILD_ICON = func(guildId, hash string) string { return GUILDS + guildId + "/icons/" + hash + ".jpg" }
- CHANNEL = func(channelId string) string { return CHANNELS + channelId }
- CHANNEL_MESSAGES = func(channelId string) string { return CHANNELS + channelId + "/messages" }
- CHANNEL_PERMISSIONS = func(channelId string) string { return CHANNELS + channelId + "/permissions" }
- CHANNEL_INVITES = func(channelId string) string { return CHANNELS + channelId + "/invites" }
- CHANNEL_TYPING = func(channelId string) string { return CHANNELS + channelId + "/typing" }
- INTEGRATIONS_JOIN = func(intId string) string { return API + "integrations/" + intId + "/join" }
- )
- // Request makes a (GET/POST/?) Requests to Discord REST API.
- // All the other Discord REST Calls in this file use this function.
- func (s *Session) Request(method, urlStr, body string) (response []byte, err error) {
- if s.Debug {
- fmt.Println("REQUEST :: " + method + " " + urlStr + "\n" + body)
- }
- req, err := http.NewRequest(method, urlStr, bytes.NewBuffer([]byte(body)))
- if err != nil {
- return
- }
- // Not used on initial login..
- if s.Token != "" {
- req.Header.Set("authorization", s.Token)
- }
- req.Header.Set("Content-Type", "application/json")
- client := &http.Client{Timeout: (20 * time.Second)}
- resp, err := client.Do(req)
- if err != nil {
- return
- }
- response, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- resp.Body.Close()
- if resp.StatusCode != 204 && resp.StatusCode != 200 {
- err = errors.New(fmt.Sprintf("StatusCode: %d, %s", resp.StatusCode, string(response)))
- return
- }
- if s.Debug {
- printJSON(response)
- }
- return
- }
- /***************************************************************************************************
- * Functions specific to this session.
- */
- // Login asks the Discord server for an authentication token
- func (s *Session) Login(email string, password string) (token string, err error) {
- response, err := s.Request("POST", LOGIN, fmt.Sprintf(`{"email":"%s", "password":"%s"}`, email, password))
- var temp map[string]interface{}
- err = json.Unmarshal(response, &temp)
- token = temp["token"].(string)
- 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))
- return
- }
- // Gateway returns the a websocket Gateway address
- func (s *Session) Gateway() (gateway string, err error) {
- response, err := s.Request("GET", GATEWAY, ``)
- var temp map[string]interface{}
- err = json.Unmarshal(response, &temp)
- gateway = temp["url"].(string)
- return
- }
- // VoiceRegions returns the voice server regions
- func (s *Session) VoiceRegions() (st []VoiceRegion, err error) {
- body, err := s.Request("GET", REGIONS, ``)
- err = json.Unmarshal(body, &st)
- return
- }
- // VoiceIce returns the voice server ICE information
- func (s *Session) VoiceIce() (st VoiceIce, err error) {
- body, err := s.Request("GET", ICE, ``)
- err = json.Unmarshal(body, &st)
- return
- }
- /***************************************************************************************************
- * Functions related to a specific user
- */
- // 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), ``)
- err = json.Unmarshal(body, &st)
- return
- }
- // UserSettings returns the settings for a given user
- // userId : A user Id or "@me" which is a shortcut of current user ID
- // This seems to only return a result for "@me"
- func (s *Session) UserSettings(userId string) (st Settings, err error) {
- body, err := s.Request("GET", USER_SETTINGS(userId), ``)
- err = json.Unmarshal(body, &st)
- return
- }
- // UserChannels returns an array of Channel structures for all private
- // channels for a user
- // userId : A user Id or "@me" which is a shortcut of current user ID
- func (s *Session) UserChannels(userId string) (st []Channel, err error) {
- body, err := s.Request("GET", USER_CHANNELS(userId), ``)
- err = json.Unmarshal(body, &st)
- return
- }
- // UserGuilds returns an array of Guild structures for all guilds for a given user
- // userId : A user Id or "@me" which is a shortcut of current user ID
- func (s *Session) UserGuilds(userId string) (st []Guild, err error) {
- body, err := s.Request("GET", USER_GUILDS(userId), ``)
- err = json.Unmarshal(body, &st)
- return
- }
- /***************************************************************************************************
- * Functions related to a specific guild
- */
- // Guild returns a Guild structure of a specific Guild.
- // guildId : The ID of the Guild you want returend.
- func (s *Session) Guild(guildId string) (st []Guild, err error) {
- body, err := s.Request("GET", GUILD(guildId), ``)
- err = json.Unmarshal(body, &st)
- return
- }
- // GuildMembers returns an array of Member structures for all members of a
- // given guild.
- // guildId : The ID of a Guild.
- func (s *Session) GuildMembers(guildId string) (st []Member, err error) {
- body, err := s.Request("GET", GUILD_MEMBERS(guildId), ``)
- err = json.Unmarshal(body, &st)
- 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), ``)
- err = json.Unmarshal(body, &st)
- return
- }
- /***************************************************************************************************
- * Functions related to a specific channel
- */
- // Channel returns a Channel strucutre of a specific Channel.
- // channelId : The ID of the Channel you want returend.
- func (s *Session) Channel(channelId string) (st Channel, err error) {
- body, err := s.Request("GET", CHANNEL(channelId), ``)
- err = json.Unmarshal(body, &st)
- return
- }
- // ChannelMessages returns an array of Message structures for messaages within
- // a given channel.
- // channelId : The ID of a Channel.
- // limit : The number messages that can be returned.
- // 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 int, afterId int) (st []Message, err error) {
- var urlStr string = ""
- if limit > 0 {
- urlStr = fmt.Sprintf("?limit=%d", limit)
- }
- if afterId > 0 {
- if urlStr != "" {
- urlStr = urlStr + fmt.Sprintf("&after=%d", afterId)
- } else {
- urlStr = fmt.Sprintf("?after=%d", afterId)
- }
- }
- if beforeId > 0 {
- if urlStr != "" {
- urlStr = urlStr + fmt.Sprintf("&before=%d", beforeId)
- } else {
- urlStr = fmt.Sprintf("?before=%d", beforeId)
- }
- }
- body, err := s.Request("GET", CHANNEL_MESSAGES(channelId)+urlStr, ``)
- err = json.Unmarshal(body, &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) {
- response, err := s.Request("POST", CHANNEL_MESSAGES(channelId), fmt.Sprintf(`{"content":"%s"}`, content))
- err = json.Unmarshal(response, &st)
- return
- }
|