diff options
| author | Bobby <[email protected]> | 2025-03-27 12:48:49 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-03-27 12:48:49 +0530 |
| commit | 1b271061415b33a8f18d1d3d960bc750b9557b69 (patch) | |
| tree | f14bd9b483d849f454f10a9111049e5c191a5154 | |
| parent | 3a63a1a8be65f3e50141a3970a9d63d91b191755 (diff) | |
| download | ai-1b271061415b33a8f18d1d3d960bc750b9557b69.tar.xz ai-1b271061415b33a8f18d1d3d960bc750b9557b69.zip | |
command handler and boilerplate play command
| -rw-r--r-- | .env.example | 1 | ||||
| -rw-r--r-- | ai/main.go | 16 | ||||
| -rw-r--r-- | commands/commands.go | 20 | ||||
| -rw-r--r-- | commands/play.go | 24 | ||||
| -rw-r--r-- | config/config.go | 12 | ||||
| -rw-r--r-- | handlers/interactionCreateHandler.go | 12 | ||||
| -rw-r--r-- | handlers/slashCommandHandler.go | 13 | ||||
| -rw-r--r-- | types/bot.go | 1 |
8 files changed, 95 insertions, 4 deletions
diff --git a/.env.example b/.env.example index 1e276ba..1175596 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ +GUILD_ID= DISCORD_TOKEN= YOUTUBE_API_KEY= SPOTIFY_CLIENT_ID= @@ -1,9 +1,12 @@ package main import ( + "ai/commands" "ai/config" + "ai/handlers" "ai/types" "ai/utils/logger" + "fmt" "os" "os/signal" "syscall" @@ -28,6 +31,7 @@ func init() { session.Identify.Intents |= discordgo.IntentsAllWithoutPrivileged session.AddHandler(ready) + session.AddHandler(handlers.InteractionCreateHandler) } func main() { @@ -36,6 +40,18 @@ func main() { logger.Log("error opening connection,", types.LogOptions{Fatal: true, Prefix: ProcessPrefix, Level: types.Error}) } + logger.Log("Registering commands with Discord API.", types.LogOptions{Prefix: ProcessPrefix}) + + // Register commands with Discord API + registeredCommands, err := session.ApplicationCommandBulkOverwrite(session.State.User.ID, config.Config.GuildID, commands.Commands) + if err != nil { + logger.Log("Error registering commands with Discord API.", types.LogOptions{Prefix: ProcessPrefix, Level: types.Error, Fatal: true}) + } + + for _, command := range registeredCommands { + logger.Log(fmt.Sprintf("Registered command: %s", command.Name), types.LogOptions{Prefix: ProcessPrefix, Level: types.Success}) + } + // Wait here until CTRL-C or other term signal is received. logger.Log("Bot is now running. Press CTRL-C to exit.", types.LogOptions{Prefix: ProcessPrefix}) defer session.Close() diff --git a/commands/commands.go b/commands/commands.go new file mode 100644 index 0000000..53dc3dc --- /dev/null +++ b/commands/commands.go @@ -0,0 +1,20 @@ +package commands + +import "github.com/bwmarrin/discordgo" + +var ( + Commands = []*discordgo.ApplicationCommand{ + { + Name: "play", + Description: "Search and play a song from Spotify or YouTube", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "query", + Description: "Search query for the song/playlist (or URL)", + Required: true, + }, + }, + }, + } +) diff --git a/commands/play.go b/commands/play.go new file mode 100644 index 0000000..6c7fae4 --- /dev/null +++ b/commands/play.go @@ -0,0 +1,24 @@ +package commands + +import ( + "fmt" + + "github.com/bwmarrin/discordgo" +) + +// Command: Play +// Takes in a search query and autocompletes the search query to play a song from Spotify or YouTube +// Also takes in a URL to a Spotify Song/Playlist or YouTube Video/Playlist +// Joins the user's voice channel and plays the song. Adds to the queue if a song is already playing +// in any of the voice channels. User must be in a voice channel to use this command +// Usage: /play <search query or URL> +func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { + // For now return the query as reply + reply := fmt.Sprintf("Playing: %s", i.ApplicationCommandData().Options[0].StringValue()) + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: reply, + }, + }) +} diff --git a/config/config.go b/config/config.go index e3fc5e4..cfc9ef7 100644 --- a/config/config.go +++ b/config/config.go @@ -15,10 +15,9 @@ var Config *types.BotConfig func init() { logPrefix := "Config" logOptions := types.LogOptions{ - Timestamp: true, - Prefix: logPrefix, - Level: types.Error, - Fatal: true, + Prefix: logPrefix, + Level: types.Error, + Fatal: true, } if err := godotenv.Load(); err != nil { @@ -26,6 +25,7 @@ func init() { } Config = &types.BotConfig{ + GuildID: getEnv("GUILD_ID"), DiscordToken: getEnv("DISCORD_TOKEN"), SpotifyClientId: getEnv("SPOTIFY_CLIENT_ID"), SpotifyClientSecret: getEnv("SPOTIFY_CLIENT_SECRET"), @@ -35,6 +35,10 @@ func init() { ActivityURL: getEnv("ACTIVITY_URL"), } + if Config.GuildID == "" { + logger.Log("Unable to read Guild ID. environment variable GUILD_ID is required", logOptions) + } + if Config.DiscordToken == "" { logger.Log("Unable to read Discord token. environment variable DISCORD_TOKEN is required", logOptions) } diff --git a/handlers/interactionCreateHandler.go b/handlers/interactionCreateHandler.go new file mode 100644 index 0000000..8e04111 --- /dev/null +++ b/handlers/interactionCreateHandler.go @@ -0,0 +1,12 @@ +package handlers + +import "github.com/bwmarrin/discordgo" + +func InteractionCreateHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + switch i.Type { + case discordgo.InteractionApplicationCommand: + if handler, ok := SlashCommandHandlers[i.ApplicationCommandData().Name]; ok { + handler(s, i) + } + } +} diff --git a/handlers/slashCommandHandler.go b/handlers/slashCommandHandler.go new file mode 100644 index 0000000..ebcd0db --- /dev/null +++ b/handlers/slashCommandHandler.go @@ -0,0 +1,13 @@ +package handlers + +import ( + "ai/commands" + + "github.com/bwmarrin/discordgo" +) + +var ( + SlashCommandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ + "play": commands.Play, + } +) diff --git a/types/bot.go b/types/bot.go index 2f6adf3..f7eb53e 100644 --- a/types/bot.go +++ b/types/bot.go @@ -10,6 +10,7 @@ const ( ) type BotConfig struct { + GuildID string DiscordToken string SpotifyClientId string SpotifyClientSecret string |
