123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- package discordgo
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "strconv"
- "time"
- )
- func (s *Session) Request(method, urlStr string, data interface{}) (response []byte, err error) {
- if s.Debug {
- fmt.Printf("API REQUEST %8s :: %s\n", method, urlStr)
- fmt.Println("API REQUEST PAYLOAD :: [" + fmt.Sprintf("%+v", data) + "]")
- }
- body, err := json.Marshal(data)
- if err != nil {
- return
- }
- req, err := http.NewRequest(method, urlStr, bytes.NewBuffer(body))
- if err != nil {
- return
- }
-
-
- if s.Token != "" {
- req.Header.Set("authorization", s.Token)
- }
- req.Header.Set("Content-Type", "application/json")
-
- 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 {
- fmt.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
- }
- response, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- return
- }
- resp.Body.Close()
- if s.Debug {
- fmt.Printf("API RESPONSE STATUS :: %s\n", resp.Status)
- for k, v := range resp.Header {
- fmt.Printf("API RESPONSE HEADER :: [%s] = %+v\n", k, v)
- }
- fmt.Printf("API RESPONSE BODY :: [%s]\n", response)
- }
-
- switch resp.StatusCode {
- case 200:
- case 204:
-
-
- default:
- err = fmt.Errorf("HTTP %d", resp.StatusCode)
- return
- }
- return
- }
- func (s *Session) Login(email string, password string) (token string, err error) {
- data := struct {
- Email string `json:"email"`
- Password string `json:"password"`
- }{email, password}
- response, err := s.Request("POST", LOGIN, data)
- var temp map[string]interface{}
- err = json.Unmarshal(response, &temp)
- if err != nil {
- return
- }
- token = temp["token"].(string)
- return
- }
- func (s *Session) Register(username string) (token string, err error) {
- data := struct {
- Username string `json:"username"`
- }{username}
- response, err := s.Request("POST", REGISTER, data)
- var temp map[string]interface{}
- err = json.Unmarshal(response, &temp)
- if err != nil {
- return
- }
- token = temp["token"].(string)
- return
- }
- func (s *Session) Logout() (err error) {
-
- return
- }
- func (s *Session) User(userID string) (st User, err error) {
- body, err := s.Request("GET", USER(userID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) UserUpdate(userID, email, password, username, avatar, newPassword string) (st User, err error) {
-
-
-
-
- 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(userID), data)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) UserAvatar(userID string) (st User, err error) {
- u, err := s.User(userID)
- _, err = s.Request("GET", USER_AVATAR(userID, u.Avatar), nil)
-
- return
- }
- func (s *Session) UserSettings(userID string) (st Settings, err error) {
- body, err := s.Request("GET", USER_SETTINGS(userID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) UserChannels(userID string) (st []Channel, err error) {
- body, err := s.Request("GET", USER_CHANNELS(userID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) UserChannelCreate(userID, recipientID string) (st Channel, err error) {
- data := struct {
- RecipientID string `json:"recipient_id"`
- }{recipientID}
- body, err := s.Request(
- "POST",
- USER_CHANNELS(userID),
- data)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) UserGuilds(userID string) (st []Guild, err error) {
- body, err := s.Request("GET", USER_GUILDS(userID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) Guild(guildID string) (st Guild, err error) {
- body, err := s.Request("GET", GUILD(guildID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildCreate(name string) (st Guild, err error) {
- data := struct {
- Name string `json:"name"`
- }{name}
- body, err := s.Request("POST", GUILDS, data)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildEdit(guildID, name string) (st Guild, err error) {
- data := struct {
- Name string `json:"name"`
- }{name}
- body, err := s.Request("POST", GUILD(guildID), data)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildDelete(guildID string) (st Guild, err error) {
- body, err := s.Request("DELETE", GUILD(guildID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildBans(guildID string) (st []User, err error) {
- body, err := s.Request("GET", GUILD_BANS(guildID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- 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
- }
- func (s *Session) GuildBanDelete(guildID, userID string) (err error) {
- _, err = s.Request("DELETE", GUILD_BAN(guildID, userID), nil)
- return
- }
- func (s *Session) GuildMemberDelete(guildID, userID string) (err error) {
- _, err = s.Request("DELETE", GUILD_MEMBER_DEL(guildID, userID), nil)
- return
- }
- func (s *Session) GuildChannels(guildID string) (st []Channel, err error) {
- body, err := s.Request("GET", GUILD_CHANNELS(guildID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- 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)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildInvites(guildID string) (st []Invite, err error) {
- body, err := s.Request("GET", GUILD_INVITES(guildID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildInviteCreate(guildID string, i Invite) (st Invite, err error) {
- data := struct {
- MaxAge int `json:"max_age"`
- MaxUses int `json:"max_uses"`
- Temporary bool `json:"temporary"`
- XKCDPass bool `json:"xkcdpass"`
- }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
- body, err := s.Request("POST", GUILD_INVITES(guildID), data)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildRoles(guildID string) (st []Role, err error) {
- body, err := s.Request("GET", GUILD_ROLES(guildID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildRoleCreate(guildID string) (st Role, err error) {
- body, err := s.Request("POST", GUILD_ROLES(guildID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildRoleEdit(guildID, roleID, name string, color int, hoist bool, perm int) (st Role, err error) {
- data := struct {
- Name string `json:"name"`
- Color int `json:"color"`
- Hoist bool `json:"hoist"`
- Permissions int `json:"permissions"`
- }{name, color, hoist, perm}
- body, err := s.Request("PATCH", GUILD_ROLE(guildID, roleID), data)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildRoleReorder(guildID string, roles []Role) (st []Role, err error) {
- body, err := s.Request("PATCH", GUILD_ROLES(guildID), roles)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) GuildRoleDelete(guildID, roleID string) (err error) {
- _, err = s.Request("DELETE", GUILD_ROLE(guildID, roleID), nil)
- return
- }
- func (s *Session) Channel(channelID string) (st Channel, err error) {
- body, err := s.Request("GET", CHANNEL(channelID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- 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)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) ChannelDelete(channelID string) (st Channel, err error) {
- body, err := s.Request("DELETE", CHANNEL(channelID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) ChannelTyping(channelID string) (err error) {
- _, err = s.Request("POST", CHANNEL_TYPING(channelID), nil)
- return
- }
- func (s *Session) ChannelMessages(channelID string, limit int, beforeID int, afterID int) (st []Message, err error) {
- uri := CHANNEL_MESSAGES(channelID)
- v := url.Values{}
- if limit > 0 {
- v.Set("limit", strconv.Itoa(limit))
- }
- if afterID > 0 {
- v.Set("after", strconv.Itoa(afterID))
- }
- if beforeID > 0 {
- v.Set("before", strconv.Itoa(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 = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
- _, err = s.Request("POST", CHANNEL_MESSAGE_ACK(channelID, messageID), nil)
- return
- }
- func (s *Session) ChannelMessageSend(channelID string, content string) (st Message, err error) {
-
- data := struct {
- Content string `json:"content"`
- TTS bool `json:"tts"`
- }{content, false}
-
- response, err := s.Request("POST", CHANNEL_MESSAGES(channelID), data)
- err = json.Unmarshal(response, &st)
- return
- }
- 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)
- err = json.Unmarshal(response, &st)
- return
- }
- func (s *Session) ChannelMessageDelete(channelID, messageID string) (err error) {
- _, err = s.Request("DELETE", CHANNEL_MESSAGE(channelID, messageID), nil)
- return
- }
- func (s *Session) ChannelInvites(channelID string) (st []Invite, err error) {
- body, err := s.Request("GET", CHANNEL_INVITES(channelID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- 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 bool `json:"xkcdpass"`
- }{i.MaxAge, i.MaxUses, i.Temporary, i.XkcdPass}
- body, err := s.Request("POST", CHANNEL_INVITES(channelID), data)
- err = json.Unmarshal(body, &st)
- return
- }
- 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
- }
- func (s *Session) ChannelPermissionDelete(channelID, targetID string) (err error) {
- _, err = s.Request("DELETE", CHANNEL_PERMISSION(channelID, targetID), nil)
- return
- }
- func (s *Session) Invite(inviteID string) (st Invite, err error) {
- body, err := s.Request("GET", INVITE(inviteID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) InviteDelete(inviteID string) (st Invite, err error) {
- body, err := s.Request("DELETE", INVITE(inviteID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) InviteAccept(inviteID string) (st Invite, err error) {
- body, err := s.Request("POST", INVITE(inviteID), nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) VoiceRegions() (st []VoiceRegion, err error) {
- body, err := s.Request("GET", VOICE_REGIONS, nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) VoiceICE() (st VoiceICE, err error) {
- body, err := s.Request("GET", VOICE_ICE, nil)
- err = json.Unmarshal(body, &st)
- return
- }
- func (s *Session) Gateway() (gateway string, err error) {
- response, err := s.Request("GET", GATEWAY, nil)
- var temp map[string]interface{}
- err = json.Unmarshal(response, &temp)
- gateway = temp["url"].(string)
- return
- }
|