1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-11 17:42:48 +00:00

fix(bot handler): crash when trying to join invalid channel

This commit is contained in:
2018-06-19 22:00:01 -05:00
parent f369fce941
commit 0f4a8957a6

View File

@@ -77,7 +77,7 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
} }
// create new connection instance // create new connection instance
newInstance := &AudioConnection{ activeConnections[c.GuildID] = &AudioConnection{
Guild: newGuild, Guild: newGuild,
Session: s, Session: s,
Sounds: make(map[string]*AudioClip, 0), Sounds: make(map[string]*AudioClip, 0),
@@ -86,11 +86,6 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
AudioListenerLock: false, AudioListenerLock: false,
Disconnect: make(chan bool), Disconnect: make(chan bool),
} }
activeConnections[c.GuildID] = newInstance
// start listening on the sound channel
go activeConnections[c.GuildID].playSounds()
} }
// start new go routine handling the message // start new go routine handling the message
@@ -160,23 +155,38 @@ func (conn *AudioConnection) summon(m *discordgo.MessageCreate) {
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return
} }
// set the current channel // set the current channel
conn.CurrentChannel = c conn.CurrentChannel = c
go conn.watchForDisconnect()
// start go routine that plays sounds
go conn.playSounds()
// start listening to audio if not locked // start listening to audio if not locked
if !conn.AudioListenerLock { if !conn.AudioListenerLock {
go conn.startAudioListener() go conn.startAudioListener()
} }
return
} }
} }
} }
} }
// update disconnect channel when voice connection becomes unready
func (conn *AudioConnection) watchForDisconnect() {
for {
if !conn.VoiceConnection.Ready {
conn.Disconnect <- true
break
}
time.Sleep(5 * time.Second)
}
}
// play audio in channel that user is in // play audio in channel that user is in
func (conn *AudioConnection) playAudio(soundName string, m *discordgo.MessageCreate) { func (conn *AudioConnection) playAudio(soundName string, m *discordgo.MessageCreate) {
@@ -329,7 +339,6 @@ loop:
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }
} }
// start listening to the voice channel // start listening to the voice channel
@@ -341,16 +350,6 @@ func (conn *AudioConnection) startAudioListener() {
conn.VoiceClipQueue = make(chan *discordgo.Packet, voiceClipQueuePacketSize) conn.VoiceClipQueue = make(chan *discordgo.Packet, voiceClipQueuePacketSize)
} }
// exit loop if
go func() {
for {
if !conn.VoiceConnection.Ready {
conn.Disconnect <- true
}
time.Sleep(5 * time.Second)
}
}()
loop: loop:
for { for {
@@ -390,7 +389,6 @@ loop:
case <-conn.Disconnect: case <-conn.Disconnect:
break loop break loop
} }
} }
// remove lock upon exit // remove lock upon exit
@@ -401,8 +399,8 @@ loop:
func (conn *AudioConnection) playSounds() (err error) { func (conn *AudioConnection) playSounds() (err error) {
for { for {
newSoundName := <-conn.SoundQueue select {
case newSoundName := <-conn.SoundQueue:
conn.toggleSoundPlayingLock(true) conn.toggleSoundPlayingLock(true)
if !conn.VoiceConnection.Ready { if !conn.VoiceConnection.Ready {
@@ -424,8 +422,11 @@ func (conn *AudioConnection) playSounds() (err error) {
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
conn.toggleSoundPlayingLock(false) conn.toggleSoundPlayingLock(false)
}
case <-conn.Disconnect:
break
}
}
} }
func (conn *AudioConnection) toggleSoundPlayingLock(playing bool) { func (conn *AudioConnection) toggleSoundPlayingLock(playing bool) {
@@ -433,9 +434,3 @@ func (conn *AudioConnection) toggleSoundPlayingLock(playing bool) {
conn.SoundPlayingLock = playing conn.SoundPlayingLock = playing
conn.Mutex.Unlock() conn.Mutex.Unlock()
} }
func checkErr(err error) {
if err != nil {
log.Error(err)
}
}