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

Can't use VoiceConnection when the bot is moved to another vc by a User #849

Closed
colecrouter opened this issue Dec 12, 2020 · 2 comments · Fixed by #1271
Closed

Can't use VoiceConnection when the bot is moved to another vc by a User #849

colecrouter opened this issue Dec 12, 2020 · 2 comments · Fixed by #1271

Comments

@colecrouter
Copy link

Ran into a situation where when playing a sound, if the bot was moved to another voice channel, it seemingly gets stuck in limbo. Running Disconnect() just crashes. s.VoiceConnections[e.GuildID] seems to just flat out return nil every time (for me at least).

I was able to sorta get around this by storing the VoiceConnection* as a global, then setting a custom error handler for dgvoice:

dgvoice.OnError = func(str string, err error) {
	vc.Disconnect()
}

then setting up an event handler for voiceStateUpdate:

if e.UserID == "bot-id" {
	if e.BeforeUpdate != nil {
		vc, _ = s.ChannelVoiceJoin(e.GuildID, e.ChannelID, false, false)
		vc.Disconnect()
	}
	return
}

This also requires #833 in order to work, or some manual work keeping track of the bot's previous voice channel.

Basically, if it detects the bot being moved (e.BeforeUpdate != nil) then it reconnects to the same channel manually, bringing it out of "limbo". Then it just disconnects.

Obviously this isn't the end of the world, but it's a pretty rough fix. Does anyone have any better approaches? My ideas are:

  • Add some "fromAnotherChannel" property to VoiceStateUpdate, then it's at least a bit easier for users to define their wanted behavior
  • Add a separate "voiceChannelMoved` event to make it even a little bit more easy
  • Ideally, somehow have VoiceConnection* update itself when moved

Thoughts?

@colecrouter colecrouter changed the title Can't know if/when the bot is moved to another vc by a User Can't get voiceConnection when the bot is moved to another vc by a User Dec 12, 2020
@colecrouter colecrouter changed the title Can't get voiceConnection when the bot is moved to another vc by a User Can't use VoiceConnection when the bot is moved to another vc by a User Dec 12, 2020
@nonnonstop
Copy link
Contributor

I'm having same issue.

When I moved the bot to another VC, I got the following log.

...
2021/03/07 18:56:55 [DG2] voice.go:354:wsListen() received 4014 manual disconnection
2021/03/07 18:56:55 [DG2] voice.go:167:Close() called
2021/03/07 18:56:55 [DG2] voice.go:176:Close() closing v.close
2021/03/07 18:56:55 [DG2] voice.go:182:Close() closing udp
...

This log is output from voice.go

discordgo/voice.go

Lines 351 to 368 in 37088ae

// 4014 indicates a manual disconnection by someone in the guild;
// we shouldn't reconnect.
if websocket.IsCloseError(err, 4014) {
v.log(LogInformational, "received 4014 manual disconnection")
// Abandon the voice WS connection
v.Lock()
v.wsConn = nil
v.Unlock()
v.session.Lock()
delete(v.session.VoiceConnections, v.GuildID)
v.session.Unlock()
v.Close()
return
}

I found the same issue at Rapptz/discord.py#5904.
Maybe the connection check is still needed when the client receives 4014 error.

@qaisjp
Copy link
Contributor

qaisjp commented Mar 7, 2021

storing the VoiceConnection* as a global, then setting a custom error handler for dgvoice:

dgvoice.OnError = func(str string, err error) {
	vc.Disconnect()
}

Btw, as a general Go tip, you need not store it as a global.

If you have something like this:

type MyBot struct {
	d *discordgo.Session
	vc *discordgo.VoiceConnection
}

func (b *MyBot) onError(str string, err error) {
	b.vc.Disconnect()
}

You can assign a method "bound" to this struct like this:

func main() {
	bot := &MyBot{}
	// assign some things inside bot
	dgvoice.OnError = bot.onError
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants