From 7bb0965a6ff435b2587c2a4c50e3a0fedbf9325b Mon Sep 17 00:00:00 2001 From: legolord208 Date: Sat, 22 Jul 2017 15:17:39 +0200 Subject: [PATCH] Updated to v6 (fixes #408) (#410) * Updated to v6 * Unified websocket and REST version --- endpoints.go | 5 ++++- message.go | 16 +++++++++++++++- state.go | 4 ++-- structs.go | 16 +++++++++++++--- wsapi.go | 3 +-- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/endpoints.go b/endpoints.go index 0ecdf0b..06e3e9e 100644 --- a/endpoints.go +++ b/endpoints.go @@ -11,6 +11,9 @@ package discordgo +// The Discord API version used for the REST and Websocket API. +var ApiVersion = "6" + // Known Discord API Endpoints. var ( EndpointStatus = "https://status.discordapp.com/api/v2/" @@ -19,7 +22,7 @@ var ( EndpointSmUpcoming = EndpointSm + "upcoming.json" EndpointDiscord = "https://discordapp.com/" - EndpointAPI = EndpointDiscord + "api/" + EndpointAPI = EndpointDiscord + "api/v" + ApiVersion + "/" EndpointGuilds = EndpointAPI + "guilds/" EndpointChannels = EndpointAPI + "channels/" EndpointUsers = EndpointAPI + "users/" diff --git a/message.go b/message.go index d46f3f3..5232e83 100644 --- a/message.go +++ b/message.go @@ -15,6 +15,19 @@ import ( "strings" ) +type MessageType int + +const ( + MessageTypeDefault MessageType = iota + MessageTypeRecipientAdd + MessageTypeRecipientRemove + MessageTypeCall + MessageTypeChannelNameChange + MessageTypeChannelIconChange + MessageTypeChannelPinnedMessage + MessageTypeGuildMemberJoin +) + // A Message stores all data related to a specific Discord message. type Message struct { ID string `json:"id"` @@ -30,6 +43,7 @@ type Message struct { Embeds []*MessageEmbed `json:"embeds"` Mentions []*User `json:"mentions"` Reactions []*MessageReactions `json:"reactions"` + Type MessageType `json:"type"` } // File stores info about files you e.g. send in messages. @@ -226,7 +240,7 @@ func (m *Message) ContentWithMoreMentionsReplaced(s *Session) (content string, e content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string { channel, err := s.State.Channel(mention[2 : len(mention)-1]) - if err != nil || channel.Type == "voice" { + if err != nil || channel.Type == ChannelTypeGuildVoice { return mention } diff --git a/state.go b/state.go index 7400ef6..4ebfb1e 100644 --- a/state.go +++ b/state.go @@ -427,7 +427,7 @@ func (s *State) ChannelAdd(channel *Channel) error { return nil } - if channel.IsPrivate { + if channel.Type == ChannelTypeDM || channel.Type == ChannelTypeGroupDM { s.PrivateChannels = append(s.PrivateChannels, channel) } else { guild, ok := s.guildMap[channel.GuildID] @@ -454,7 +454,7 @@ func (s *State) ChannelRemove(channel *Channel) error { return err } - if channel.IsPrivate { + if channel.Type == ChannelTypeDM || channel.Type == ChannelTypeGroupDM { s.Lock() defer s.Unlock() diff --git a/structs.go b/structs.go index 9697fa5..d9f9699 100644 --- a/structs.go +++ b/structs.go @@ -144,18 +144,27 @@ type Invite struct { Temporary bool `json:"temporary"` } +type ChannelType int + +const ( + ChannelTypeGuildText ChannelType = iota + ChannelTypeDM + ChannelTypeGuildVoice + ChannelTypeGroupDM + ChannelTypeGuildCategory +) + // A Channel holds all data related to an individual Discord channel. type Channel struct { ID string `json:"id"` GuildID string `json:"guild_id"` Name string `json:"name"` Topic string `json:"topic"` - Type string `json:"type"` + Type ChannelType `json:"type"` LastMessageID string `json:"last_message_id"` Position int `json:"position"` Bitrate int `json:"bitrate"` - IsPrivate bool `json:"is_private"` - Recipient *User `json:"recipient"` + Recipients []*User `json:"recipient"` Messages []*Message `json:"-"` PermissionOverwrites []*PermissionOverwrite `json:"permission_overwrites"` } @@ -295,6 +304,7 @@ type Presence struct { Game *Game `json:"game"` Nick string `json:"nick"` Roles []string `json:"roles"` + Since *int `json:"since"` } // A Game struct holds the name of the "playing .." game for a user diff --git a/wsapi.go b/wsapi.go index 213ea72..d050ffd 100644 --- a/wsapi.go +++ b/wsapi.go @@ -15,7 +15,6 @@ import ( "compress/zlib" "encoding/json" "errors" - "fmt" "io" "net/http" "runtime" @@ -87,7 +86,7 @@ func (s *Session) Open() (err error) { } // Add the version and encoding to the URL - s.gateway = fmt.Sprintf("%s?v=5&encoding=json", s.gateway) + s.gateway = s.gateway + "?v=" + ApiVersion + "&encoding=json" } header := http.Header{}