From 3e65b4f6281ddc4039a27a62428db8a95ffc3677 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 22 Jan 2026 21:53:29 +0100 Subject: refactor(): completely remove old gokord and finish to update everything to use contexts --- commands/config.go | 10 +++++----- commands/credits.go | 6 +++--- commands/rank.go | 18 ++++++------------ commands/reset.go | 10 +++++----- commands/stats.go | 16 +++++++++------- commands/top.go | 2 +- 6 files changed, 29 insertions(+), 33 deletions(-) (limited to 'commands') diff --git a/commands/config.go b/commands/config.go index 12d1c4d..d9b04ec 100644 --- a/commands/config.go +++ b/commands/config.go @@ -20,8 +20,8 @@ const ( OpenConfig = "config" ) -func ConfigResponse(guildID string) *interaction.Response { - cfg := config.GetGuildConfig(guildID) +func ConfigResponse(ctx context.Context, guildID string) *interaction.Response { + cfg := config.GetGuildConfig(ctx, guildID) roles := "" l := len(cfg.XpRoles) - 1 slices.SortFunc(cfg.XpRoles, func(xp1, xp2 config.XpRole) int { @@ -121,14 +121,14 @@ func ConfigResponse(guildID string) *interaction.Response { } func ConfigCommand(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand) { - err := dg.InteractionAPI().Respond(i.Interaction, ConfigResponse(i.GuildID)).Do(ctx) + err := dg.InteractionAPI().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(i.GuildID) + response := ConfigResponse(ctx, i.GuildID) response.Type = types.InteractionResponseUpdateMessage err := dg.InteractionAPI().Respond(i.Interaction, response).Do(ctx) @@ -138,7 +138,7 @@ func ConfigMessageComponent(ctx context.Context, dg bot.Session, i *interaction. } func ConfigModal(ctx context.Context, dg bot.Session, i *interaction.ModalSubmit) { - response := ConfigResponse(i.GuildID) + response := ConfigResponse(ctx, i.GuildID) response.Type = types.InteractionResponseUpdateMessage err := dg.InteractionAPI().Respond(i.Interaction, response).Do(ctx) diff --git a/commands/credits.go b/commands/credits.go index 21dd8c7..a27d2cc 100644 --- a/commands/credits.go +++ b/commands/credits.go @@ -3,7 +3,7 @@ package commands import ( "context" - "github.com/anhgelus/gokord" + "git.anhgelus.world/anhgelus/les-copaings-bot/common" "github.com/nyttikord/gokord/bot" "github.com/nyttikord/gokord/interaction" ) @@ -11,8 +11,8 @@ import ( func Credits(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand) { msg := "**Les Copaings**, le bot gérant les serveurs privés de [anhgelus]().\n" msg += "Code source : \n\n" - msg += "Host du bot : " + gokord.BaseCfg.GetAuthor() + ".\n\n" - msg += "Utilise :\n- [anhgelus/gokord]()\n" + msg += "Host du bot : " + common.GetAuthor(ctx) + ".\n\n" + msg += "Utilise :\n- [nyttikord/gokord]()\n" msg += "- [Inter]()" resp := interaction.NewMessageResponse().Message(msg).Response() err := dg.InteractionAPI().Respond(i.Interaction, resp).Do(ctx) diff --git a/commands/rank.go b/commands/rank.go index 53e4724..07bbe75 100644 --- a/commands/rank.go +++ b/commands/rank.go @@ -15,9 +15,9 @@ func Rank(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand msg := "Votre niveau" m := i.Member opts := i.OptionMap() - var resp *interaction.Response + resp := interaction.NewMessageResponse() defer func() { - err := dg.InteractionAPI().Respond(i.Interaction, resp).Do(ctx) + err := dg.InteractionAPI().Respond(i.Interaction, resp.Response()).Do(ctx) if err != nil { bot.Logger(ctx).Error("replying to interaction", "error", err) } @@ -26,19 +26,13 @@ func Rank(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand if v, ok := opts["copaing"]; ok { u := v.UserValue(ctx) if u.Bot { - resp = interaction.NewMessageResponse(). - IsEphemeral(). - Message("Imagine si les bots avaient un niveau :rolling_eyes:"). - Response() + resp.IsEphemeral().Message("Imagine si les bots avaient un niveau :rolling_eyes:") return } m, err = dg.GuildAPI().Member(i.GuildID, u.ID).Do(ctx) if err != nil { bot.Logger(ctx).Error("fetching guild member", "error", err, "user", u.Username, "guild", i.GuildID) - resp = interaction.NewMessageResponse(). - IsEphemeral(). - Message("Erreur : impossible de récupérer le membre"). - Response() + resp.IsEphemeral().Message("Erreur : impossible de récupérer le membre") return } c = user.GetCopaing(ctx, u.ID, i.GuildID) // current user = member targeted by member who wrote /rank @@ -47,8 +41,8 @@ func Rank(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand xp := c.XP lvl := exp.Level(xp) nxtLvlXP := exp.LevelXP(lvl + 1) - resp = interaction.NewMessageResponse().Message(fmt.Sprintf( + resp.Message(fmt.Sprintf( "%s : **%d**\n> XP : %d\n> Prochain niveau dans %d XP", msg, lvl, xp, nxtLvlXP-xp, - )).Response() + )) } diff --git a/commands/reset.go b/commands/reset.go index 9f2debc..d1e04ea 100644 --- a/commands/reset.go +++ b/commands/reset.go @@ -3,8 +3,8 @@ package commands import ( "context" + "git.anhgelus.world/anhgelus/les-copaings-bot/common" "git.anhgelus.world/anhgelus/les-copaings-bot/user" - "github.com/anhgelus/gokord" "github.com/nyttikord/gokord/bot" "github.com/nyttikord/gokord/interaction" ) @@ -12,7 +12,7 @@ import ( func Reset(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand) { var copaings []*user.Copaing //TODO: delete everything from cache - gokord.DB.Where("guild_id = ?", i.GuildID).Delete(&copaings) + common.GetDB(ctx).Where("guild_id = ?", i.GuildID).Delete(&copaings) resp := interaction.NewMessageResponse().IsEphemeral().Message("L'XP a été reset.").Response() err := dg.InteractionAPI().Respond(i.Interaction, resp).Do(ctx) if err != nil { @@ -31,7 +31,7 @@ func ResetUser(ctx context.Context, dg bot.Session, i *interaction.ApplicationCo opts := i.OptionMap() v, ok := opts["user"] if !ok { - resp.Message("Le user n'a pas été renseigné.") + resp.Message("Le copaing n'a pas été renseigné.") return } m := v.UserValue(ctx) @@ -42,8 +42,8 @@ func ResetUser(ctx context.Context, dg bot.Session, i *interaction.ApplicationCo err := user.GetCopaing(ctx, m.ID, i.GuildID).Delete(ctx) if err != nil { bot.Logger(ctx).Error("deleting copaing", "error", err, "user", m.Username, "guild", i.GuildID) - resp.Message("Erreur : impossible de reset l'utilisateur") + resp.Message("Erreur : impossible de reset le copaing") return } - resp.Message("Le user bien été reset.") + resp.Message("Le copaing bien été reset.") } diff --git a/commands/stats.go b/commands/stats.go index 92402c8..e74f3e1 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -15,7 +15,6 @@ import ( "git.anhgelus.world/anhgelus/les-copaings-bot/config" "git.anhgelus.world/anhgelus/les-copaings-bot/exp" "git.anhgelus.world/anhgelus/les-copaings-bot/user" - "github.com/anhgelus/gokord" "github.com/jackc/pgx/v5/pgtype" "github.com/nyttikord/gokord/bot" "github.com/nyttikord/gokord/discord/request" @@ -48,7 +47,7 @@ var colors = []color.RGBA{ } func Stats(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand) { - cfg := config.GetGuildConfig(i.GuildID) + cfg := config.GetGuildConfig(ctx, i.GuildID) days := 15 if common.IsDebug(ctx) { days = 90 @@ -103,7 +102,10 @@ func Stats(ctx context.Context, dg bot.Session, i *interaction.ApplicationComman func statsAll(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand, days int) (io.WriterTo, error) { return stats(ctx, dg, i, days, func(before, after string) *gorm.DB { - return gokord.DB.Raw(before+"WHERE guild_id = ? and created_at > ?"+after, i.GuildID, exp.TimeStampNDaysBefore(uint(days))) + return common.GetDB(ctx).Raw( + before+"WHERE guild_id = ? and created_at > ?"+after, + i.GuildID, exp.TimeStampNDaysBefore(ctx, uint(days)), + ) }) } @@ -113,9 +115,9 @@ func statsMember(ctx context.Context, dg bot.Session, i *interaction.Application return nil, err } return stats(ctx, dg, i, days, func(before, after string) *gorm.DB { - return gokord.DB.Raw( + return common.GetDB(ctx).Raw( before+"WHERE guild_id = ? and created_at > ? and copaing_id = ?"+after, - i.GuildID, exp.TimeStampNDaysBefore(uint(days)), user.GetCopaing(ctx, discordID, i.GuildID).ID, + i.GuildID, exp.TimeStampNDaysBefore(ctx, uint(days)), user.GetCopaing(ctx, discordID, i.GuildID).ID, ) }) } @@ -161,7 +163,7 @@ func stats(ctx context.Context, dg bot.Session, i *interaction.ApplicationComman _, ok := copaings[raw.CopaingID] if !ok { var cp user.Copaing - if err := gokord.DB.First(&cp, raw.CopaingID).Error; err != nil { + if err := common.GetDB(ctx).First(&cp, raw.CopaingID).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { bot.Logger(ctx).Error("finding copaing", "error", err, "copaing", raw.CopaingID) return nil, err @@ -213,7 +215,7 @@ func generatePlot(ctx context.Context, dg bot.Session, i *interaction.Applicatio // set scales p.Title.Text = "XP gagnées" p.X.Label.Text = "Jours" - if gokord.Debug { + if common.IsDebug(ctx) { p.X.Label.Text = fmt.Sprintf("%d secondes", exp.DebugFactor) } p.Y.Label.Text = "XP" diff --git a/commands/top.go b/commands/top.go index 867cf26..7b57d8f 100644 --- a/commands/top.go +++ b/commands/top.go @@ -26,7 +26,7 @@ func Top(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand) } } - cfg := config.GetGuildConfig(i.GuildID) + cfg := config.GetGuildConfig(ctx, i.GuildID) if cfg.DaysXPRemains > 30 { wg.Go(func() { fn(fmt.Sprintf("Top %d jours", cfg.DaysXPRemains), 10, -1, 0) -- cgit v1.2.3