[Refactor] Config command #1

Merged
anhgelus merged 23 commits from refactor/config-command into main 2025-08-21 12:54:53 +00:00
5 changed files with 27 additions and 20 deletions
Showing only changes of commit ecf41adc3c - Show all commits

View file

@ -1,11 +1,12 @@
package config
import (
"strconv"
"github.com/anhgelus/gokord/cmd"
"github.com/anhgelus/gokord/component"
"github.com/anhgelus/gokord/logger"
"github.com/bwmarrin/discordgo"
"strconv"
)
const (
@ -14,11 +15,13 @@ const (
)
func HandleModifyPeriodicReduce(_ *discordgo.Session, _ *discordgo.InteractionCreate, _ discordgo.MessageComponentInteractionData, resp *cmd.ResponseBuilder) {
err := resp.IsModal().SetComponents(component.New().ForModal().Add(component.NewActionRow().Add(
component.NewTextInput(TimeReduceSet, "Jours avant la réduction", discordgo.TextInputShort).
SetMinLength(1).
SetMaxLength(3),
))).Send()
err := resp.IsModal().
SetCustomID(TimeReduceSet).
SetComponents(component.New().ForModal().Add(component.NewActionRow().ForModal().Add(
component.NewTextInput(TimeReduceSet, "Jours avant la réduction", discordgo.TextInputShort).
SetMinLength(1).
SetMaxLength(3),
))).Send()
if err != nil {
logger.Alert("config/xp_reduce.go - Sending modal for periodic reduce", err.Error())
}
@ -31,14 +34,14 @@ func HandleTimeReduceSet(_ *discordgo.Session, i *discordgo.InteractionCreate, d
if err != nil {
logger.Debug(err.Error())
if err = resp.SetMessage("Nombres de jours invalides. Merci de mettre un entier.").Send(); err != nil {
logger.Alert("config/channel.go - Sending bad input", err.Error())
logger.Alert("config/xp_reduce.go - Sending bad input", err.Error())
}
return
}
if days < 30 {
err = resp.SetMessage("Le nombre de jours est inférieur à 30.").Send()
if err != nil {
logger.Alert("commands/config.go - Days < 30 (fallback)", err.Error())
logger.Alert("config/xp_reduce.go - Days < 30 (fallback)", err.Error())
}
return
}
@ -47,11 +50,11 @@ func HandleTimeReduceSet(_ *discordgo.Session, i *discordgo.InteractionCreate, d
if err = cfg.Save(); err != nil {
logger.Alert("config/channel.go - Saving days xp remains", err.Error())
if err = resp.SetMessage("Erreur lors de la sauvegarde du salon").Send(); err != nil {
logger.Alert("config/channel.go - Sending error while saving days xp remains", err.Error())
logger.Alert("config/xp_reduce.go - Sending error while saving days xp remains", err.Error())
}
return
}
if err = resp.SetMessage("Modification sauvegardée.").Send(); err != nil {
logger.Alert("config/channel.go - Sending days saved", err.Error())
logger.Alert("config/xp_reduce.go - Sending days saved", err.Error())
}
}

View file

@ -2,13 +2,14 @@ package config
import (
"fmt"
"strconv"
"time"
"github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/cmd"
"github.com/anhgelus/gokord/component"
"github.com/anhgelus/gokord/logger"
"github.com/bwmarrin/discordgo"
"strconv"
"time"
)
type XpRole struct {
@ -52,7 +53,7 @@ func HandleModifyXpRole(_ *discordgo.Session, _ *discordgo.InteractionCreate, _
),
)).Send()
if err != nil {
logger.Alert("config/guild.go - Sending config", err.Error())
logger.Alert("config/xp_reduce.go - Sending config", err.Error())
}
}
@ -63,7 +64,8 @@ func HandleXpRoleAddEdit(_ *discordgo.Session, _ *discordgo.InteractionCreate, d
}
err := resp.IsModal().
SetTitle("Role").
SetComponents(component.New().ForModal().Add(component.NewActionRow().Add(
SetCustomID(cID).
SetComponents(component.New().ForModal().Add(component.NewActionRow().ForModal().Add(
component.NewTextInput(cID, "Niveau", discordgo.TextInputShort).
SetPlaceholder("5").
IsRequired().
@ -72,7 +74,7 @@ func HandleXpRoleAddEdit(_ *discordgo.Session, _ *discordgo.InteractionCreate, d
))).
Send()
if err != nil {
logger.Alert("config/guild.go - Sending modal to add/edit", err.Error())
logger.Alert("config/xp_reduce.go - Sending modal to add/edit", err.Error())
}
}
@ -142,7 +144,7 @@ func HandleXpRoleDel(_ *discordgo.Session, _ *discordgo.InteractionCreate, _ dis
SetComponents(component.New().Add(component.NewActionRow().Add(component.NewRoleSelect(XpRoleDelRole)))).
Send()
if err != nil {
logger.Alert("config/guild.go - Sending response to del", err.Error())
logger.Alert("config/xp_reduce.go - Sending response to del", err.Error())
}
}
@ -204,10 +206,10 @@ func HandleXpRoleLevel(_ *discordgo.Session, i *discordgo.InteractionCreate, dat
SetComponents(component.New().Add(component.NewActionRow().Add(component.NewRoleSelect(cID)))).
Send()
if err != nil {
logger.Alert("config/guild.go - Sending response to add/edit", err.Error())
logger.Alert("config/xp_reduce.go - Sending response to add/edit", err.Error())
}
}
func getKeyConfigRole(i *discordgo.InteractionCreate) string {
return fmt.Sprintf("r:%s:%s", i.GuildID, i.User.ID)
return fmt.Sprintf("r:%s:%s", i.GuildID, i.Member.User.ID)
}

View file

@ -53,7 +53,7 @@ func LevelXP(level uint) uint {
func TimeStampNDaysBefore(n uint) string {
var unix time.Time
if gokord.Debug {
unix = time.Unix(time.Now().Unix()-int64(n), 0) // reduce time for debug
unix = time.Unix(time.Now().Unix()-int64(n)*6, 0) // reduce time for debug
} else {
unix = time.Unix(time.Now().Unix()-int64(n*24*60*60), 0)
}

2
go.mod
View file

@ -3,7 +3,7 @@ module github.com/anhgelus/les-copaings-bot
go 1.24
require (
github.com/anhgelus/gokord v0.11.1-0.20250821115246-50e5f7d17717
github.com/anhgelus/gokord v0.11.1-0.20250821122244-0aee6c37eef6
github.com/bwmarrin/discordgo v0.29.0
github.com/joho/godotenv v1.5.1
github.com/pelletier/go-toml/v2 v2.2.4

2
go.sum
View file

@ -14,6 +14,8 @@ github.com/anhgelus/gokord v0.11.1-0.20250807111049-5de23912c524 h1:mK7UtqJPNYhS
github.com/anhgelus/gokord v0.11.1-0.20250807111049-5de23912c524/go.mod h1:4xpwLzIG34/XG9QZiPsnYScQhckiCpQMAI0CjP0Nc2k=
github.com/anhgelus/gokord v0.11.1-0.20250821115246-50e5f7d17717 h1:KfcBHUpwbffRO6aIITq7iN7cP7KcKmUnIE+eWiwsYYw=
github.com/anhgelus/gokord v0.11.1-0.20250821115246-50e5f7d17717/go.mod h1:4xpwLzIG34/XG9QZiPsnYScQhckiCpQMAI0CjP0Nc2k=
github.com/anhgelus/gokord v0.11.1-0.20250821122244-0aee6c37eef6 h1:4eO/9UqTPfrKyss2CeLafeKeR06bgoFihudKOdLpWpI=
github.com/anhgelus/gokord v0.11.1-0.20250821122244-0aee6c37eef6/go.mod h1:4xpwLzIG34/XG9QZiPsnYScQhckiCpQMAI0CjP0Nc2k=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=