feat(ws): add toggle for voice reconnects on session reconnect (#1350)
* Add flag for voice reconnect on session reconnect * Rename ShouldReconnectVoiceConnOnError toggle as suggested Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com> * Fix indentaion for discord.go * Change wording in docs --------- Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com>
This commit is contained in:
parent
af63880a0b
commit
a90485df0c
3 changed files with 28 additions and 23 deletions
29
discord.go
29
discord.go
|
@ -33,20 +33,21 @@ func New(token string) (s *Session, err error) {
|
||||||
|
|
||||||
// Create an empty Session interface.
|
// Create an empty Session interface.
|
||||||
s = &Session{
|
s = &Session{
|
||||||
State: NewState(),
|
State: NewState(),
|
||||||
Ratelimiter: NewRatelimiter(),
|
Ratelimiter: NewRatelimiter(),
|
||||||
StateEnabled: true,
|
StateEnabled: true,
|
||||||
Compress: true,
|
Compress: true,
|
||||||
ShouldReconnectOnError: true,
|
ShouldReconnectOnError: true,
|
||||||
ShouldRetryOnRateLimit: true,
|
ShouldReconnectVoiceOnSessionError: true,
|
||||||
ShardID: 0,
|
ShouldRetryOnRateLimit: true,
|
||||||
ShardCount: 1,
|
ShardID: 0,
|
||||||
MaxRestRetries: 3,
|
ShardCount: 1,
|
||||||
Client: &http.Client{Timeout: (20 * time.Second)},
|
MaxRestRetries: 3,
|
||||||
Dialer: websocket.DefaultDialer,
|
Client: &http.Client{Timeout: (20 * time.Second)},
|
||||||
UserAgent: "DiscordBot (https://github.com/bwmarrin/discordgo, v" + VERSION + ")",
|
Dialer: websocket.DefaultDialer,
|
||||||
sequence: new(int64),
|
UserAgent: "DiscordBot (https://github.com/bwmarrin/discordgo, v" + VERSION + ")",
|
||||||
LastHeartbeatAck: time.Now().UTC(),
|
sequence: new(int64),
|
||||||
|
LastHeartbeatAck: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the Identify Package with defaults
|
// Initialize the Identify Package with defaults
|
||||||
|
|
|
@ -42,6 +42,9 @@ type Session struct {
|
||||||
// Should the session reconnect the websocket on errors.
|
// Should the session reconnect the websocket on errors.
|
||||||
ShouldReconnectOnError bool
|
ShouldReconnectOnError bool
|
||||||
|
|
||||||
|
// Should voice connections reconnect on a session reconnect.
|
||||||
|
ShouldReconnectVoiceOnSessionError bool
|
||||||
|
|
||||||
// Should the session retry requests when rate limited.
|
// Should the session retry requests when rate limited.
|
||||||
ShouldRetryOnRateLimit bool
|
ShouldRetryOnRateLimit bool
|
||||||
|
|
||||||
|
|
19
wsapi.go
19
wsapi.go
|
@ -862,17 +862,18 @@ func (s *Session) reconnect() {
|
||||||
// However, there seems to be cases where something "weird"
|
// However, there seems to be cases where something "weird"
|
||||||
// happens. So we're doing this for now just to improve
|
// happens. So we're doing this for now just to improve
|
||||||
// stability in those edge cases.
|
// stability in those edge cases.
|
||||||
s.RLock()
|
if s.ShouldReconnectVoiceOnSessionError {
|
||||||
defer s.RUnlock()
|
s.RLock()
|
||||||
for _, v := range s.VoiceConnections {
|
defer s.RUnlock()
|
||||||
|
for _, v := range s.VoiceConnections {
|
||||||
|
|
||||||
s.log(LogInformational, "reconnecting voice connection to guild %s", v.GuildID)
|
s.log(LogInformational, "reconnecting voice connection to guild %s", v.GuildID)
|
||||||
go v.reconnect()
|
go v.reconnect()
|
||||||
|
|
||||||
// This is here just to prevent violently spamming the
|
|
||||||
// voice reconnects
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
|
|
||||||
|
// This is here just to prevent violently spamming the
|
||||||
|
// voice reconnects
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue