forked from pothtonswer/discordmuffin
Implement guild sharding
This commit is contained in:
parent
ed47e30df3
commit
835a23a89d
3 changed files with 27 additions and 1 deletions
|
@ -40,6 +40,8 @@ func New(args ...interface{}) (s *Session, err error) {
|
|||
StateEnabled: true,
|
||||
Compress: true,
|
||||
ShouldReconnectOnError: true,
|
||||
ShardID: 0,
|
||||
NumShards: 1,
|
||||
}
|
||||
|
||||
// If no arguments are passed return the empty Session interface.
|
||||
|
|
|
@ -39,6 +39,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.
|
||||
|
|
22
wsapi.go
22
wsapi.go
|
@ -41,6 +41,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 {
|
||||
|
@ -74,6 +75,16 @@ func (s *Session) Open() (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
if s.NumShards <= 0 {
|
||||
err = errors.New("NumShards must be greater or equal to 1")
|
||||
return
|
||||
}
|
||||
|
||||
if s.ShardID >= s.NumShards {
|
||||
err = errors.New("ShardID must be less than NumShards")
|
||||
return
|
||||
}
|
||||
|
||||
if s.VoiceConnections == nil {
|
||||
s.log(LogInformational, "creating new VoiceConnections map")
|
||||
s.VoiceConnections = make(map[string]*VoiceConnection)
|
||||
|
@ -132,6 +143,7 @@ func (s *Session) Open() (err error) {
|
|||
},
|
||||
250,
|
||||
s.Compress,
|
||||
[2]int{s.ShardID, s.NumShards},
|
||||
},
|
||||
}
|
||||
s.log(LogInformational, "sending identify packet to gateway")
|
||||
|
@ -375,7 +387,15 @@ func (s *Session) onEvent(messageType int, message []byte) {
|
|||
|
||||
s.log(LogInformational, "sending identify packet to gateway in response to Op9")
|
||||
s.wsMutex.Lock()
|
||||
err = s.wsConn.WriteJSON(handshakeOp{2, handshakeData{s.Token, handshakeProperties{runtime.GOOS, "Discordgo v" + VERSION, "", "", ""}, 250, s.Compress}})
|
||||
err = s.wsConn.WriteJSON(handshakeOp{
|
||||
2,
|
||||
handshakeData{
|
||||
s.Token,
|
||||
handshakeProperties{runtime.GOOS, "Discordgo v" + VERSION, "", "", ""},
|
||||
250,
|
||||
s.Compress,
|
||||
[2]int{s.ShardID, s.NumShards},
|
||||
}})
|
||||
s.wsMutex.Unlock()
|
||||
if err != nil {
|
||||
s.log(LogWarning, "error sending gateway identify packet, %s, %s", s.gateway, err)
|
||||
|
|
Loading…
Reference in a new issue