diff options
| author | William Hergès <william@herges.fr> | 2026-01-17 22:05:47 +0100 |
|---|---|---|
| committer | William Hergès <william@herges.fr> | 2026-01-17 22:05:47 +0100 |
| commit | a3b0f488f799020d20c6cd2eb6bb082071bb0455 (patch) | |
| tree | 0969f9cec659338988507cc24f8ca455cf926901 | |
| parent | a3543d79561a3754540b921c54c3c177016c2397 (diff) | |
style(): clean various old things
| -rw-r--r-- | commands/top.go | 59 | ||||
| -rw-r--r-- | user/state.go | 1 | ||||
| -rw-r--r-- | user/xp.go | 7 |
3 files changed, 33 insertions, 34 deletions
diff --git a/commands/top.go b/commands/top.go index 99c8f16..a6ed992 100644 --- a/commands/top.go +++ b/commands/top.go @@ -17,49 +17,44 @@ import ( func Top(ctx context.Context) func(s bot.Session, i *event.InteractionCreate, _ cmd.OptionMap, resp *cmd.ResponseBuilder) { return func(s bot.Session, i *event.InteractionCreate, _ cmd.OptionMap, resp *cmd.ResponseBuilder) { embeds := make([]*channel.MessageEmbed, 3) - wg := sync.WaitGroup{} + var wg sync.WaitGroup fn := func(str string, n uint, d int, id int) { - defer wg.Done() - tops, err := user.GetBestXP(ctx, i.GuildID, n, d) - if err != nil { - s.Logger().Error("fetching best xp", "error", err, "n", n, "d", d, "id", id, "guild", i.GuildID) - embeds[id] = &channel.MessageEmbed{ - Title: str, - Description: "Erreur : impossible de récupérer la liste", - Color: 0x831010, - } - return - } + tops := user.GetBestXP(ctx, i.GuildID, n, d) embeds[id] = &channel.MessageEmbed{ Title: str, Description: genTopsMessage(tops), Color: 0x10E6AD, } } + cfg := config.GetGuildConfig(i.GuildID) if cfg.DaysXPRemains > 30 { - wg.Add(1) - go fn(fmt.Sprintf("Top %d jours", cfg.DaysXPRemains), 10, -1, 0) + wg.Go(func() { + fn(fmt.Sprintf("Top %d jours", cfg.DaysXPRemains), 10, -1, 0) + }) + } + + wg.Go(func() { + fn("Top 30 jours", 5, 30, 1) + }) + wg.Go(func() { + fn("Top 7 jours", 5, 7, 2) + }) + + wg.Wait() + if cfg.DaysXPRemains > 30 { + resp.AddEmbed(embeds[0]). + AddEmbed(embeds[1]). + AddEmbed(embeds[2]) + } else { + resp.AddEmbed(embeds[1]). + AddEmbed(embeds[2]) + } + err := resp.Send() + if err != nil { + s.Logger().Error("sending response top", "error", err) } - wg.Add(2) - go fn("Top 30 jours", 5, 30, 1) - go fn("Top 7 jours", 5, 7, 2) - go func() { - wg.Wait() - if cfg.DaysXPRemains > 30 { - resp.AddEmbed(embeds[0]). - AddEmbed(embeds[1]). - AddEmbed(embeds[2]) - } else { - resp.AddEmbed(embeds[1]). - AddEmbed(embeds[2]) - } - err := resp.Send() - if err != nil { - s.Logger().Error("sending response top", "error", err) - } - }() } } diff --git a/user/state.go b/user/state.go index effdc80..676728b 100644 --- a/user/state.go +++ b/user/state.go @@ -214,6 +214,7 @@ func generateXPs(c *Copaing) []XPCached { i := 0 for _, v := range data { ccs[i] = v + println(v.Time.String(), v.XP) i++ } return ccs @@ -51,7 +51,7 @@ func (cc *CopaingCached) GetXPForDays(n uint) uint { } // GetBestXP returns n Copaing with the best XP within d days (d <= cfg.DaysXPRemain; d < 0 <=> d = cfg.DaysXPRemain) -func GetBestXP(ctx context.Context, guildId string, n uint, d int) ([]CopaingCached, error) { +func GetBestXP(ctx context.Context, guildId string, n uint, d int) []CopaingCached { ccs := GetState(ctx).Copaings(guildId) if d > 0 { for _, v := range ccs { @@ -62,5 +62,8 @@ func GetBestXP(ctx context.Context, guildId string, n uint, d int) ([]CopaingCac // desc order return int(b.XP) - int(a.XP) }) - return ccs[:min(len(ccs), int(n))], nil + m := min(len(ccs), int(n)) + res := make([]CopaingCached, m) + copy(ccs[:m], res) + return res } |
