mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-10 09:02:49 +00:00
refactor sounds handler - now uses channels for queue
This commit is contained in:
3
main.go
3
main.go
@@ -16,7 +16,8 @@ func main() {
|
|||||||
|
|
||||||
//add handlers
|
//add handlers
|
||||||
bot.AddHandler(bothandlers.SoundsHandler)
|
bot.AddHandler(bothandlers.SoundsHandler)
|
||||||
bot.AddHandler(bothandlers.GifHandler)
|
// remove gif functionality for not
|
||||||
|
//bot.AddHandler(bothandlers.GifHandler)
|
||||||
|
|
||||||
// start new go routine for the discord websockets
|
// start new go routine for the discord websockets
|
||||||
go bot.Start()
|
go bot.Start()
|
||||||
|
|||||||
@@ -14,16 +14,18 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"layeh.com/gopus"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/mgerb/go-discord-bot/server/config"
|
"github.com/mgerb/go-discord-bot/server/config"
|
||||||
"layeh.com/gopus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
channels int = 2 // 1 for mono, 2 for stereo
|
channels int = 2 // 1 for mono, 2 for stereo
|
||||||
frameRate int = 48000 // audio sampling rate
|
frameRate int = 48000 // audio sampling rate
|
||||||
frameSize int = 960 // uint16 size of each audio frame
|
frameSize int = 960 // uint16 size of each audio frame
|
||||||
maxBytes int = (frameSize * 2) * 2 // max size of opus data
|
maxBytes int = (frameSize * 2) * 2 // max size of opus data
|
||||||
|
maxSoundQueue int = 10 // max amount of sounds that can be queued at one time
|
||||||
)
|
)
|
||||||
|
|
||||||
// store our connection objects in a map tied to a guild id
|
// store our connection objects in a map tied to a guild id
|
||||||
@@ -31,18 +33,12 @@ var activeConnections = make(map[string]*audioConnection)
|
|||||||
|
|
||||||
type audioConnection struct {
|
type audioConnection struct {
|
||||||
guild *discordgo.Guild
|
guild *discordgo.Guild
|
||||||
|
session *discordgo.Session
|
||||||
sounds map[string]*audioClip
|
sounds map[string]*audioClip
|
||||||
soundQueue chan string
|
soundQueue chan string
|
||||||
voiceConnection *discordgo.VoiceConnection
|
voiceConnection *discordgo.VoiceConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
sounds = make(map[string]*audioClip, 0)
|
|
||||||
soundQueue = []string{}
|
|
||||||
soundPlayingLock = false
|
|
||||||
voiceConnection *discordgo.VoiceConnection
|
|
||||||
)
|
|
||||||
|
|
||||||
type audioClip struct {
|
type audioClip struct {
|
||||||
Name string
|
Name string
|
||||||
Extension string
|
Extension string
|
||||||
@@ -52,7 +48,7 @@ type audioClip struct {
|
|||||||
// SoundsHandler -
|
// SoundsHandler -
|
||||||
func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
// get guild ID and check for connection instance
|
// get channel state to get guild id
|
||||||
c, err := s.State.Channel(m.ChannelID)
|
c, err := s.State.Channel(m.ChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Could not find channel.
|
// Could not find channel.
|
||||||
@@ -60,16 +56,36 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check to see if active connection object exists
|
||||||
if _, ok := activeConnections[c.GuildID]; !ok {
|
if _, ok := activeConnections[c.GuildID]; !ok {
|
||||||
newConnectionInstance, err := getNewConnectionInstance(s, m)
|
|
||||||
|
// Find the guild for that channel.
|
||||||
|
newGuild, err := s.State.Guild(c.GuildID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
activeConnections[c.GuildID] = newConnectionInstance
|
// create new connection instance
|
||||||
|
newInstance := &audioConnection{
|
||||||
|
guild: newGuild,
|
||||||
|
session: s,
|
||||||
|
sounds: make(map[string]*audioClip, 0),
|
||||||
|
soundQueue: make(chan string, maxSoundQueue),
|
||||||
|
}
|
||||||
|
|
||||||
|
activeConnections[c.GuildID] = newInstance
|
||||||
|
|
||||||
|
// start listening on the sound channel
|
||||||
|
go activeConnections[c.GuildID].playSounds()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// start new go routine handling the message
|
||||||
|
go activeConnections[c.GuildID].handleMessage(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *audioConnection) handleMessage(m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
// check if valid command
|
// check if valid command
|
||||||
if strings.HasPrefix(m.Content, config.Config.BotPrefix) {
|
if strings.HasPrefix(m.Content, config.Config.BotPrefix) {
|
||||||
|
|
||||||
@@ -78,34 +94,30 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
switch command {
|
switch command {
|
||||||
|
|
||||||
case "summon":
|
case "summon":
|
||||||
summon(s, m)
|
conn.summon(m)
|
||||||
|
|
||||||
case "dismiss":
|
case "dismiss":
|
||||||
dismiss()
|
conn.dismiss()
|
||||||
|
|
||||||
default:
|
default:
|
||||||
playAudio(command, s, m)
|
conn.playAudio(command, m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNewConnectionInstance(s *discordgo.Session, m *discordgo.MessageCreate) (*audioConnection, error) {
|
func (conn *audioConnection) dismiss() {
|
||||||
return &audioConnection{}, nil
|
if conn.voiceConnection != nil {
|
||||||
}
|
conn.voiceConnection.Disconnect()
|
||||||
|
|
||||||
func dismiss() {
|
|
||||||
if voiceConnection != nil {
|
|
||||||
voiceConnection.Disconnect()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func summon(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func (conn *audioConnection) summon(m *discordgo.MessageCreate) {
|
||||||
// Join the channel the user issued the command from if not in it
|
// Join the channel the user issued the command from if not in it
|
||||||
if voiceConnection == nil || voiceConnection.ChannelID != m.ChannelID {
|
if conn.voiceConnection == nil || conn.voiceConnection.ChannelID != m.ChannelID {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Find the channel that the message came from.
|
// Find the channel that the message came from.
|
||||||
c, err := s.State.Channel(m.ChannelID)
|
c, err := conn.session.State.Channel(m.ChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Could not find channel.
|
// Could not find channel.
|
||||||
fmt.Println("User channel not found.")
|
fmt.Println("User channel not found.")
|
||||||
@@ -113,7 +125,7 @@ func summon(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find the guild for that channel.
|
// Find the guild for that channel.
|
||||||
g, err := s.State.Guild(c.GuildID)
|
g, err := conn.session.State.Guild(c.GuildID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
@@ -123,7 +135,7 @@ func summon(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
for _, vs := range g.VoiceStates {
|
for _, vs := range g.VoiceStates {
|
||||||
if vs.UserID == m.Author.ID {
|
if vs.UserID == m.Author.ID {
|
||||||
|
|
||||||
voiceConnection, err = s.ChannelVoiceJoin(g.ID, vs.ChannelID, false, false)
|
conn.voiceConnection, err = conn.session.ChannelVoiceJoin(g.ID, vs.ChannelID, false, false)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@@ -136,12 +148,12 @@ func summon(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func playAudio(soundName string, s *discordgo.Session, m *discordgo.MessageCreate) {
|
func (conn *audioConnection) playAudio(soundName string, m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
// check if sound exists in memory
|
// check if sound exists in memory
|
||||||
if _, ok := sounds[soundName]; !ok {
|
if _, ok := conn.sounds[soundName]; !ok {
|
||||||
// try to load the sound if not found in memory
|
// try to load the sound if not found in memory
|
||||||
err := loadFile(soundName)
|
err := conn.loadFile(soundName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -149,44 +161,21 @@ func playAudio(soundName string, s *discordgo.Session, m *discordgo.MessageCreat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add sound to queue
|
// summon bot to channel
|
||||||
soundQueue = append(soundQueue, soundName)
|
conn.summon(m)
|
||||||
|
|
||||||
// return if a sound is playing - it will play if it's in the queue
|
// add sound to queue if queue isn't full
|
||||||
if soundPlayingLock {
|
select {
|
||||||
|
case conn.soundQueue <- soundName:
|
||||||
|
|
||||||
|
default:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the channel that the message came from.
|
|
||||||
c, err := s.State.Channel(m.ChannelID)
|
|
||||||
if err != nil {
|
|
||||||
// Could not find channel.
|
|
||||||
fmt.Println("User channel not found.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the guild for that channel.
|
|
||||||
g, err := s.State.Guild(c.GuildID)
|
|
||||||
if err != nil {
|
|
||||||
// Could not find guild.
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look for the message sender in that guilds current voice states.
|
|
||||||
for _, vs := range g.VoiceStates {
|
|
||||||
if vs.UserID == m.Author.ID {
|
|
||||||
err = playSounds(s, g.ID, vs.ChannelID)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error playing sound:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// load dca file into memory
|
// load dca file into memory
|
||||||
func loadFile(fileName string) error {
|
func (conn *audioConnection) loadFile(fileName string) error {
|
||||||
|
|
||||||
// scan directory for file
|
// scan directory for file
|
||||||
files, _ := ioutil.ReadDir(config.Config.SoundsPath)
|
files, _ := ioutil.ReadDir(config.Config.SoundsPath)
|
||||||
@@ -244,7 +233,7 @@ func loadFile(fileName string) error {
|
|||||||
return errors.New("NewEncoder error.")
|
return errors.New("NewEncoder error.")
|
||||||
}
|
}
|
||||||
|
|
||||||
sounds[fileName] = &audioClip{
|
conn.sounds[fileName] = &audioClip{
|
||||||
Content: make([][]byte, 0),
|
Content: make([][]byte, 0),
|
||||||
Name: fileName,
|
Name: fileName,
|
||||||
Extension: fextension,
|
Extension: fextension,
|
||||||
@@ -268,51 +257,34 @@ func loadFile(fileName string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// append sound bytes to the content for this audio file
|
// append sound bytes to the content for this audio file
|
||||||
sounds[fileName].Content = append(sounds[fileName].Content, opus)
|
conn.sounds[fileName].Content = append(conn.sounds[fileName].Content, opus)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// playSounds - plays the current buffer to the provided channel.
|
// playSounds - plays the current buffer to the provided channel.
|
||||||
func playSounds(s *discordgo.Session, guildID, channelID string) (err error) {
|
func (conn *audioConnection) playSounds() (err error) {
|
||||||
|
|
||||||
//prevent other sounds from interrupting
|
for {
|
||||||
soundPlayingLock = true
|
newSoundName := <-conn.soundQueue
|
||||||
|
|
||||||
// Join the channel the user issued the command from if not in it
|
if !conn.voiceConnection.Ready {
|
||||||
if voiceConnection == nil || !voiceConnection.Ready {
|
continue
|
||||||
var err error
|
|
||||||
voiceConnection, err = s.ChannelVoiceJoin(guildID, channelID, false, false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// keep playing sounds as long as they exist in queue
|
|
||||||
for len(soundQueue) > 0 {
|
|
||||||
|
|
||||||
// Sleep for a specified amount of time before playing the sound
|
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
|
|
||||||
// Start speaking.
|
// Start speaking.
|
||||||
_ = voiceConnection.Speaking(true)
|
_ = conn.voiceConnection.Speaking(true)
|
||||||
|
|
||||||
// Send the buffer data.
|
// Send the buffer data.
|
||||||
for _, buff := range sounds[soundQueue[0]].Content {
|
for _, buff := range conn.sounds[newSoundName].Content {
|
||||||
voiceConnection.OpusSend <- buff
|
conn.voiceConnection.OpusSend <- buff
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop speaking
|
// Stop speaking
|
||||||
_ = voiceConnection.Speaking(false)
|
_ = conn.voiceConnection.Speaking(false)
|
||||||
|
|
||||||
// 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)
|
||||||
|
|
||||||
soundQueue = append(soundQueue[1:])
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
soundPlayingLock = false
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user