Skip to content

Commit a8e535a

Browse files
committed
update latest command with plugin name + git url
1 parent cb883ba commit a8e535a

File tree

3 files changed

+53
-41
lines changed

3 files changed

+53
-41
lines changed

commands/latest.go

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package commands
33
import (
44
"context"
55
"fmt"
6+
"strings"
7+
"sync"
68

79
"github.com/disgoorg/disgo/discord"
810
"github.com/disgoorg/disgo/handler"
@@ -24,9 +26,9 @@ var latest = discord.SlashCommandCreate{
2426
}
2527

2628
func (c *Commands) LatestAutocomplete(e *handler.AutocompleteEvent) error {
27-
options := []string{"lavalink"}
29+
options := []string{"Lavalink"}
2830
for _, plugin := range c.Cfg.Plugins {
29-
options = append(options, plugin.Dependency)
31+
options = append(options, plugin.Name)
3032
}
3133

3234
ranks := fuzzy.RankFindFold(e.Data.String("type"), options)
@@ -36,9 +38,17 @@ func (c *Commands) LatestAutocomplete(e *handler.AutocompleteEvent) error {
3638
if len(choices) >= 25 {
3739
break
3840
}
41+
42+
var value string
43+
if rank.OriginalIndex == 0 {
44+
value = "lavalink"
45+
} else {
46+
value = c.Cfg.Plugins[rank.OriginalIndex-1].Dependency
47+
}
48+
3949
choices = append(choices, discord.AutocompleteChoiceString{
4050
Name: options[rank.OriginalIndex],
41-
Value: options[rank.OriginalIndex],
51+
Value: value,
4252
})
4353
}
4454

@@ -50,45 +60,49 @@ func (c *Commands) Latest(data discord.SlashCommandInteractionData, e *handler.C
5060
return err
5161
}
5262

53-
latestType, ok := e.SlashCommandInteractionData().OptString("type")
54-
if !ok {
55-
embed := getLatestEmbed(c, "lavalink")
63+
var types []string
64+
latestType, ok := data.OptString("type")
65+
if ok {
66+
types = []string{latestType}
67+
} else {
68+
types = append(types, "lavalink")
5669
for _, plugin := range c.Cfg.Plugins {
57-
newEmbed := getLatestEmbed(c, plugin.Dependency)
58-
embed.Fields = append(embed.Fields, discord.EmbedField{
59-
Name: newEmbed.Title,
60-
Value: newEmbed.Description,
61-
})
70+
types = append(types, plugin.Dependency)
6271
}
72+
}
6373

64-
_, err := e.UpdateInteractionResponse(discord.MessageUpdate{
65-
Embeds: &[]discord.Embed{embed},
66-
})
67-
return err
74+
var wg sync.WaitGroup
75+
versions := make([]string, len(types))
76+
for i, versionType := range types {
77+
wg.Add(1)
78+
go func() {
79+
defer wg.Done()
80+
versions[i] = getLatest(c, versionType)
81+
}()
6882
}
69-
embed := getLatestEmbed(c, latestType)
83+
wg.Wait()
84+
7085
_, err := e.UpdateInteractionResponse(discord.MessageUpdate{
71-
Embeds: &[]discord.Embed{embed},
86+
Embeds: &[]discord.Embed{
87+
{
88+
Title: "Latest Versions",
89+
URL: "https://lavalink.dev/plugins.html",
90+
Description: strings.Join(versions, "\n\n"),
91+
},
92+
},
7293
})
7394
return err
7495
}
7596

76-
func getLatestEmbed(c *Commands, latestType string) discord.Embed {
97+
func getLatest(c *Commands, latestType string) string {
7798
if latestType == "lavalink" {
78-
embed := discord.Embed{
79-
Author: &discord.EmbedAuthor{
80-
Name: "Latest Lavalink Version",
81-
},
82-
}
8399
release, _, err := c.GitHub.Repositories.GetLatestRelease(context.Background(), "lavalink-devs", "Lavalink")
84100
if err != nil {
85-
embed.Description = "Failed to get latest Lavalink version: " + err.Error()
86-
return embed
101+
return "Failed to get latest Lavalink version: " + err.Error()
87102
}
88-
embed.Author.URL = release.GetHTMLURL()
89-
embed.Description = fmt.Sprintf("**Version:** `%s`\n**Release Date:** %s", release.GetTagName(), discord.NewTimestamp(discord.TimestampStyleLongDateTime, release.GetPublishedAt().Time))
90-
return embed
103+
return fmt.Sprintf("**[`Lavalink`](%s):**\n**Latest Version:** `%s`\n**Release Date:** %s", release.GetHTMLURL(), release.GetTagName(), discord.NewTimestamp(discord.TimestampStyleLongDateTime, release.GetPublishedAt().Time))
91104
}
105+
92106
var pluginCfg lavalinkbot.PluginConfig
93107
for _, plugin := range c.Cfg.Plugins {
94108
if plugin.Dependency == latestType {
@@ -97,20 +111,12 @@ func getLatestEmbed(c *Commands, latestType string) discord.Embed {
97111
}
98112
}
99113
if pluginCfg.Dependency == "" {
100-
return discord.Embed{
101-
Description: fmt.Sprintf("Unknown plugin: `%s`", latestType),
102-
}
114+
return fmt.Sprintf("Unknown plugin: `%s`", latestType)
103115
}
104116

105117
metadata, err := c.Maven.FetchLatestVersion(pluginCfg.Dependency, pluginCfg.Repository)
106118
if err != nil {
107-
return discord.Embed{
108-
Description: fmt.Sprintf("Failed to get latest %s version: %s", pluginCfg.Dependency, err.Error()),
109-
}
119+
return fmt.Sprintf("Failed to get latest %s version: %s", pluginCfg.Dependency, err.Error())
110120
}
111-
return discord.Embed{
112-
Title: fmt.Sprintf("Latest %s Version", metadata.ArtifactID),
113-
Description: fmt.Sprintf("**Version:** `%s`", metadata.Latest()),
114-
}
115-
121+
return fmt.Sprintf("**[`%s`](%s):**\n**Latest Version:** `%s`", pluginCfg.Name, pluginCfg.Git, metadata.Latest())
116122
}

config.example.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ nodes:
1414
session_id: ...
1515

1616
plugins:
17-
- dependency: ...
17+
- name: ...
18+
dependency: ...
1819
repository: ...
20+
git: ...

lavalinkbot/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,17 @@ func (c PluginConfigs) String() string {
125125
}
126126

127127
type PluginConfig struct {
128+
Name string `yaml:"name"`
128129
Dependency string `yaml:"dependency"`
129130
Repository string `yaml:"repository"`
131+
Git string `yaml:"git"`
130132
}
131133

132134
func (c PluginConfig) String() string {
133-
return fmt.Sprintf("\n Dependency: %s\n Repository: %s",
135+
return fmt.Sprintf("\n Name: %s\n Dependency: %s\n Repository: %s\n Git: %s\n",
136+
c.Name,
134137
c.Dependency,
135138
c.Repository,
139+
c.Git,
136140
)
137141
}

0 commit comments

Comments
 (0)