Skip to content

Commit

Permalink
Implement Guild Sharding
Browse files Browse the repository at this point in the history
This implements the upcoming change (see discord/discord-api-docs#17)
to add guild-sharding support directly in the Discord Gateways.
  • Loading branch information
b1naryth1ef committed Apr 20, 2016
1 parent 84af3ca commit 259804d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type Session struct {
// Should the session request compressed websocket data.
Compress bool

// Sharding
ShardID int
NumShards int

// Should state tracking be enabled.
// State tracking is the best way for getting the the users
// active guilds and the members of the guilds.
Expand Down
16 changes: 15 additions & 1 deletion wsapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type handshakeData struct {
Properties handshakeProperties `json:"properties"`
LargeThreshold int `json:"large_threshold"`
Compress bool `json:"compress"`
Shard [2]int `json:"shard"`
}

type handshakeOp struct {
Expand Down Expand Up @@ -78,7 +79,20 @@ func (s *Session) Open() (err error) {
return
}

err = s.wsConn.WriteJSON(handshakeOp{2, handshakeData{3, s.Token, handshakeProperties{runtime.GOOS, "Discordgo v" + VERSION, "", "", ""}, 250, s.Compress}})
handshake := handshakeData{
Version: 4,
Token: s.Token,
Properties: handshakeProperties{runtime.GOOS, "Discordgo v" + VERSION, "", "", ""},
LargeThreshold: 250,
Compress: s.Compress,
}

// If we've set NumShards, add the shard information to the handshake
if s.NumShards > 0 {
handshake.Shard = [2]int{s.ShardID, s.NumShards}
}

err = s.wsConn.WriteJSON(handshakeOp{2, handshake})
if err != nil {
return
}
Expand Down

0 comments on commit 259804d

Please sign in to comment.