diff options
| author | Anhgelus Morhtuuzh <anhgelus@anhgelus.world> | 2025-05-13 16:15:47 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <anhgelus@anhgelus.world> | 2025-05-13 16:15:47 +0200 |
| commit | 01bafe9bf1de5be4e770b9500480807d4973d8d6 (patch) | |
| tree | c6eafab11fef21c221136019ea490176185cec81 /commands/top.go | |
| parent | 799df74fcda5266fd295b49fc759c605c815cad9 (diff) | |
feat(top): implements new kind of tops
Diffstat (limited to 'commands/top.go')
| -rw-r--r-- | commands/top.go | 57 |
1 files changed, 39 insertions, 18 deletions
diff --git a/commands/top.go b/commands/top.go index 22574ce..a43e055 100644 --- a/commands/top.go +++ b/commands/top.go @@ -2,15 +2,14 @@ package commands import ( "fmt" - "github.com/anhgelus/gokord" "github.com/anhgelus/gokord/utils" "github.com/anhgelus/les-copaings-bot/exp" "github.com/anhgelus/les-copaings-bot/user" "github.com/bwmarrin/discordgo" + "sync" ) func Top(s *discordgo.Session, i *discordgo.InteractionCreate) { - user.LastEventUpdate(s, user.GetCopaing(i.Member.User.ID, i.GuildID)) resp := utils.ResponseBuilder{C: s, I: i} err := resp.IsDeferred().Send() if err != nil { @@ -18,26 +17,48 @@ func Top(s *discordgo.Session, i *discordgo.InteractionCreate) { return } resp.NotDeferred().IsEdit() - go func() { - var tops []user.Copaing - gokord.DB.Where("guild_id = ?", i.GuildID).Limit(10).Order("exp desc").Find(&tops) - msg := "" - for i, c := range tops { - if i == 9 { - msg += fmt.Sprintf("%d. **<@%s>** - niveau %d", i+1, c.DiscordID, exp.Level(c.XP)) - } else { - msg += fmt.Sprintf("%d. **<@%s>** - niveau %d\n", i+1, c.DiscordID, exp.Level(c.XP)) + embeds := make([]*discordgo.MessageEmbed, 3) + wg := sync.WaitGroup{} + + fn := func(s string, n uint, d int, id int) { + defer wg.Done() + tops, err := user.GetBestXP(i.GuildID, n, d) + if err != nil { + utils.SendAlert("commands/top.go - Fetching best xp", err.Error(), "n", n, "d", d, "id", id, "guild_id", i.GuildID) + embeds[id] = &discordgo.MessageEmbed{ + Title: s, + Description: "Erreur : impossible de récupérer la liste", + Color: utils.Error, } + return } - err = resp.Embeds([]*discordgo.MessageEmbed{ - { - Title: "Top", - Description: msg, - Color: utils.Success, - }, - }).Send() + embeds[id] = &discordgo.MessageEmbed{ + Title: s, + Description: genTopsMessage(tops), + Color: utils.Success, + } + } + + wg.Add(3) + go fn("Top full time", 10, -1, 0) + go fn("Top 30 jours", 5, 30, 1) + go fn("Top 7 jours", 5, 7, 2) + go func() { + wg.Wait() + err = resp.Embeds(embeds).Send() if err != nil { utils.SendAlert("commands/top.go - Sending response top", err.Error()) } }() } + +func genTopsMessage(tops []user.CopaingAccess) string { + msg := "" + for i, c := range tops { + msg += fmt.Sprintf("%d. **<@%s>** - niveau %d", i+1, c.ToCopaing().DiscordID, exp.Level(c.GetXP())) + if i != len(tops)-1 { + msg += "\n" + } + } + return msg +} |
