Skip to content

Commit

Permalink
update latest command with plugin name + git url
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed May 20, 2024
1 parent cb883ba commit a8e535a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 41 deletions.
84 changes: 45 additions & 39 deletions commands/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package commands
import (
"context"
"fmt"
"strings"
"sync"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/handler"
Expand All @@ -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)
Expand All @@ -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,
})
}

Expand All @@ -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 {
Expand All @@ -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())
}
4 changes: 3 additions & 1 deletion config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ nodes:
session_id: ...

plugins:
- dependency: ...
- name: ...
dependency: ...
repository: ...
git: ...
6 changes: 5 additions & 1 deletion lavalinkbot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

0 comments on commit a8e535a

Please sign in to comment.