diff options
| author | Bobby <[email protected]> | 2025-04-13 18:41:17 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-04-13 18:41:17 +0530 |
| commit | 6b486844a18fe8bf9b4ca4bdd3c44f45094e03e5 (patch) | |
| tree | b2655928e1fd60516619359ba214015084b05b9c /commands | |
| parent | f884a0fe47f7403850de650d3be3733420ac5986 (diff) | |
| download | ai-6b486844a18fe8bf9b4ca4bdd3c44f45094e03e5.tar.xz ai-6b486844a18fe8bf9b4ca4bdd3c44f45094e03e5.zip | |
refactor; add logging; add disconnect; supress opus warnings
Diffstat (limited to 'commands')
| -rw-r--r-- | commands/commands.go | 4 | ||||
| -rw-r--r-- | commands/disconnect.go | 55 | ||||
| -rw-r--r-- | commands/helpers.go | 19 | ||||
| -rw-r--r-- | commands/play.go | 31 | ||||
| -rw-r--r-- | commands/play_autocomplete.go | 8 |
5 files changed, 80 insertions, 37 deletions
diff --git a/commands/commands.go b/commands/commands.go index 00f09f8..756c38f 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -17,5 +17,9 @@ var ( }, }, }, + { + Name: "disconnect", + Description: "Disconnect the bot from the voice channel", + }, } ) diff --git a/commands/disconnect.go b/commands/disconnect.go new file mode 100644 index 0000000..94249c8 --- /dev/null +++ b/commands/disconnect.go @@ -0,0 +1,55 @@ +package commands + +import ( + "ai/utils/music" + "fmt" + + "github.com/bwmarrin/discordgo" +) + +func Disconnect(s *discordgo.Session, i *discordgo.InteractionCreate) { + guildID := i.GuildID + userID := i.Member.User.ID + + isSameVC, userChannelID := music.IsUserInSameVC(s, guildID, userID) + + if userChannelID == "" { + respondWithError(s, i, "You must be in a voice channel to use this command.") + return + } + + voice, exists := music.VoiceConnection[guildID] + if !exists { + respondWithError(s, i, "I'm not in a voice channel.") + return + } + + if !isSameVC { + channel, err := s.Channel(voice.ChannelID) + if err == nil { + respondWithError(s, i, fmt.Sprintf("You must be in the same voice channel as me (**%s**) to use this command.", channel.Name)) + } else { + respondWithError(s, i, "You must be in the same voice channel as me to use this command.") + } + return + } + + channel, err := s.Channel(voice.ChannelID) + channelName := "voice channel" + if err == nil { + channelName = channel.Name + } + + err = music.LeaveVoiceChannel(guildID) + if err != nil { + respondWithError(s, i, fmt.Sprintf("Error disconnecting from voice channel: %v", err)) + return + } + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: fmt.Sprintf("✅ Disconnected from **%s**.", channelName), + }, + }) +} diff --git a/commands/helpers.go b/commands/helpers.go new file mode 100644 index 0000000..19859db --- /dev/null +++ b/commands/helpers.go @@ -0,0 +1,19 @@ +package commands + +import "github.com/bwmarrin/discordgo" + +func respondWithError(s *discordgo.Session, i *discordgo.InteractionCreate, message string) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: message, + Flags: discordgo.MessageFlagsEphemeral, + }, + }) +} + +func updateResponse(s *discordgo.Session, i *discordgo.InteractionCreate, message string) { + s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ + Content: &message, + }) +} diff --git a/commands/play.go b/commands/play.go index b8d39fd..6aac6c3 100644 --- a/commands/play.go +++ b/commands/play.go @@ -14,7 +14,6 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { options := i.ApplicationCommandData().Options input := options[0].StringValue() - // Special cases from autocomplete if input == "min_chars" { respondWithError(s, i, "Enter at least 3 characters to search.") return @@ -25,7 +24,6 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { return } - // Check if the user is in a voice channel BEFORE deferring response guildID := i.GuildID userID := i.Member.User.ID @@ -36,7 +34,6 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { return } - // Check if bot is already in a voice channel but not the same as the user voice, exists := music.VoiceConnection[guildID] if exists && !isSameVC { channel, err := s.Channel(voice.ChannelID) @@ -48,12 +45,10 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { return } - // Now that we've checked all error conditions, defer the response s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseDeferredChannelMessageWithSource, }) - // Process track selection or URL var trackURL, trackID, trackTitle string var sourceType types.SourceType @@ -81,12 +76,10 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { trackID = ytTrack.ID } } else { - // malformed selection updateResponse(s, i, "❌ Invalid track selection. Please try again.") return } } else { - // Direct URL or search query if music.IsYouTubeURL(input) { trackInfo, err := music.GetYouTubeInfo(input) if err != nil { @@ -104,7 +97,6 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { return } - // Get YouTube equivalent ytTrack, err := music.GetYouTubeForSpotify(trackInfo.Title, trackInfo.Artist) if err != nil { updateResponse(s, i, "❌ Error fetching YouTube equivalent for Spotify track.") @@ -112,10 +104,9 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { } trackURL = ytTrack.URL trackID = ytTrack.ID - trackTitle = ytTrack.Title + trackTitle = trackInfo.Title sourceType = types.Spotify } else { - // treat as search query results, err := music.Search(input, 1) if err != nil || len(results) == 0 { updateResponse(s, i, "❌ No results found for your search query.") @@ -141,7 +132,6 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { } } - // Join voice channel voice, err := music.JoinVoiceChannel(s, guildID, userChannelID) if err != nil { logger.Log(fmt.Sprintf("Failed to join voice channel: %v", err), types.LogOptions{ @@ -152,10 +142,8 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { return } - // Send "now playing" message first updateResponse(s, i, fmt.Sprintf("🎵 Now playing: **%s**", trackTitle)) - // Play the track go func() { err := voice.PlayYouTube(trackURL, trackID) if err != nil { @@ -163,24 +151,7 @@ func Play(s *discordgo.Session, i *discordgo.InteractionCreate) { Prefix: "Play Command", Level: types.Error, }) - // Also update the message to show the error to the user updateResponse(s, i, fmt.Sprintf("❌ Error playing **%s**: %v", trackTitle, err)) } }() } - -func respondWithError(s *discordgo.Session, i *discordgo.InteractionCreate, message string) { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: message, - Flags: discordgo.MessageFlagsEphemeral, - }, - }) -} - -func updateResponse(s *discordgo.Session, i *discordgo.InteractionCreate, message string) { - s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ - Content: &message, - }) -} diff --git a/commands/play_autocomplete.go b/commands/play_autocomplete.go index dee02d1..85613b3 100644 --- a/commands/play_autocomplete.go +++ b/commands/play_autocomplete.go @@ -40,7 +40,6 @@ func PlayAutocomplete(s *discordgo.Session, i *discordgo.InteractionCreate) { return } - // Search for tracks results, err := music.Search(query, 10) if err != nil { logger.Log(fmt.Sprintf("Search error: %v", err), types.LogOptions{ @@ -62,15 +61,13 @@ func PlayAutocomplete(s *discordgo.Session, i *discordgo.InteractionCreate) { return } - // Create choices for autocomplete choices := make([]*discordgo.ApplicationCommandOptionChoice, 0, 25) for i, result := range results { if i >= 25 { - break // Discord limits to 25 choices + break } - // Format display name var displayName string if result.SourceType == types.YouTube { displayName = fmt.Sprintf("▶️ %s - %s", result.Title, result.Artist) @@ -78,12 +75,10 @@ func PlayAutocomplete(s *discordgo.Session, i *discordgo.InteractionCreate) { displayName = fmt.Sprintf("🎵 %s - %s", result.Title, result.Artist) } - // Truncate name if needed if len(displayName) > 100 { displayName = displayName[:97] + "..." } - // Use just source type and ID as value to avoid length issues valueStr := fmt.Sprintf("%s|%s|%s", result.SourceType, result.ID, result.URL) if len(valueStr) > 100 { valueStr = fmt.Sprintf("%s|%s", result.SourceType, result.ID) @@ -102,7 +97,6 @@ func PlayAutocomplete(s *discordgo.Session, i *discordgo.InteractionCreate) { }) } - // Send response err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionApplicationCommandAutocompleteResult, Data: &discordgo.InteractionResponseData{ |
