aboutsummaryrefslogtreecommitdiff
path: root/commands/top.go
blob: fa12a66bbcddc5c884e3d68cbe4a6436e2cb449d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package commands

import (
	"context"
	"fmt"
	"sync"

	"git.anhgelus.world/anhgelus/les-copaings-bot/config"
	"git.anhgelus.world/anhgelus/les-copaings-bot/exp"
	"git.anhgelus.world/anhgelus/les-copaings-bot/user"
	"github.com/anhgelus/gokord/cmd"
	"github.com/nyttikord/gokord/bot"
	"github.com/nyttikord/gokord/channel"
	"github.com/nyttikord/gokord/event"
)

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) {
		err := resp.IsDeferred().Send()
		if err != nil {
			s.Logger().Error("sending deferred", "error", err)
			return
		}
		embeds := make([]*channel.MessageEmbed, 3)
		wg := sync.WaitGroup{}

		fn := func(str string, n uint, d int, id int) {
			defer wg.Done()
			tops, err := user.GetBestXP(ctx, s.Logger(), 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
			}
			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.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)
			}
		}()
	}
}

func genTopsMessage(tops []user.CopaingCached) string {
	msg := ""
	for i, c := range tops {
		msg += fmt.Sprintf("%d. **<@%s>** - niveau %d", i+1, c.DiscordID, exp.Level(c.XPs))
		if i != len(tops)-1 {
			msg += "\n"
		}
	}
	return msg
}