Skip to content

Commit

Permalink
allow !ma <number> to add a number from the latest search results
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwiltink committed Jun 9, 2023
1 parent 3ab8e9d commit 5c857c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pkg/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 23 additions & 2 deletions pkg/bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())

},
Expand Down

0 comments on commit 5c857c9

Please sign in to comment.