From 03fa82b2a20eb44019b019c07e2dc814034fb48e Mon Sep 17 00:00:00 2001 From: Mitchell Gerber Date: Thu, 29 Jun 2017 23:52:23 -0500 Subject: [PATCH] allow sounds to be queued up - summon/dimiss now a thing --- config.template.json | 3 +- glide.lock | 36 ------ glide.yaml | 11 -- makefile | 2 +- readme.md | 2 +- server/bothandlers/sounds.go | 157 +++++++++++++++++-------- server/main.go | 8 +- server/webserver/handlers/soundlist.go | 5 +- server/webserver/handlers/upload.go | 5 +- server/webserver/server.go | 4 +- 10 files changed, 126 insertions(+), 107 deletions(-) delete mode 100644 glide.lock delete mode 100644 glide.yaml diff --git a/config.template.json b/config.template.json index ff5f56b..9c5d374 100644 --- a/config.template.json +++ b/config.template.json @@ -1,8 +1,7 @@ -// IMPORTANT - rename this file to config.json and remove this line { "Token": "", "BotPrefix": "#", "SoundsPath": "./sounds/", "UploadPassword": "", "ServerAddr": ":80" -} \ No newline at end of file +} diff --git a/glide.lock b/glide.lock deleted file mode 100644 index ad4122e..0000000 --- a/glide.lock +++ /dev/null @@ -1,36 +0,0 @@ -hash: aa2cc77713b57b79c4fc5bd35494fc4077e122137fdd68fa77aab8fdbc4215c8 -updated: 2017-06-28T22:24:45.223511-05:00 -imports: -- name: github.com/buaazp/fasthttprouter - version: ade4e2031af3aed7fffd241084aad80a58faf421 -- name: github.com/bwmarrin/discordgo - version: 0993a94b4e1c3291bed2047f583f34792269355c -- name: github.com/gorilla/websocket - version: ea4d1f681babbce9545c9c5f3d5194a789c89f5b -- name: github.com/karalabe/xgo - version: 34c7afab9a8eb8bff579e20bdbce9a764ce2a9ab -- name: github.com/klauspost/compress - version: f3dce52e0576655d55fd69e74b63da96ad1108f3 - subpackages: - - flate - - gzip - - zlib -- name: github.com/klauspost/cpuid - version: 09cded8978dc9e80714c4d85b0322337b0a1e5e0 -- name: github.com/tidwall/gjson - version: c784c417818f59d6597274642d8ac1d09efc9b01 -- name: github.com/tidwall/match - version: 173748da739a410c5b0b813b956f89ff94730b4c -- name: github.com/valyala/fasthttp - version: d42167fd04f636e20b005e9934159e95454233c7 - subpackages: - - fasthttputil -- name: golang.org/x/crypto - version: 84f24dfdf3c414ed893ca1b318d0045ef5a1f607 - subpackages: - - nacl/secretbox - - poly1305 - - salsa20/salsa -- name: layeh.com/gopus - version: 0ebf989153aa016ef5e77141b08c48e48a79312e -testImports: [] diff --git a/glide.yaml b/glide.yaml deleted file mode 100644 index 2ce2a0a..0000000 --- a/glide.yaml +++ /dev/null @@ -1,11 +0,0 @@ -package: . -import: -- package: github.com/buaazp/fasthttprouter - version: ^0.1.1 -- package: github.com/bwmarrin/discordgo - version: ^0.16.0 -- package: github.com/tidwall/gjson -- package: github.com/valyala/fasthttp - version: ^20160617.0.0 -- package: layeh.com/gopus -- package: github.com/karalabe/xgo diff --git a/makefile b/makefile index 07a03be..29ec847 100644 --- a/makefile +++ b/makefile @@ -2,7 +2,7 @@ run: go run ./server/main.go install: - glide install && yarn install + go get ./server && yarn install build: go build -o ./dist/bot ./server/main.go diff --git a/readme.md b/readme.md index 665174c..62bee67 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,7 @@ Sounds are stored in the `dist/sounds` directory. You may copy files directly to ### Dependencies - Go -- Glide - [GoLang package manager](https://glide.sh/) +- Godep - [Godep package manager](https://github.com/tools/godep) - Yarn (or npm - makefile will need to be adjusted) - make diff --git a/server/bothandlers/sounds.go b/server/bothandlers/sounds.go index 9dda99a..a1698d9 100644 --- a/server/bothandlers/sounds.go +++ b/server/bothandlers/sounds.go @@ -7,14 +7,15 @@ import ( "fmt" "io" "io/ioutil" + "log" "os/exec" "runtime" "strconv" "strings" "time" - "../config" "github.com/bwmarrin/discordgo" + "github.com/mgerb/go-discord-bot/server/config" "layeh.com/gopus" ) @@ -27,7 +28,9 @@ const ( var ( sounds = make(map[string]*AudioClip, 0) + soundQueue = []string{} soundPlayingLock = false + voiceConnection *discordgo.VoiceConnection ) type AudioClip struct { @@ -40,27 +43,35 @@ const SOUNDS_DIR string = "./sounds/" func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) { - // exit function call if sound is playing - if soundPlayingLock { - fmt.Println("Function in progress, exiting function call...") - return - } - // check if valid command if strings.HasPrefix(m.Content, config.Config.BotPrefix) { - soundName := strings.TrimPrefix(m.Content, config.Config.BotPrefix) + command := strings.TrimPrefix(m.Content, config.Config.BotPrefix) - // check if sound exists in memory - if _, ok := sounds[soundName]; !ok { - // try to load the sound if not found in memory - err := loadFile(soundName) + switch command { - if err != nil { - fmt.Println(err) - return - } + case "summon": + summon(s, m) + + case "dismiss": + dismiss() + + default: + playAudio(command, s, m) } + } +} + +func dismiss() { + if voiceConnection != nil { + voiceConnection.Disconnect() + } +} + +func summon(s *discordgo.Session, m *discordgo.MessageCreate) { + // Join the channel the user issued the command from if not in it + if voiceConnection == nil || voiceConnection.ChannelID != m.ChannelID { + var err error // Find the channel that the message came from. c, err := s.State.Channel(m.ChannelID) @@ -73,21 +84,73 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) { // Find the guild for that channel. g, err := s.State.Guild(c.GuildID) if err != nil { - // Could not find guild. + log.Println(err) return } // Look for the message sender in that guilds current voice states. for _, vs := range g.VoiceStates { if vs.UserID == m.Author.ID { - err = playSound(s, g.ID, vs.ChannelID, soundName) + + voiceConnection, err = s.ChannelVoiceJoin(g.ID, vs.ChannelID, false, false) + if err != nil { - fmt.Println("Error playing sound:", err) + log.Println(err) } return } } + + } +} + +func playAudio(soundName string, s *discordgo.Session, m *discordgo.MessageCreate) { + + // check if sound exists in memory + if _, ok := sounds[soundName]; !ok { + // try to load the sound if not found in memory + err := loadFile(soundName) + + if err != nil { + fmt.Println(err) + return + } + } + + // add sound to queue + soundQueue = append(soundQueue, soundName) + + // return if a sound is playing - it will play if it's in the queue + if soundPlayingLock { + 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 + } } } @@ -177,45 +240,47 @@ func loadFile(fileName string) error { sounds[fileName].Content = append(sounds[fileName].Content, opus) } - return nil } -// playSound plays the current buffer to the provided channel. -func playSound(s *discordgo.Session, guildID, channelID string, sound string) (err error) { - - if _, ok := sounds[sound]; !ok { - return errors.New("Sound not found") - } +// playSounds - plays the current buffer to the provided channel. +func playSounds(s *discordgo.Session, guildID, channelID string) (err error) { //prevent other sounds from interrupting soundPlayingLock = true - // Join the provided voice channel. - vc, err := s.ChannelVoiceJoin(guildID, channelID, false, false) - if err != nil { - return err + // Join the channel the user issued the command from if not in it + if voiceConnection == nil || voiceConnection.ChannelID != channelID { + var err error + voiceConnection, err = s.ChannelVoiceJoin(guildID, channelID, false, false) + if err != nil { + return err + } } - // Sleep for a specified amount of time before playing the sound - time.Sleep(100 * time.Millisecond) + // keep playing sounds as long as they exist in queue + for len(soundQueue) > 0 { - // Start speaking. - _ = vc.Speaking(true) + // Sleep for a specified amount of time before playing the sound + time.Sleep(50 * time.Millisecond) + + // Start speaking. + _ = voiceConnection.Speaking(true) + + // Send the buffer data. + for _, buff := range sounds[soundQueue[0]].Content { + voiceConnection.OpusSend <- buff + } + + // Stop speaking + _ = voiceConnection.Speaking(false) + + // Sleep for a specificed amount of time before ending. + time.Sleep(50 * time.Millisecond) + + soundQueue = append(soundQueue[1:]) - // Send the buffer data. - for _, buff := range sounds[sound].Content { - vc.OpusSend <- buff } - // Stop speaking - _ = vc.Speaking(false) - - // Sleep for a specificed amount of time before ending. - time.Sleep(250 * time.Millisecond) - - // Disconnect from the provided voice channel. - _ = vc.Disconnect() - soundPlayingLock = false return nil diff --git a/server/main.go b/server/main.go index aff8e26..3401111 100644 --- a/server/main.go +++ b/server/main.go @@ -1,10 +1,10 @@ package main import ( - "./bot" - "./bothandlers" - "./config" - "./webserver" + "github.com/mgerb/go-discord-bot/server/bot" + "github.com/mgerb/go-discord-bot/server/bothandlers" + "github.com/mgerb/go-discord-bot/server/config" + "github.com/mgerb/go-discord-bot/server/webserver" ) func main() { diff --git a/server/webserver/handlers/soundlist.go b/server/webserver/handlers/soundlist.go index 94121d4..5cdb025 100644 --- a/server/webserver/handlers/soundlist.go +++ b/server/webserver/handlers/soundlist.go @@ -1,12 +1,13 @@ package handlers import ( - "../../config" "encoding/json" "fmt" - "github.com/valyala/fasthttp" "io/ioutil" "strings" + + "github.com/mgerb/go-discord-bot/server/config" + "github.com/valyala/fasthttp" ) var soundList []sound diff --git a/server/webserver/handlers/upload.go b/server/webserver/handlers/upload.go index 2255fa2..29247f8 100644 --- a/server/webserver/handlers/upload.go +++ b/server/webserver/handlers/upload.go @@ -1,10 +1,11 @@ package handlers import ( - "../../config" - "github.com/valyala/fasthttp" "io" "os" + + "github.com/mgerb/go-discord-bot/server/config" + "github.com/valyala/fasthttp" ) func FileUpload(ctx *fasthttp.RequestCtx) { diff --git a/server/webserver/server.go b/server/webserver/server.go index aa0196e..c72a3af 100644 --- a/server/webserver/server.go +++ b/server/webserver/server.go @@ -3,9 +3,9 @@ package webserver import ( "log" - "../config" - "./handlers" "github.com/buaazp/fasthttprouter" + "github.com/mgerb/go-discord-bot/server/config" + "github.com/mgerb/go-discord-bot/server/webserver/handlers" "github.com/valyala/fasthttp" )