diff --git a/commands/latest.go b/commands/latest.go index 8ca2f0b..6d1415a 100644 --- a/commands/latest.go +++ b/commands/latest.go @@ -3,6 +3,8 @@ package commands import ( "context" "fmt" + "strings" + "sync" "github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/handler" @@ -24,9 +26,9 @@ var latest = discord.SlashCommandCreate{ } func (c *Commands) LatestAutocomplete(e *handler.AutocompleteEvent) error { - options := []string{"lavalink"} + options := []string{"Lavalink"} for _, plugin := range c.Cfg.Plugins { - options = append(options, plugin.Dependency) + options = append(options, plugin.Name) } ranks := fuzzy.RankFindFold(e.Data.String("type"), options) @@ -36,9 +38,17 @@ func (c *Commands) LatestAutocomplete(e *handler.AutocompleteEvent) error { if len(choices) >= 25 { break } + + var value string + if rank.OriginalIndex == 0 { + value = "lavalink" + } else { + value = c.Cfg.Plugins[rank.OriginalIndex-1].Dependency + } + choices = append(choices, discord.AutocompleteChoiceString{ Name: options[rank.OriginalIndex], - Value: options[rank.OriginalIndex], + Value: value, }) } @@ -50,45 +60,49 @@ func (c *Commands) Latest(data discord.SlashCommandInteractionData, e *handler.C return err } - latestType, ok := e.SlashCommandInteractionData().OptString("type") - if !ok { - embed := getLatestEmbed(c, "lavalink") + var types []string + latestType, ok := data.OptString("type") + if ok { + types = []string{latestType} + } else { + types = append(types, "lavalink") for _, plugin := range c.Cfg.Plugins { - newEmbed := getLatestEmbed(c, plugin.Dependency) - embed.Fields = append(embed.Fields, discord.EmbedField{ - Name: newEmbed.Title, - Value: newEmbed.Description, - }) + types = append(types, plugin.Dependency) } + } - _, err := e.UpdateInteractionResponse(discord.MessageUpdate{ - Embeds: &[]discord.Embed{embed}, - }) - return err + var wg sync.WaitGroup + versions := make([]string, len(types)) + for i, versionType := range types { + wg.Add(1) + go func() { + defer wg.Done() + versions[i] = getLatest(c, versionType) + }() } - embed := getLatestEmbed(c, latestType) + wg.Wait() + _, err := e.UpdateInteractionResponse(discord.MessageUpdate{ - Embeds: &[]discord.Embed{embed}, + Embeds: &[]discord.Embed{ + { + Title: "Latest Versions", + URL: "https://lavalink.dev/plugins.html", + Description: strings.Join(versions, "\n\n"), + }, + }, }) return err } -func getLatestEmbed(c *Commands, latestType string) discord.Embed { +func getLatest(c *Commands, latestType string) string { if latestType == "lavalink" { - embed := discord.Embed{ - Author: &discord.EmbedAuthor{ - Name: "Latest Lavalink Version", - }, - } release, _, err := c.GitHub.Repositories.GetLatestRelease(context.Background(), "lavalink-devs", "Lavalink") if err != nil { - embed.Description = "Failed to get latest Lavalink version: " + err.Error() - return embed + return "Failed to get latest Lavalink version: " + err.Error() } - embed.Author.URL = release.GetHTMLURL() - embed.Description = fmt.Sprintf("**Version:** `%s`\n**Release Date:** %s", release.GetTagName(), discord.NewTimestamp(discord.TimestampStyleLongDateTime, release.GetPublishedAt().Time)) - return embed + return fmt.Sprintf("**[`Lavalink`](%s):**\n**Latest Version:** `%s`\n**Release Date:** %s", release.GetHTMLURL(), release.GetTagName(), discord.NewTimestamp(discord.TimestampStyleLongDateTime, release.GetPublishedAt().Time)) } + var pluginCfg lavalinkbot.PluginConfig for _, plugin := range c.Cfg.Plugins { if plugin.Dependency == latestType { @@ -97,20 +111,12 @@ func getLatestEmbed(c *Commands, latestType string) discord.Embed { } } if pluginCfg.Dependency == "" { - return discord.Embed{ - Description: fmt.Sprintf("Unknown plugin: `%s`", latestType), - } + return fmt.Sprintf("Unknown plugin: `%s`", latestType) } metadata, err := c.Maven.FetchLatestVersion(pluginCfg.Dependency, pluginCfg.Repository) if err != nil { - return discord.Embed{ - Description: fmt.Sprintf("Failed to get latest %s version: %s", pluginCfg.Dependency, err.Error()), - } + return fmt.Sprintf("Failed to get latest %s version: %s", pluginCfg.Dependency, err.Error()) } - return discord.Embed{ - Title: fmt.Sprintf("Latest %s Version", metadata.ArtifactID), - Description: fmt.Sprintf("**Version:** `%s`", metadata.Latest()), - } - + return fmt.Sprintf("**[`%s`](%s):**\n**Latest Version:** `%s`", pluginCfg.Name, pluginCfg.Git, metadata.Latest()) } diff --git a/config.example.yml b/config.example.yml index d26aeb0..bacf064 100644 --- a/config.example.yml +++ b/config.example.yml @@ -14,5 +14,7 @@ nodes: session_id: ... plugins: - - dependency: ... + - name: ... + dependency: ... repository: ... + git: ... diff --git a/lavalinkbot/config.go b/lavalinkbot/config.go index 92b51c2..4cebb6f 100644 --- a/lavalinkbot/config.go +++ b/lavalinkbot/config.go @@ -125,13 +125,17 @@ func (c PluginConfigs) String() string { } type PluginConfig struct { + Name string `yaml:"name"` Dependency string `yaml:"dependency"` Repository string `yaml:"repository"` + Git string `yaml:"git"` } func (c PluginConfig) String() string { - return fmt.Sprintf("\n Dependency: %s\n Repository: %s", + return fmt.Sprintf("\n Name: %s\n Dependency: %s\n Repository: %s\n Git: %s\n", + c.Name, c.Dependency, c.Repository, + c.Git, ) }