From 9effc92908b341fbce4ad1e7e7f71f3cc6c1cec2 Mon Sep 17 00:00:00 2001 From: NonNonStop <42905588+nonnonstop@users.noreply.github.com> Date: Thu, 15 Dec 2022 09:17:27 +0900 Subject: [PATCH] Fix the problem of reconnecting when moved to another VC (#1271) * Fix disconnection when moved to another VC * Update voice.go Co-authored-by: Fedor Lapshin * Update voice.go Co-authored-by: Fedor Lapshin * Update voice.go Co-authored-by: Fedor Lapshin * feat: add VoiceServerUpdate todo Co-authored-by: Fedor Lapshin --- voice.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/voice.go b/voice.go index 04fa8ec..87e84b1 100644 --- a/voice.go +++ b/voice.go @@ -360,6 +360,25 @@ func (v *VoiceConnection) wsListen(wsConn *websocket.Conn, close <-chan struct{} v.wsConn = nil v.Unlock() + // Wait for VOICE_SERVER_UPDATE. + // When the bot is moved by the user to another voice channel, + // VOICE_SERVER_UPDATE is received after the code 4014. + for i := 0; i < 5; i++ { // TODO: temp, wait for VoiceServerUpdate. + <-time.After(1 * time.Second) + + v.RLock() + reconnected := v.wsConn != nil + v.RUnlock() + if !reconnected { + continue + } + v.log(LogInformational, "successfully reconnected after 4014 manual disconnection") + return + } + + // When VOICE_SERVER_UPDATE is not received, disconnect as usual. + v.log(LogInformational, "disconnect due to 4014 manual disconnection") + v.session.Lock() delete(v.session.VoiceConnections, v.GuildID) v.session.Unlock()