aboutsummaryrefslogtreecommitdiff
path: root/config/xp_reduce.go
blob: c3a3e10eb1891a9d11098f6d1c918a8fd4564a98 (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
package config

import (
	"fmt"
	"strconv"

	"github.com/anhgelus/gokord/cmd"
	"github.com/nyttikord/gokord/bot"
	"github.com/nyttikord/gokord/component"
	"github.com/nyttikord/gokord/discord/types"
	"github.com/nyttikord/gokord/event"
	"github.com/nyttikord/gokord/interaction"
)

const (
	ModifyTimeReduce = "time_reduce"
	TimeReduceSet    = "time_reduce_set"
)

func HandleModifyPeriodicReduceCommand(s bot.Session, i *event.InteractionCreate, _ *interaction.MessageComponentData, _ *cmd.ResponseBuilder) {
	cfg := GetGuildConfig(i.GuildID)
	response := interaction.Response{
		Type: types.InteractionResponseModal,
		Data: &interaction.ResponseData{
			CustomID: TimeReduceSet,
			Title:    "Modifier la durée de l'expérience",
			Components: []component.Component{
				// TODO: When gokord supports it, enable this description again
				// &component.TextDisplay{
				// 	Content: "Seul l'expérience gagnée sur cette période sera comptabilisée dans le niveau par défaut",
				// },
				&component.Label{
					Label: "Durée en jours",
					Component: &component.TextInput{
						CustomID:    TimeReduceSet,
						MinLength:   1,
						MaxLength:   3,
						Style:       component.TextInputShort,
						Placeholder: "Durée en jours",
						Value:       fmt.Sprintf("%d", cfg.DaysXPRemains),
					},
				},
			},
		},
	}
	err := s.InteractionAPI().Respond(i.Interaction, &response)
	if err != nil {
		s.LogError(err, "Sending xp reduce modal")
	}
}

func HandleTimeReduceSet(s bot.Session, i *event.InteractionCreate, data *interaction.ModalSubmitData, resp *cmd.ResponseBuilder) bool {
	v := data.Components[0].(*component.Label).Component.(*component.TextInput).Value
	days, err := strconv.Atoi(v)
	if err != nil {
		err = resp.IsEphemeral().SetMessage(fmt.Sprintf("La valeur indiquée, `%s`, c'est pas un entier.", v)).Send()
		if err != nil {
			s.LogError(err, "Sending bad input message")
		}
		return false
	}
	if days < 30 {
		err = resp.IsEphemeral().SetMessage("Le nombre de jours doit être suppérieur à 30.").Send()
		if err != nil {
			s.LogError(err, "Sending less than 30 days message")
		}
		return false
	}
	cfg := GetGuildConfig(i.GuildID)
	cfg.DaysXPRemains = uint(days)
	err = cfg.Save()
	if err != nil {
		s.LogError(err, "Saving DaysXPRemains configuration")
		return false
	}
	return true
}