diff --git a/discord.go b/discord.go index 5c0fea0..584e567 100644 --- a/discord.go +++ b/discord.go @@ -33,20 +33,21 @@ func New(token string) (s *Session, err error) { // Create an empty Session interface. s = &Session{ - State: NewState(), - Ratelimiter: NewRatelimiter(), - StateEnabled: true, - Compress: true, - ShouldReconnectOnError: true, - ShouldRetryOnRateLimit: true, - ShardID: 0, - ShardCount: 1, - MaxRestRetries: 3, - Client: &http.Client{Timeout: (20 * time.Second)}, - Dialer: websocket.DefaultDialer, - UserAgent: "DiscordBot (https://github.com/bwmarrin/discordgo, v" + VERSION + ")", - sequence: new(int64), - LastHeartbeatAck: time.Now().UTC(), + State: NewState(), + Ratelimiter: NewRatelimiter(), + StateEnabled: true, + Compress: true, + ShouldReconnectOnError: true, + ShouldReconnectVoiceOnSessionError: true, + ShouldRetryOnRateLimit: true, + ShardID: 0, + ShardCount: 1, + MaxRestRetries: 3, + Client: &http.Client{Timeout: (20 * time.Second)}, + Dialer: websocket.DefaultDialer, + UserAgent: "DiscordBot (https://github.com/bwmarrin/discordgo, v" + VERSION + ")", + sequence: new(int64), + LastHeartbeatAck: time.Now().UTC(), } // Initialize the Identify Package with defaults diff --git a/structs.go b/structs.go index 4b6ae5e..78fe4b2 100644 --- a/structs.go +++ b/structs.go @@ -42,6 +42,9 @@ type Session struct { // Should the session reconnect the websocket on errors. ShouldReconnectOnError bool + // Should voice connections reconnect on a session reconnect. + ShouldReconnectVoiceOnSessionError bool + // Should the session retry requests when rate limited. ShouldRetryOnRateLimit bool diff --git a/wsapi.go b/wsapi.go index 6c82388..9ef6dd0 100644 --- a/wsapi.go +++ b/wsapi.go @@ -862,17 +862,18 @@ func (s *Session) reconnect() { // However, there seems to be cases where something "weird" // happens. So we're doing this for now just to improve // stability in those edge cases. - s.RLock() - defer s.RUnlock() - for _, v := range s.VoiceConnections { + if s.ShouldReconnectVoiceOnSessionError { + s.RLock() + defer s.RUnlock() + for _, v := range s.VoiceConnections { - s.log(LogInformational, "reconnecting voice connection to guild %s", v.GuildID) - go v.reconnect() - - // This is here just to prevent violently spamming the - // voice reconnects - time.Sleep(1 * time.Second) + s.log(LogInformational, "reconnecting voice connection to guild %s", v.GuildID) + go v.reconnect() + // This is here just to prevent violently spamming the + // voice reconnects + time.Sleep(1 * time.Second) + } } return }