aboutsummaryrefslogtreecommitdiff
path: root/commands/config.go
blob: 867aec1c2d02ce484d70bb1da249918bdf987d65 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package commands

import (
	"context"
	"fmt"
	"slices"
	"strconv"
	"strings"

	"git.anhgelus.world/anhgelus/les-copaings-bot/config"
	"git.anhgelus.world/anhgelus/les-copaings-bot/exp"
	"github.com/nyttikord/gokord/bot"
	"github.com/nyttikord/gokord/channel"
	"github.com/nyttikord/gokord/component"
	"github.com/nyttikord/gokord/discord/types"
	"github.com/nyttikord/gokord/interaction"
)

const (
	ConfigModify = "config_modify"
	OpenConfig   = "config"
)

func ConfigResponse(ctx context.Context, guildID uint64) *interaction.Response {
	cfg := config.GetGuild(ctx, guildID)
	roles := ""
	l := len(cfg.XpRoles) - 1
	slices.SortFunc(cfg.XpRoles, func(xp1, xp2 *config.XpRole) int {
		return int(xp2.XP) - int(xp1.XP)
	})
	for i, r := range cfg.XpRoles {
		if i == l {
			roles += fmt.Sprintf("> Niveau %d - <@&%d>", exp.Level(r.XP), r.RoleID)
		} else {
			roles += fmt.Sprintf("> Niveau %d - <@&%d>\n", exp.Level(r.XP), r.RoleID)
		}
	}
	if len(roles) == 0 {
		roles = "Aucun rôle configuré"
	}
	disChans := strings.Split(cfg.DisabledChannels, ";")
	var disChansDefault []component.SelectMenuDefaultValue
	for _, c := range disChans {
		if c != "" {
			v, _ := strconv.ParseUint(c, 10, 64)
			disChansDefault = append(disChansDefault, component.SelectMenuDefaultValue{
				ID:   v,
				Type: types.SelectMenuDefaultValueChannel,
			})
		}
	}
	var defaultChan []component.SelectMenuDefaultValue
	if cfg.FallbackChannel != 0 {
		defaultChan = append(defaultChan, component.SelectMenuDefaultValue{
			ID:   cfg.FallbackChannel,
			Type: types.SelectMenuDefaultValueChannel,
		})
	}
	zero := 0
	content := []component.Component{
		&component.Container{
			Components: []component.Message{
				&component.TextDisplay{Content: "## Configuration"},
				&component.Separator{},
				&component.TextDisplay{Content: "**Salons par défaut**\n-# Les niveaux obtenue grâce à un appel sont affichés ici"},
				&component.ActionsRow{
					Components: []component.Message{
						&component.SelectMenu{
							MenuType:      types.SelectMenuChannel,
							CustomID:      config.ModifyFallbackChannel,
							Placeholder:   "Pas de salon par défaut",
							MinValues:     &zero,
							MaxValues:     1,
							DefaultValues: defaultChan,
						},
					},
				},
				&component.TextDisplay{Content: "**Salons désactivé**\n-# Les messages ne donneront pas d'expérience dans ces salons"},
				&component.ActionsRow{
					Components: []component.Message{
						&component.SelectMenu{
							MenuType:      types.SelectMenuChannel,
							CustomID:      config.ModifyDisChannel,
							Placeholder:   "Pas de salons désactivé",
							MinValues:     &zero,
							MaxValues:     25,
							DefaultValues: disChansDefault,
						},
					},
				},
				&component.Section{
					Components: []component.Message{
						&component.TextDisplay{Content: "**Rôles de niveau**\n" + roles},
					},
					Accessory: &component.Button{
						Label:    "Modifier",
						Style:    component.ButtonStyleSecondary,
						CustomID: config.ModifyXpRole,
					},
				},
				&component.Section{
					Components: []component.Message{
						&component.TextDisplay{
							Content: fmt.Sprintf("**Jours avant la réduction**\n-# Seule l'expérience gagnée les x derniers jours est comptabilisée dans le niveau par défaut\n%d jours", cfg.DaysXPRemains),
						},
					},
					Accessory: &component.Button{
						Label:    "Modifier",
						Style:    component.ButtonStyleSecondary,
						CustomID: config.ModifyTimeReduce,
					},
				},
			},
		},
	}
	return &interaction.Response{
		Type: types.InteractionResponseChannelMessageWithSource,
		Data: &interaction.ResponseData{
			Components: content,
			Flags:      channel.MessageFlagsEphemeral | channel.MessageFlagsIsComponentsV2,
		},
	}
}

func ConfigCommand(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand) {
	err := interaction.Respond(i.Interaction, ConfigResponse(ctx, i.GuildID)).Do(ctx)
	if err != nil {
		bot.Logger(ctx).Error("sending config", "error", err)
	}
}

func ConfigMessageComponent(ctx context.Context, dg bot.Session, i *interaction.MessageComponent) {
	response := ConfigResponse(ctx, i.GuildID)
	response.Type = types.InteractionResponseUpdateMessage

	err := interaction.Respond(i.Interaction, response).Do(ctx)
	if err != nil {
		bot.Logger(ctx).Error("sending config", "error", err)
	}
}

func ConfigModal(ctx context.Context, dg bot.Session, i *interaction.ModalSubmit) {
	response := ConfigResponse(ctx, i.GuildID)
	response.Type = types.InteractionResponseUpdateMessage

	err := interaction.Respond(i.Interaction, response).Do(ctx)
	if err != nil {
		bot.Logger(ctx).Error("sending config", "error", err)
	}
}