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

allow sounds to be queued up - summon/dimiss now a thing

This commit is contained in:
2017-06-29 23:52:23 -05:00
parent 55bbf7cc1f
commit 03fa82b2a2
10 changed files with 126 additions and 107 deletions

View File

@@ -1,4 +1,3 @@
// IMPORTANT - rename this file to config.json and remove this line
{
"Token": "",
"BotPrefix": "#",

36
glide.lock generated
View File

@@ -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: []

View File

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

View File

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

View File

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

View File

@@ -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,16 +43,69 @@ 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)
switch command {
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)
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 {
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 {
voiceConnection, err = s.ChannelVoiceJoin(g.ID, vs.ChannelID, false, false)
if err != nil {
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 {
@@ -62,6 +118,14 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
}
}
// 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 {
@@ -80,7 +144,7 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// 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)
err = playSounds(s, g.ID, vs.ChannelID)
if err != nil {
fmt.Println("Error playing sound:", err)
}
@@ -88,7 +152,6 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
return
}
}
}
}
// load dca file into memory
@@ -177,44 +240,46 @@ 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)
// 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
}
}
// 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(100 * time.Millisecond)
time.Sleep(50 * time.Millisecond)
// Start speaking.
_ = vc.Speaking(true)
_ = voiceConnection.Speaking(true)
// Send the buffer data.
for _, buff := range sounds[sound].Content {
vc.OpusSend <- buff
for _, buff := range sounds[soundQueue[0]].Content {
voiceConnection.OpusSend <- buff
}
// Stop speaking
_ = vc.Speaking(false)
_ = voiceConnection.Speaking(false)
// Sleep for a specificed amount of time before ending.
time.Sleep(250 * time.Millisecond)
time.Sleep(50 * time.Millisecond)
// Disconnect from the provided voice channel.
_ = vc.Disconnect()
soundQueue = append(soundQueue[1:])
}
soundPlayingLock = false

View File

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

View File

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

View File

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

View File

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