Browse Source

Updated to v6 (fixes #408) (#410)

* Updated to v6

* Unified websocket and REST version
legolord208 7 years ago
parent
commit
7bb0965a6f
5 changed files with 35 additions and 9 deletions
  1. 4 1
      endpoints.go
  2. 15 1
      message.go
  3. 2 2
      state.go
  4. 13 3
      structs.go
  5. 1 2
      wsapi.go

+ 4 - 1
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/"

+ 15 - 1
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
 		}
 

+ 2 - 2
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()
 

+ 13 - 3
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

+ 1 - 2
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{}