discord-jukebox-bot/pkg/commands/setup.go

73 lines
No EOL
1.7 KiB
Go

package commands
import (
"fmt"
"sync"
"discord-jukebox-bot/pkg/config"
"discord-jukebox-bot/pkg/discord"
"discord-jukebox-bot/pkg/subsonic"
"github.com/bwmarrin/discordgo"
)
// Store a reference to the jukebox player for external access
var (
jukeboxPlayer *discord.JukeboxPlayer
jukeboxPlayerLock sync.RWMutex
)
// Setup sets up all commands for the Discord bot
func Setup(cfg *config.Config) (*discord.Bot, error) {
// Create the Subsonic client
subsonicClient := subsonic.New(
cfg.SubsonicServer,
cfg.SubsonicUsername,
cfg.SubsonicPassword,
cfg.SubsonicVersion,
cfg.GetTimeout(),
)
// Create the Discord bot
bot, err := discord.New(cfg, subsonicClient)
if err != nil {
return nil, fmt.Errorf("error creating bot: %w", err)
}
// Initialize the jukebox player
jukebox := discord.NewJukeboxPlayer(bot)
if jukebox == nil {
return nil, fmt.Errorf("error creating jukebox player")
}
// Store the jukebox player reference
jukeboxPlayerLock.Lock()
jukeboxPlayer = jukebox
jukeboxPlayerLock.Unlock()
// Add any additional command initialization here
// This is where you can easily add new commands in the future
return bot, nil
}
// RegisterCustomCommand is a helper function to register a new command
// This makes it easy to add new commands in the future
func RegisterCustomCommand(
bot *discord.Bot,
name string,
description string,
options []*discordgo.ApplicationCommandOption,
handler func(s *discordgo.Session, i *discordgo.InteractionCreate),
) error {
// Register the command handler
bot.RegisterCommand(name, handler)
return nil
}
// GetJukebox returns the current jukebox player instance
func GetJukebox() *discord.JukeboxPlayer {
jukeboxPlayerLock.RLock()
defer jukeboxPlayerLock.RUnlock()
return jukeboxPlayer
}