Skip to content

Commit

Permalink
Fix issue with bot getting stuck on worker error
Browse files Browse the repository at this point in the history
  • Loading branch information
TwiN committed Jun 15, 2020
1 parent 2f255fe commit 83f07bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func HandleYoutubeCommand(bot *discordgo.Session, message *discordgo.MessageCrea
err = worker(bot, message.GuildID, voiceChannelId)
if err != nil {
log.Printf("[%s] Failed to start worker: %s", guildName, err.Error())
_, _ = bot.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Unable to start voice worker: %s", err.Error()))
_, _ = bot.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Unable to start voice worker: %s", err.Error()))
_ = os.Remove(media.FilePath)
return
}
Expand Down
17 changes: 13 additions & 4 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

func worker(bot *discordgo.Session, guildId, channelId string) error {
guildName := GetGuildNameById(bot, guildId)
defer cleanUpGuildWorker(guildName, guildId)
// See https://github.com/Malchemy/DankMemes/blob/master/sound.go#L26
voice, err := bot.ChannelVoiceJoin(guildId, channelId, false, true)
if err != nil {
Expand All @@ -29,14 +30,22 @@ func worker(bot *discordgo.Session, guildId, channelId string) error {
break
}
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)
}
time.Sleep(500 * time.Millisecond)
return nil
}

log.Printf("[%s] Closing channel", guildName)
func cleanUpGuildWorker(guildName, guildId string) {
log.Printf("[%s] Cleaning up before destroying worker", guildName)
actionQueuesMutex.Lock()
actionQueues[guildId] = nil
actionQueuesMutex.Unlock()
mediaQueuesMutex.Lock()
close(mediaQueues[guildId])
mediaQueues[guildId] = nil
actionQueues[guildId] = nil
return nil
mediaQueuesMutex.Unlock()
log.Printf("[%s] Cleaned up all channels successfully", guildName)
}

func play(voice *discordgo.VoiceConnection, media *core.Media, guildName string, actions *core.Actions) {
Expand Down

0 comments on commit 83f07bc

Please sign in to comment.