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 <fe.lap.prog@gmail.com>

* Update voice.go

Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com>

* Update voice.go

Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com>

* feat: add VoiceServerUpdate todo

Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com>
This commit is contained in:
NonNonStop 2022-12-15 09:17:27 +09:00 committed by GitHub
parent b8188269f9
commit 9effc92908
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -360,6 +360,25 @@ func (v *VoiceConnection) wsListen(wsConn *websocket.Conn, close <-chan struct{}
v.wsConn = nil v.wsConn = nil
v.Unlock() 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() v.session.Lock()
delete(v.session.VoiceConnections, v.GuildID) delete(v.session.VoiceConnections, v.GuildID)
v.session.Unlock() v.session.Unlock()