Compare commits

..

No commits in common. "15448aa0014e1473582227e2ea301df7ccfb9560" and "1975321016e56a3cdf5b3ce27602501d922c3b73" have entirely different histories.

6 changed files with 25 additions and 50 deletions

View file

@ -1,12 +1,11 @@
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 (
@ -15,13 +14,11 @@ const (
)
func HandleModifyPeriodicReduce(_ *discordgo.Session, _ *discordgo.InteractionCreate, _ discordgo.MessageComponentInteractionData, resp *cmd.ResponseBuilder) {
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()
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()
if err != nil {
logger.Alert("config/xp_reduce.go - Sending modal for periodic reduce", err.Error())
}
@ -34,14 +31,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/xp_reduce.go - Sending bad input", err.Error())
logger.Alert("config/channel.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("config/xp_reduce.go - Days < 30 (fallback)", err.Error())
logger.Alert("commands/config.go - Days < 30 (fallback)", err.Error())
}
return
}
@ -50,11 +47,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/xp_reduce.go - Sending error while saving days xp remains", err.Error())
logger.Alert("config/channel.go - Sending error while saving days xp remains", err.Error())
}
return
}
if err = resp.SetMessage("Modification sauvegardée.").Send(); err != nil {
logger.Alert("config/xp_reduce.go - Sending days saved", err.Error())
logger.Alert("config/channel.go - Sending days saved", err.Error())
}
}

View file

@ -2,15 +2,13 @@ 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/anhgelus/les-copaings-bot/exp"
"github.com/bwmarrin/discordgo"
"strconv"
"time"
)
type XpRole struct {
@ -54,7 +52,7 @@ func HandleModifyXpRole(_ *discordgo.Session, _ *discordgo.InteractionCreate, _
),
)).Send()
if err != nil {
logger.Alert("config/xp_reduce.go - Sending config", err.Error())
logger.Alert("config/guild.go - Sending config", err.Error())
}
}
@ -65,8 +63,7 @@ func HandleXpRoleAddEdit(_ *discordgo.Session, _ *discordgo.InteractionCreate, d
}
err := resp.IsModal().
SetTitle("Role").
SetCustomID(cID).
SetComponents(component.New().ForModal().Add(component.NewActionRow().ForModal().Add(
SetComponents(component.New().ForModal().Add(component.NewActionRow().Add(
component.NewTextInput(cID, "Niveau", discordgo.TextInputShort).
SetPlaceholder("5").
IsRequired().
@ -75,7 +72,7 @@ func HandleXpRoleAddEdit(_ *discordgo.Session, _ *discordgo.InteractionCreate, d
))).
Send()
if err != nil {
logger.Alert("config/xp_reduce.go - Sending modal to add/edit", err.Error())
logger.Alert("config/guild.go - Sending modal to add/edit", err.Error())
}
}
@ -145,7 +142,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/xp_reduce.go - Sending response to del", err.Error())
logger.Alert("config/guild.go - Sending response to del", err.Error())
}
}
@ -190,7 +187,7 @@ func HandleXpRoleLevel(_ *discordgo.Session, i *discordgo.InteractionCreate, dat
}
return
}
configModifyMap[k] = exp.LevelXP(uint(in))
configModifyMap[k] = uint(in)
go func(i *discordgo.InteractionCreate, k string) {
time.Sleep(5 * time.Minute)
delete(configModifyMap, k)
@ -207,10 +204,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/xp_reduce.go - Sending response to add/edit", err.Error())
logger.Alert("config/guild.go - Sending response to add/edit", err.Error())
}
}
func getKeyConfigRole(i *discordgo.InteractionCreate) string {
return fmt.Sprintf("r:%s:%s", i.GuildID, i.Member.User.ID)
return fmt.Sprintf("r:%s:%s", i.GuildID, i.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)*6, 0) // reduce time for debug
unix = time.Unix(time.Now().Unix()-int64(n), 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.20250821122244-0aee6c37eef6
github.com/anhgelus/gokord v0.11.1-0.20250807111049-5de23912c524
github.com/bwmarrin/discordgo v0.29.0
github.com/joho/godotenv v1.5.1
github.com/pelletier/go-toml/v2 v2.2.4

4
go.sum
View file

@ -12,10 +12,6 @@ github.com/anhgelus/gokord v0.11.1-0.20250806143823-567c33f63688 h1:0ngeLQxHr80X
github.com/anhgelus/gokord v0.11.1-0.20250806143823-567c33f63688/go.mod h1:4xpwLzIG34/XG9QZiPsnYScQhckiCpQMAI0CjP0Nc2k=
github.com/anhgelus/gokord v0.11.1-0.20250807111049-5de23912c524 h1:mK7UtqJPNYhStRVeZ2N4c89tjlhlRZX0LcLs7TAB34I=
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=

23
main.go
View file

@ -138,26 +138,8 @@ func main() {
}
// interaction: /config
bot.HandleMessageComponent(func(s *discordgo.Session, i *discordgo.InteractionCreate, data discordgo.MessageComponentInteractionData, resp *cmd.ResponseBuilder) {
if len(data.Values) != 1 {
logger.Alert("main.go - Handle config modify", "invalid data values", "values", data.Values)
return
}
switch data.Values[0] {
case config.ModifyXpRole:
config.HandleModifyXpRole(s, i, data, resp)
case config.ModifyFallbackChannel:
config.HandleModifyFallbackChannel(s, i, data, resp)
case config.ModifyDisChannel:
config.HandleModifyDisChannel(s, i, data, resp)
case config.ModifyTimeReduce:
config.HandleModifyPeriodicReduce(s, i, data, resp)
default:
logger.Alert("main.go - Detecting value", "unkown value", "value", data.Values[0])
return
}
}, commands.ConfigModify)
// xp role related
bot.HandleMessageComponent(config.HandleModifyXpRole, config.ModifyXpRole)
bot.HandleMessageComponent(config.HandleXpRoleAddEdit, config.XpRoleAdd)
bot.HandleMessageComponent(config.HandleXpRoleAddEdit, config.XpRoleEdit)
bot.HandleMessageComponent(config.HandleXpRoleAddRole, config.XpRoleAddRole)
@ -167,12 +149,15 @@ func main() {
bot.HandleModal(config.HandleXpRoleLevel, config.XpRoleAddLevel)
bot.HandleModal(config.HandleXpRoleLevel, config.XpRoleEditLevel)
// channel related
bot.HandleMessageComponent(config.HandleModifyFallbackChannel, config.ModifyFallbackChannel)
bot.HandleMessageComponent(config.HandleFallbackChannelSet, config.FallbackChannelSet)
bot.HandleMessageComponent(config.HandleModifyDisChannel, config.ModifyDisChannel)
bot.HandleMessageComponent(config.HandleDisChannel, config.DisChannelAdd)
bot.HandleMessageComponent(config.HandleDisChannel, config.DisChannelDel)
bot.HandleMessageComponent(config.HandleDisChannelAddSet, config.DisChannelAddSet)
bot.HandleMessageComponent(config.HandleDisChannelDelSet, config.DisChannelDelSet)
// reduce related
bot.HandleMessageComponent(config.HandleModifyPeriodicReduce, config.ModifyTimeReduce)
bot.HandleModal(config.HandleTimeReduceSet, config.TimeReduceSet)
// xp handlers