diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-01-22 21:53:29 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-01-22 21:53:29 +0100 |
| commit | 3e65b4f6281ddc4039a27a62428db8a95ffc3677 (patch) | |
| tree | b1005f908be45aa47da48b604f3863ef23a3d7ea /config | |
| parent | 8255a2e51454049f3ac1532f6e1125f528691c37 (diff) | |
refactor(): completely remove old gokord and finish to update everything to use contexts
Diffstat (limited to 'config')
| -rw-r--r-- | config/channel.go | 8 | ||||
| -rw-r--r-- | config/guild.go | 35 | ||||
| -rw-r--r-- | config/xp_reduce.go | 6 | ||||
| -rw-r--r-- | config/xp_role.go | 36 |
4 files changed, 40 insertions, 45 deletions
diff --git a/config/channel.go b/config/channel.go index 6086f09..4650e15 100644 --- a/config/channel.go +++ b/config/channel.go @@ -20,13 +20,13 @@ const ( ) func HandleModifyFallbackChannel(ctx context.Context, dg bot.Session, i *interaction.MessageComponent) bool { - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) var channelID string if len(i.Data.Values) > 0 { channelID = i.Data.Values[0] } cfg.FallbackChannel = channelID - err := cfg.Save() + err := cfg.Save(ctx) if err != nil { bot.Logger(ctx).Error("saving fallback channel", "error", err) return false @@ -35,9 +35,9 @@ func HandleModifyFallbackChannel(ctx context.Context, dg bot.Session, i *interac } func HandleModifyDisChannel(ctx context.Context, dg bot.Session, i *interaction.MessageComponent) bool { - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) cfg.DisabledChannels = strings.Join(i.Data.Values, ";") - err := cfg.Save() + err := cfg.Save(ctx) if err != nil { bot.Logger(ctx).Error("unable to save disabled channel", "error", err) return false diff --git a/config/guild.go b/config/guild.go index c1df2c9..756c0a4 100644 --- a/config/guild.go +++ b/config/guild.go @@ -4,11 +4,11 @@ import ( "context" "strings" - "github.com/anhgelus/gokord" + "git.anhgelus.world/anhgelus/les-copaings-bot/common" "github.com/nyttikord/gokord/bot" ) -type GuildConfig struct { +type Guild struct { ID uint `gorm:"primarykey"` GuildID string `gorm:"not null;unique"` XpRoles []XpRole @@ -36,36 +36,31 @@ type RoleReact struct { CounterID uint `gorm:"-"` } -func GetGuildConfig(guildID string) *GuildConfig { - cfg := GuildConfig{GuildID: guildID} - if err := cfg.Load(); err != nil { +func GetGuildConfig(ctx context.Context, guildID string) *Guild { + cfg := Guild{GuildID: guildID} + if err := cfg.Load(ctx); err != nil { panic(err) } return &cfg } -func (cfg *GuildConfig) Load() error { - return gokord.DB.Where("guild_id = ?", cfg.GuildID).Preload("XpRoles").FirstOrCreate(cfg).Error +func (cfg *Guild) Load(ctx context.Context) error { + return common.GetDB(ctx).Where("guild_id = ?", cfg.GuildID).Preload("XpRoles").FirstOrCreate(cfg).Error } -func (cfg *GuildConfig) Save() error { - return gokord.DB.Save(cfg).Error +func (cfg *Guild) Save(ctx context.Context) error { + return common.GetDB(ctx).Save(cfg).Error } -func (cfg *GuildConfig) IsDisabled(ctx context.Context, s bot.Session, channelID string) bool { +func (cfg *Guild) IsDisabled(ctx context.Context, dg bot.Session, channelID string) bool { ok := true for channelID != "" && ok { ok = !strings.Contains(cfg.DisabledChannels, channelID) - c, err := s.ChannelAPI().State.Channel(channelID) + c, err := dg.ChannelAPI().State.Channel(channelID) if err != nil { bot.Logger(ctx).Error("unable to find channel %s in state", "error", err, "channel", c) - c, err = s.ChannelAPI().Channel(channelID).Do(ctx) - if err == nil { - err = s.ChannelAPI().State.ChannelAdd(c) - if err != nil { - bot.Logger(ctx).Error("unable to add channel to state", "error", err, "channel", c) - } - } else { + c, err = dg.ChannelAPI().Channel(channelID).Do(ctx) + if err != nil { bot.Logger(ctx).Error("unable to fetch channel", "error", err, "channel", c) return false } @@ -78,7 +73,7 @@ func (cfg *GuildConfig) IsDisabled(ctx context.Context, s bot.Session, channelID return !ok } -func (cfg *GuildConfig) FindXpRole(roleID string) (int, *XpRole) { +func (cfg *Guild) FindXpRole(roleID string) (int, *XpRole) { for i, r := range cfg.XpRoles { if r.RoleID == roleID { return i, &r @@ -87,7 +82,7 @@ func (cfg *GuildConfig) FindXpRole(roleID string) (int, *XpRole) { return 0, nil } -func (cfg *GuildConfig) FindXpRoleID(ID uint) (int, *XpRole) { +func (cfg *Guild) FindXpRoleID(ID uint) (int, *XpRole) { for i, r := range cfg.XpRoles { if r.ID == ID { return i, &r diff --git a/config/xp_reduce.go b/config/xp_reduce.go index f8f8d95..aa2c540 100644 --- a/config/xp_reduce.go +++ b/config/xp_reduce.go @@ -16,7 +16,7 @@ const ( ) func HandleModifyPeriodicReduceCommand(ctx context.Context, dg bot.Session, i *interaction.MessageComponent) { - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) resp := interaction.NewModalResponse(). CustomID(TimeReduceSet). Title("Modifier la durée de l'expérience"). @@ -66,9 +66,9 @@ func HandleTimeReduceSet(ctx context.Context, dg bot.Session, i *interaction.Mod } return false } - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) cfg.DaysXPRemains = uint(days) - err = cfg.Save() + err = cfg.Save(ctx) if err != nil { bot.Logger(ctx).Error("saving DaysXPRemains configuration", "error", err) return false diff --git a/config/xp_role.go b/config/xp_role.go index 76389c9..edf5f61 100644 --- a/config/xp_role.go +++ b/config/xp_role.go @@ -6,9 +6,9 @@ import ( "slices" "strconv" + "git.anhgelus.world/anhgelus/les-copaings-bot/common" "git.anhgelus.world/anhgelus/les-copaings-bot/dynamicid" "git.anhgelus.world/anhgelus/les-copaings-bot/exp" - "github.com/anhgelus/gokord" "github.com/nyttikord/gokord/bot" "github.com/nyttikord/gokord/channel" "github.com/nyttikord/gokord/component" @@ -39,7 +39,7 @@ const ( ) func HandleXpRole(ctx context.Context, dg bot.Session, i *interaction.Interaction) { - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) container := component.Container{ Components: []component.Message{ &component.TextDisplay{Content: "## Configuration / Rôles de niveaux"}, @@ -121,14 +121,14 @@ func HandleXpRoleNew(ctx context.Context, dg bot.Session, i *interaction.Message }, }). Response() - err := dg.InteractionAPI().Respond(i.Interaction, resp) + err := dg.InteractionAPI().Respond(i.Interaction, resp).Do(ctx) if err != nil { bot.Logger(ctx).Error("sending modal to add", "error", err) } } func HandleXpRoleEdit(ctx context.Context, dg bot.Session, i *interaction.Interaction, params *XpRoleId) { - config := GetGuildConfig(i.GuildID) + config := GetGuildConfig(ctx, i.GuildID) id := params.ID _, role := config.FindXpRoleID(id) if role == nil { @@ -181,7 +181,7 @@ func HandleXpRoleEdit(ctx context.Context, dg bot.Session, i *interaction.Intera }, } - err := dg.InteractionAPI().Respond(i, response) + err := dg.InteractionAPI().Respond(i, response).Do(ctx) if err != nil { bot.Logger(ctx).Error("sending xp_role config", "error", err) } @@ -190,7 +190,7 @@ func HandleXpRoleEdit(ctx context.Context, dg bot.Session, i *interaction.Intera func HandleXpRoleEditRole(ctx context.Context, dg bot.Session, i *interaction.MessageComponent, params *XpRoleId) { id := params.ID role := i.Data.Values[0] - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) _, xpRole := cfg.FindXpRoleID(id) if xpRole == nil { err := dg.InteractionAPI().Respond(i.Interaction, &interaction.Response{ @@ -199,14 +199,14 @@ func HandleXpRoleEditRole(ctx context.Context, dg bot.Session, i *interaction.Me Flags: channel.MessageFlagsEphemeral, Content: "Impossible de modifier le rôle. Peut-être a-t-il été supprimé ?", }, - }) + }).Do(ctx) if err != nil { bot.Logger(ctx).Error("sending unable to get role message", "error", err) } return } xpRole.RoleID = role - err := gokord.DB.Save(xpRole).Error + err := common.GetDB(ctx).Save(xpRole).Error if err != nil { bot.Logger(ctx).Error("saving config", "error", err, "guild", i.GuildID, "id", id, "type", "add") } @@ -215,7 +215,7 @@ func HandleXpRoleEditRole(ctx context.Context, dg bot.Session, i *interaction.Me func HandleXpRoleEditLevelStart(ctx context.Context, dg bot.Session, i *interaction.MessageComponent, params *XpRoleId) { id := params.ID - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) _, xpRole := cfg.FindXpRoleID(id) if xpRole == nil { err := dg.InteractionAPI().Respond(i.Interaction, &interaction.Response{ @@ -224,7 +224,7 @@ func HandleXpRoleEditLevelStart(ctx context.Context, dg bot.Session, i *interact Flags: channel.MessageFlagsEphemeral, Content: "Impossible de trouver le rôle. Peut-être a-t-il été supprimé ?", }, - }) + }).Do(ctx) if err != nil { bot.Logger(ctx).Error("sending unable to get role message", "error", err) } @@ -251,7 +251,7 @@ func HandleXpRoleEditLevelStart(ctx context.Context, dg bot.Session, i *interact }, }, } - err := dg.InteractionAPI().Respond(i.Interaction, response) + err := dg.InteractionAPI().Respond(i.Interaction, response).Do(ctx) if err != nil { bot.Logger(ctx).Error("sending edit level modal", "error", err) } @@ -275,7 +275,7 @@ func HandleXpRoleEditLevel(ctx context.Context, dg bot.Session, i *interaction.M } xp := exp.LevelXP(uint(level)) - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) _, xpRole := cfg.FindXpRoleID(id) if xpRole == nil { err = dg.InteractionAPI().Respond(i.Interaction, &interaction.Response{ @@ -291,7 +291,7 @@ func HandleXpRoleEditLevel(ctx context.Context, dg bot.Session, i *interaction.M return } xpRole.XP = xp - err = gokord.DB.Save(xpRole).Error + err = common.GetDB(ctx).Save(xpRole).Error if err != nil { bot.Logger(ctx).Error("saving config", "guild", i.GuildID, "id", id, "type", "edit") } @@ -300,7 +300,7 @@ func HandleXpRoleEditLevel(ctx context.Context, dg bot.Session, i *interaction.M func HandleXpRoleDel(ctx context.Context, dg bot.Session, i *interaction.MessageComponent, parameters *XpRoleId) { id := parameters.ID - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) _, role := cfg.FindXpRoleID(id) if role == nil { err := dg.InteractionAPI().Respond(i.Interaction, &interaction.Response{ @@ -309,13 +309,13 @@ func HandleXpRoleDel(ctx context.Context, dg bot.Session, i *interaction.Message Content: "Rôle introuvable. Peut-être a-t-il déjà été supprimé ?", Flags: channel.MessageFlagsEphemeral, }, - }) + }).Do(ctx) if err != nil { bot.Logger(ctx).Error("sending role not found message", "error", err) } return } - err := gokord.DB.Delete(role).Error + err := common.GetDB(ctx).Delete(role).Error if err != nil { bot.Logger(ctx).Error("deleting entry", "error", err, "guild", i.GuildID, "id", id, "type", "del") } @@ -342,12 +342,12 @@ func HandleXpRoleAdd(ctx context.Context, dg bot.Session, i *interaction.ModalSu roleId := i.Data.Components[1].(*component.Label).Component.(*component.SelectMenu).Values[0] - cfg := GetGuildConfig(i.GuildID) + cfg := GetGuildConfig(ctx, i.GuildID) cfg.XpRoles = append(cfg.XpRoles, XpRole{ XP: xp, RoleID: roleId, }) - err = cfg.Save() + err = cfg.Save(ctx) if err != nil { bot.Logger(ctx).Error("saving config", "error", err, "role", roleId, "guild", i.GuildID) return |
