1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-09 08:32:48 +00:00

feat: play random sound clip

This commit is contained in:
2018-10-07 19:28:54 -05:00
parent 4a2204bbe7
commit 4c287a55fc

View File

@@ -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) {