mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-13 10:32:47 +00:00
major file refactor - fix disconnecting bug
This commit is contained in:
@@ -26,23 +26,50 @@ const (
|
||||
maxBytes int = (frameSize * 2) * 2 // max size of opus data
|
||||
)
|
||||
|
||||
// store our connection objects in a map tied to a guild id
|
||||
var activeConnections = make(map[string]*audioConnection)
|
||||
|
||||
type audioConnection struct {
|
||||
guild *discordgo.Guild
|
||||
sounds map[string]*audioClip
|
||||
soundQueue chan string
|
||||
voiceConnection *discordgo.VoiceConnection
|
||||
}
|
||||
|
||||
var (
|
||||
sounds = make(map[string]*AudioClip, 0)
|
||||
sounds = make(map[string]*audioClip, 0)
|
||||
soundQueue = []string{}
|
||||
soundPlayingLock = false
|
||||
voiceConnection *discordgo.VoiceConnection
|
||||
)
|
||||
|
||||
type AudioClip struct {
|
||||
type audioClip struct {
|
||||
Name string
|
||||
Extension string
|
||||
Content [][]byte
|
||||
}
|
||||
|
||||
const SOUNDS_DIR string = "./sounds/"
|
||||
|
||||
// SoundsHandler -
|
||||
func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
|
||||
// get guild ID and check for connection instance
|
||||
c, err := s.State.Channel(m.ChannelID)
|
||||
if err != nil {
|
||||
// Could not find channel.
|
||||
fmt.Println("Unable to find channel.")
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := activeConnections[c.GuildID]; !ok {
|
||||
newConnectionInstance, err := getNewConnectionInstance(s, m)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
activeConnections[c.GuildID] = newConnectionInstance
|
||||
}
|
||||
|
||||
// check if valid command
|
||||
if strings.HasPrefix(m.Content, config.Config.BotPrefix) {
|
||||
|
||||
@@ -62,6 +89,10 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
}
|
||||
}
|
||||
|
||||
func getNewConnectionInstance(s *discordgo.Session, m *discordgo.MessageCreate) (*audioConnection, error) {
|
||||
return &audioConnection{}, nil
|
||||
}
|
||||
|
||||
func dismiss() {
|
||||
if voiceConnection != nil {
|
||||
voiceConnection.Disconnect()
|
||||
@@ -213,7 +244,7 @@ func loadFile(fileName string) error {
|
||||
return errors.New("NewEncoder error.")
|
||||
}
|
||||
|
||||
sounds[fileName] = &AudioClip{
|
||||
sounds[fileName] = &audioClip{
|
||||
Content: make([][]byte, 0),
|
||||
Name: fileName,
|
||||
Extension: fextension,
|
||||
@@ -249,7 +280,7 @@ func playSounds(s *discordgo.Session, guildID, channelID string) (err error) {
|
||||
soundPlayingLock = true
|
||||
|
||||
// Join the channel the user issued the command from if not in it
|
||||
if voiceConnection == nil || voiceConnection.ChannelID != channelID {
|
||||
if voiceConnection == nil || !voiceConnection.Ready {
|
||||
var err error
|
||||
voiceConnection, err = s.ChannelVoiceJoin(guildID, channelID, false, false)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"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() {
|
||||
//read config file
|
||||
config.Init()
|
||||
|
||||
//connect bot to account with token
|
||||
bot.Connect(config.Config.Token)
|
||||
|
||||
//add handlers
|
||||
bot.AddHandler(bothandlers.SoundsHandler)
|
||||
bot.AddHandler(bothandlers.GifHandler)
|
||||
|
||||
// start new go routine for the discord websockets
|
||||
go bot.Start()
|
||||
|
||||
// start the web server
|
||||
webserver.Start()
|
||||
}
|
||||
@@ -28,11 +28,11 @@ func registerRoutes(router *fasthttprouter.Router) {
|
||||
router.GET("/soundlist", handlers.SoundList)
|
||||
router.PUT("/upload", handlers.FileUpload)
|
||||
|
||||
router.ServeFiles("/static/*filepath", "./static")
|
||||
router.ServeFiles("/static/*filepath", "./dist/static")
|
||||
router.ServeFiles("/sounds/*filepath", config.Config.SoundsPath)
|
||||
|
||||
router.NotFound = func(ctx *fasthttp.RequestCtx) {
|
||||
fasthttp.ServeFile(ctx, "./index.html")
|
||||
fasthttp.ServeFile(ctx, "./dist/index.html")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user