From 1539235ec78c1fc7843ede9da8847c852beaf9b7 Mon Sep 17 00:00:00 2001 From: topi314 Date: Mon, 30 Sep 2024 23:15:57 +0200 Subject: [PATCH] fix link formatting when playing & preview track/playlist name/author in autocomplete --- commands/play.go | 33 ++++++++++++++++++++++++++++++++- internal/res/track.go | 9 +++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/commands/play.go b/commands/play.go index f996c5a..f31d0a0 100644 --- a/commands/play.go +++ b/commands/play.go @@ -38,9 +38,40 @@ func (c *Commands) PlayAutocomplete(e *handler.AutocompleteEvent) error { } if urlPattern.MatchString(query) { + ctx, cancel := context.WithTimeout(e.Ctx, 10*time.Second) + defer cancel() + result, err := c.Lavalink.BestNode().LoadTracks(ctx, query) + if err != nil { + return e.AutocompleteResult([]discord.AutocompleteChoice{ + discord.AutocompleteChoiceString{ + Name: res.Trim("Failed to load track: "+err.Error(), 100), + Value: "error", + }, + }) + } + + var name string + switch loadData := result.Data.(type) { + case lavalink.Track: + name = fmt.Sprintf("%s - %s", loadData.Info.Title, loadData.Info.Author) + case lavalink.Playlist: + var playlistInfo lavasrc.PlaylistInfo + _ = loadData.PluginInfo.Unmarshal(&playlistInfo) + name = fmt.Sprintf("%s: %s - %s", playlistInfo.Type, loadData.Info.Name, playlistInfo.Author) + case lavalink.Empty: + return e.AutocompleteResult(nil) + case lavalink.Exception: + return e.AutocompleteResult([]discord.AutocompleteChoice{ + discord.AutocompleteChoiceString{ + Name: res.Trim("Failed to load track: "+loadData.Error(), 100), + Value: "error", + }, + }) + } + return e.AutocompleteResult([]discord.AutocompleteChoice{ discord.AutocompleteChoiceString{ - Name: res.Trim("🔗 "+query, 100), + Name: res.Trim("🔗 "+name, 100), Value: query, }, }) diff --git a/internal/res/track.go b/internal/res/track.go index 60c7fa6..7b883dc 100644 --- a/internal/res/track.go +++ b/internal/res/track.go @@ -2,6 +2,7 @@ package res import ( "fmt" + "strings" "github.com/disgoorg/disgolink/v3/lavalink" "github.com/disgoorg/lavasrc-plugin" @@ -29,9 +30,13 @@ func FormatTrack(track lavalink.Track, position lavalink.Duration) string { } } - trackName := fmt.Sprintf("`%s`", track.Info.Title) + title := track.Info.Title + title = strings.TrimPrefix(title, "https://") + title = strings.TrimPrefix(title, "http://") + + trackName := fmt.Sprintf("`%s`", title) if track.Info.URI != nil { - trackName = fmt.Sprintf("[`%s`](<%s>)", track.Info.Title, *track.Info.URI) + trackName = fmt.Sprintf("[`%s`](<%s>)", title, *track.Info.URI) } var albumName string