Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toggle for voice reconnect on session reconnect #1350

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 15 additions & 14 deletions discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type Session struct {
// Should the session reconnect the websocket on errors.
ShouldReconnectOnError bool

// Should voice connections reconnect on session websocket errors.
FedorLap2006 marked this conversation as resolved.
Show resolved Hide resolved
ShouldReconnectVoiceOnSessionError bool

// Should the session retry requests when rate limited.
ShouldRetryOnRateLimit bool

Expand Down
23 changes: 12 additions & 11 deletions wsapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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)

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)
}
}
return
}
Expand Down