From 92caf31b7f4f0931a9da602a7a4173b0762cae48 Mon Sep 17 00:00:00 2001 From: andrei Date: Wed, 13 Apr 2016 00:36:34 -0700 Subject: [PATCH] Implement Guild Sharding This implements the upcoming change (see hammerandchisel/discord-api-docs#17) to add guild-sharding support directly in the Discord Gateways. --- structs.go | 4 ++++ wsapi.go | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/structs.go b/structs.go index 7ca71a6..12e8f65 100644 --- a/structs.go +++ b/structs.go @@ -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. diff --git a/wsapi.go b/wsapi.go index 765b705..7ddc940 100644 --- a/wsapi.go +++ b/wsapi.go @@ -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 { @@ -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 }