Skip to content

Commit

Permalink
fix link formatting when playing & preview track/playlist name/author…
Browse files Browse the repository at this point in the history
… in autocomplete
  • Loading branch information
topi314 committed Sep 30, 2024
1 parent 19f2381 commit 1539235
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
33 changes: 32 additions & 1 deletion commands/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
Expand Down
9 changes: 7 additions & 2 deletions internal/res/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package res

import (
"fmt"
"strings"

"github.com/disgoorg/disgolink/v3/lavalink"
"github.com/disgoorg/lavasrc-plugin"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1539235

Please sign in to comment.