Skip to content

Commit

Permalink
Automagically reconnect if the VC is no longer in a ready state
Browse files Browse the repository at this point in the history
  • Loading branch information
TwiN committed Jun 17, 2020
1 parent 83f07bc commit 1264d63
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ func worker(bot *discordgo.Session, guildId, channelId string) error {
if err != nil {
return err
}
defer voice.Disconnect()
voice.Speaking(true)
defer voice.Speaking(false)
for media := range mediaQueues[guildId] {
if !voice.Ready {
log.Printf("[%s] VoiceConnection no longer in ready state, reconnecting", guildName)
voice, err = bot.ChannelVoiceJoin(guildId, channelId, false, true)
if err != nil {
return err
}
}
_ = voice.Speaking(true)
if !actionQueues[guildId].Stopped {
play(voice, media, guildName, actionQueues[guildId])
}
Expand All @@ -32,7 +37,9 @@ func worker(bot *discordgo.Session, guildId, channelId string) error {
log.Printf("[%s] There are currently %d medias in the queue", guildName, len(mediaQueues[guildId]))
// Wait a bit before playing the next song
time.Sleep(500 * time.Millisecond)
_ = voice.Speaking(false)
}
voice.Disconnect()
return nil
}

Expand Down Expand Up @@ -80,4 +87,5 @@ func play(voice *discordgo.VoiceConnection, media *core.Media, guildName string,
log.Printf("[%s] Stopping", guildName)
_ = encodeSession.Stop()
}
return
}

0 comments on commit 1264d63

Please sign in to comment.