From e68a00ec6c3533ab180b5c19a712d41912d0db84 Mon Sep 17 00:00:00 2001 From: svenwiltink Date: Fri, 9 Jun 2023 14:53:50 +0200 Subject: [PATCH] allow !ma to add a number from the latest search results (#92) --- pkg/bot/bot.go | 4 +++- pkg/bot/commands.go | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/pkg/bot/bot.go b/pkg/bot/bot.go index fa6bd9d..bf81944 100644 --- a/pkg/bot/bot.go +++ b/pkg/bot/bot.go @@ -25,7 +25,9 @@ type MusicBot struct { config *Config commands map[string]Command commandAliases map[string]Command - allowlist *AllowList + searchCache []music.Song + + allowlist *AllowList } func NewMusicBot(config *Config, messageProvider MessageProvider) *MusicBot { diff --git a/pkg/bot/commands.go b/pkg/bot/commands.go index 0056714..d3c701f 100644 --- a/pkg/bot/commands.go +++ b/pkg/bot/commands.go @@ -53,11 +53,29 @@ var addCommand = Command{ return } + parameter = sanitizeSongURL(parameter) + + songNr, err := strconv.ParseInt(parameter, 10, 64) + if err == nil { + if len(bot.searchCache) == 0 { + bot.ReplyToMessage(message, "no search results yet") + return + } + + // we are trying to add from the search cache + if songNr < 1 || int(songNr) > len(bot.searchCache) { + bot.ReplyToMessage(message, fmt.Sprintf("invalid search index. Must be between 1 and %d", len(bot.searchCache))) + return + } + + parameter = bot.searchCache[songNr-1].Path + } + song := music.Song{ - Path: sanitizeSongURL(parameter), + Path: parameter, } - song, err := bot.musicPlayer.AddSong(song) + song, err = bot.musicPlayer.AddSong(song) if err != nil { bot.ReplyToMessage(message, err.Error()) return @@ -98,6 +116,9 @@ var searchCommand = Command{ builder.WriteString(fmt.Sprintf("%d %s - %s (%s)\n", number+1, song.Artist, song.Name, song.Duration)) } + // populate the search cache + bot.searchCache = songs + bot.ReplyToMessage(message, builder.String()) },