1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-10 09:02:49 +00:00

fix bug where voice stops if bot is dismissed during sound clip

This commit is contained in:
2017-07-04 15:49:56 -05:00
parent ddbd24608f
commit b915ec065a
2 changed files with 22 additions and 6 deletions

2
.gitignore vendored
View File

@@ -5,3 +5,5 @@ yarn-error*
vendor vendor
bot bot
sounds sounds
debug

View File

@@ -12,6 +12,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"layeh.com/gopus" "layeh.com/gopus"
@@ -32,11 +33,13 @@ const (
var activeConnections = make(map[string]*audioConnection) var activeConnections = make(map[string]*audioConnection)
type audioConnection struct { type audioConnection struct {
guild *discordgo.Guild guild *discordgo.Guild
session *discordgo.Session session *discordgo.Session
sounds map[string]*audioClip sounds map[string]*audioClip
soundQueue chan string soundQueue chan string
voiceConnection *discordgo.VoiceConnection voiceConnection *discordgo.VoiceConnection
soundPlayingLock bool
mutex *sync.Mutex // mutex for single audio connection
} }
type audioClip struct { type audioClip struct {
@@ -72,6 +75,7 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
session: s, session: s,
sounds: make(map[string]*audioClip, 0), sounds: make(map[string]*audioClip, 0),
soundQueue: make(chan string, maxSoundQueue), soundQueue: make(chan string, maxSoundQueue),
mutex: &sync.Mutex{},
} }
activeConnections[c.GuildID] = newInstance activeConnections[c.GuildID] = newInstance
@@ -106,7 +110,7 @@ func (conn *audioConnection) handleMessage(m *discordgo.MessageCreate) {
} }
func (conn *audioConnection) dismiss() { func (conn *audioConnection) dismiss() {
if conn.voiceConnection != nil { if conn.voiceConnection != nil && !conn.soundPlayingLock && len(conn.soundQueue) == 0 {
conn.voiceConnection.Disconnect() conn.voiceConnection.Disconnect()
} }
} }
@@ -268,6 +272,8 @@ func (conn *audioConnection) playSounds() (err error) {
for { for {
newSoundName := <-conn.soundQueue newSoundName := <-conn.soundQueue
conn.toggleSoundPlayingLock(true)
if !conn.voiceConnection.Ready { if !conn.voiceConnection.Ready {
continue continue
} }
@@ -285,6 +291,14 @@ func (conn *audioConnection) playSounds() (err error) {
// Sleep for a specificed amount of time before ending. // Sleep for a specificed amount of time before ending.
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
conn.toggleSoundPlayingLock(false)
} }
} }
func (conn *audioConnection) toggleSoundPlayingLock(playing bool) {
conn.mutex.Lock()
conn.soundPlayingLock = playing
conn.mutex.Unlock()
}