From 4c287a55fc0c233ca0f5a3ca8c3349a600c4deeb Mon Sep 17 00:00:00 2001 From: Mitchell Date: Sun, 7 Oct 2018 19:28:54 -0500 Subject: [PATCH] feat: play random sound clip --- server/bothandlers/sounds.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/server/bothandlers/sounds.go b/server/bothandlers/sounds.go index f215a2c..c2cb118 100644 --- a/server/bothandlers/sounds.go +++ b/server/bothandlers/sounds.go @@ -7,6 +7,7 @@ import ( "errors" "io" "io/ioutil" + "math/rand" "os" "os/exec" "strconv" @@ -109,6 +110,9 @@ func (conn *AudioConnection) handleMessage(m *discordgo.MessageCreate) { case "clip": conn.clipAudio(m) + case "random": + conn.playRandomAudio(m) + default: conn.PlayAudio(command, m) } @@ -172,6 +176,18 @@ func (conn *AudioConnection) summon(m *discordgo.MessageCreate) { func (conn *AudioConnection) queueAudio(soundName string) { } +// play a random sound clip +func (conn *AudioConnection) playRandomAudio(m *discordgo.MessageCreate) { + files, _ := ioutil.ReadDir(config.Config.SoundsPath) + if len(files) > 0 { + randomIndex := rand.Intn(len(files)) + arr := strings.Split(files[randomIndex].Name(), ".") + if len(arr) > 0 && arr[0] != "" { + conn.PlayAudio(arr[0], m) + } + } +} + // PlayAudio - play audio in channel that user is in // if MessageCreate is null play in current channel func (conn *AudioConnection) PlayAudio(soundName string, m *discordgo.MessageCreate) {