From 1f5c649169591472572cb2874d2e70eb96ca7896 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 18 Apr 2024 12:32:29 +0200 Subject: [PATCH] feat(deps): update to gokord v0.3.0 --- commands/config.go | 85 +++++++++++++++++++++++++++++++++++----- commands/rank.go | 15 ++++++- config/guild.go | 17 +++++--- go.mod | 2 +- go.sum | 2 + xp/level.go | 26 ++++++++++++- xp/member.go | 97 +++++++++++++++++++++++++++------------------- 7 files changed, 184 insertions(+), 60 deletions(-) diff --git a/commands/config.go b/commands/config.go index c308f6d..1e680c7 100644 --- a/commands/config.go +++ b/commands/config.go @@ -111,6 +111,7 @@ func ConfigXP(s *discordgo.Session, i *discordgo.InteractionCreate) { cfg := config.GetGuildConfig(i.GuildID) // add or delete or edit + var err error switch ts { case "add": for _, r := range cfg.XpRoles { @@ -126,7 +127,19 @@ func ConfigXP(s *discordgo.Session, i *discordgo.InteractionCreate) { XP: exp, RoleID: role.ID, }) - cfg.Save() + err = cfg.Save() + if err != nil { + utils.SendAlert( + "commands/config.go - Saving config", + err.Error(), + "guild_id", + i.GuildID, + "role_id", + role.ID, + "type", + "add", + ) + } case "del": _, r := cfg.FindXpRole(role.ID) if r == nil { @@ -136,7 +149,19 @@ func ConfigXP(s *discordgo.Session, i *discordgo.InteractionCreate) { } return } - gokord.DB.Delete(r) + err = gokord.DB.Delete(r).Error + if err != nil { + utils.SendAlert( + "commands/config.go - Deleting entry", + err.Error(), + "guild_id", + i.GuildID, + "role_id", + role.ID, + "type", + "del", + ) + } case "edit": _, r := cfg.FindXpRole(role.ID) if r == nil { @@ -147,7 +172,19 @@ func ConfigXP(s *discordgo.Session, i *discordgo.InteractionCreate) { return } r.XP = exp - gokord.DB.Save(r) + err = gokord.DB.Save(r).Error + if err != nil { + utils.SendAlert( + "commands/config.go - Saving config", + err.Error(), + "guild_id", + i.GuildID, + "role_id", + role.ID, + "type", + "edit", + ) + } default: err := resp.Message("Le type d'action n'est pas valide.").Send() if err != nil { @@ -155,9 +192,13 @@ func ConfigXP(s *discordgo.Session, i *discordgo.InteractionCreate) { } return } - err := resp.Message("La configuration a bien été mise à jour.").Send() if err != nil { - utils.SendAlert("commands/config.go - Config updated", err.Error()) + err = resp.Message("Il y a eu une erreur lors de la modification de de la base de données.").Send() + } else { + err = resp.Message("La configuration a bien été mise à jour.").Send() + } + if err != nil { + utils.SendAlert("commands/config.go - Config updated message", err.Error()) } } @@ -212,8 +253,22 @@ func ConfigChannel(s *discordgo.Session, i *discordgo.InteractionCreate) { return } // save - cfg.Save() - err := resp.Message("Modification sauvegardé.").Send() + err := cfg.Save() + if err != nil { + utils.SendAlert( + "commands/config.go - Saving config", + err.Error(), + "guild_id", + i.GuildID, + "type", + ts, + "channel_id", + channel.ID, + ) + err = resp.Message("Il y a eu une erreur lors de la modification de de la base de données.").Send() + } else { + err = resp.Message("Modification sauvegardé.").Send() + } if err != nil { utils.SendAlert("commands/config.go - Modification saved message", err.Error()) } @@ -243,8 +298,20 @@ func ConfigFallbackChannel(s *discordgo.Session, i *discordgo.InteractionCreate) cfg := config.GetGuildConfig(i.GuildID) cfg.FallbackChannel = channel.ID // save - cfg.Save() - err := resp.Message("Salon enregistré.").Send() + err := cfg.Save() + if err != nil { + utils.SendAlert( + "commands/config.go - Saving config", + err.Error(), + "guild_id", + i.GuildID, + "channel_id", + channel.ID, + ) + err = resp.Message("Il y a eu une erreur lors de la modification de de la base de données.").Send() + } else { + err = resp.Message("Salon enregistré.").Send() + } if err != nil { utils.SendAlert("commands/config.go - Channel saved message", err.Error()) } diff --git a/commands/rank.go b/commands/rank.go index 219977d..2d15f6f 100644 --- a/commands/rank.go +++ b/commands/rank.go @@ -40,8 +40,19 @@ func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) { return } c.DiscordID = u.ID // current copaing = member targeted by member who wrote /rank - c.Load() // reload copaing (change line before) - xp.XPUpdate(s, c) // update xp without resetting event + err = c.Load() // reload copaing (change line before) + if err != nil { + utils.SendAlert( + "commands/rank.go - Loading copaing", + err.Error(), + "discord_id", + u.ID, + "guild_id", + i.GuildID, + ) + return + } + xp.XPUpdate(s, c) // update xp without resetting event msg = fmt.Sprintf("Le niveau de %s", m.DisplayName()) } lvl := xp.Level(c.XP) diff --git a/config/guild.go b/config/guild.go index 00d3bf1..48ebc96 100644 --- a/config/guild.go +++ b/config/guild.go @@ -2,12 +2,14 @@ package config import ( "github.com/anhgelus/gokord" + "github.com/anhgelus/gokord/utils" "gorm.io/gorm" "strings" ) type GuildConfig struct { gorm.Model + gokord.DataBase GuildID string `gorm:"not null"` XpRoles []XpRole DisabledChannels string @@ -23,16 +25,19 @@ type XpRole struct { func GetGuildConfig(guildID string) *GuildConfig { cfg := GuildConfig{GuildID: guildID} - return cfg.Load() + if err := cfg.Load(); err != nil { + utils.SendAlert("config/guild.go - Loading guild config", err.Error(), "guild_id", guildID) + return nil + } + return &cfg } -func (cfg *GuildConfig) Load() *GuildConfig { - gokord.DB.Where("guild_id = ?", cfg.GuildID).Preload("XpRoles").FirstOrCreate(cfg) - return cfg +func (cfg *GuildConfig) Load() error { + return gokord.DB.Where("guild_id = ?", cfg.GuildID).Preload("XpRoles").FirstOrCreate(cfg).Error } -func (cfg *GuildConfig) Save() { - gokord.DB.Save(cfg) +func (cfg *GuildConfig) Save() error { + return gokord.DB.Save(cfg).Error } func (cfg *GuildConfig) IsDisabled(channelID string) bool { diff --git a/go.mod b/go.mod index 304ffb7..063ba65 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/anhgelus/les-copaings-bot go 1.22 require ( - github.com/anhgelus/gokord v0.2.1 + github.com/anhgelus/gokord v0.3.0 github.com/bwmarrin/discordgo v0.28.1 github.com/redis/go-redis/v9 v9.5.1 gorm.io/gorm v1.25.9 diff --git a/go.sum b/go.sum index 6d386ea..a0e6de0 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/anhgelus/gokord v0.2.1-0.20240415131203-b822009881d2 h1:ADL+XltgbvdBU github.com/anhgelus/gokord v0.2.1-0.20240415131203-b822009881d2/go.mod h1:CRyk26IhIZ/0Mkc5/5WOU8C08mGCOqzKzR6eDFfPisI= github.com/anhgelus/gokord v0.2.1 h1:yFn7WM9PJHdMyfGH2GQ2Ubu+qMvfLJ7gUSzfm9zzihY= github.com/anhgelus/gokord v0.2.1/go.mod h1:CRyk26IhIZ/0Mkc5/5WOU8C08mGCOqzKzR6eDFfPisI= +github.com/anhgelus/gokord v0.3.0 h1:6Ubo9zlNSJONpfHiL03oynWqWth9mOG0SDUGNKfEJRU= +github.com/anhgelus/gokord v0.3.0/go.mod h1:CRyk26IhIZ/0Mkc5/5WOU8C08mGCOqzKzR6eDFfPisI= 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= diff --git a/xp/level.go b/xp/level.go index 4830298..328a287 100644 --- a/xp/level.go +++ b/xp/level.go @@ -93,7 +93,18 @@ func LastEventUpdate(s *discordgo.Session, c *Copaing) { ) c.OnNewLevel(s, lvl) } - c.Save() + if err := c.Save(); err != nil { + utils.SendAlert( + "xp/level.go - Saving copaing", + err.Error(), + "xp", + c.XP, + "discord_id", + c.DiscordID, + "guild_id", + c.GuildID, + ) + } } c.SetLastEvent() } @@ -136,7 +147,18 @@ func XPUpdate(s *discordgo.Session, c *Copaing) { c.OnNewLevel(s, lvl) } utils.SendDebug("Save XP", "old", oldXP, "new", c.XP, "user", c.DiscordID) - c.Save() + if err := c.Save(); err != nil { + utils.SendAlert( + "xp/level.go - Saving copaing", + err.Error(), + "xp", + c.XP, + "discord_id", + c.DiscordID, + "guild_id", + c.GuildID, + ) + } } } diff --git a/xp/member.go b/xp/member.go index b4a4bea..9352a30 100644 --- a/xp/member.go +++ b/xp/member.go @@ -16,6 +16,8 @@ import ( type Copaing struct { gorm.Model + gokord.RedisBase + gokord.DataBase DiscordID string `gorm:"not null"` XP uint `gorm:"default:0"` GuildID string `gorm:"not null"` @@ -30,22 +32,50 @@ const ( func GetCopaing(discordID string, guildID string) *Copaing { c := Copaing{DiscordID: discordID, GuildID: guildID} - return c.Load() + if err := c.Load(); err != nil { + utils.SendAlert( + "xp/member.go - Loading copaing", + err.Error(), + "discord_id", + discordID, + "guild_id", + guildID, + ) + return nil + } + return &c } -func (c *Copaing) Load() *Copaing { - gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c) - return c +func (c *Copaing) Load() error { + return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error } -func (c *Copaing) Save() { - gokord.DB.Save(c) +func (c *Copaing) Save() error { + return gokord.DB.Save(c).Error +} + +func (c *Copaing) GenKey(key string) string { + return fmt.Sprintf("%s:%s:%s", c.GuildID, c.DiscordID, key) } func (c *Copaing) AddXP(s *discordgo.Session, m *discordgo.Member, xp uint, fn func(uint, uint)) { pastLevel := Level(c.XP) + old := c.XP c.XP += xp - c.Save() + if err := c.Save(); err != nil { + utils.SendAlert( + "xp/level.go - Saving copaing", + err.Error(), + "xp", + c.XP, + "discord_id", + c.DiscordID, + "guild_id", + c.GuildID, + ) + c.XP = old + return + } newLevel := Level(c.XP) if newLevel > pastLevel { fn(c.XP, newLevel) @@ -59,24 +89,22 @@ func (c *Copaing) SetLastEvent() { utils.SendAlert("xp/member.go - Getting redis client (set)", err.Error()) return } - u := c.GetUserBase() t := time.Now().Unix() - err = client.Set(context.Background(), fmt.Sprintf( - "%s:%s", - u.GenKey(), - LastEvent, - ), strconv.FormatInt(t, 10), 0).Err() + err = client.Set(context.Background(), c.GenKey(LastEvent), strconv.FormatInt(t, 10), 0).Err() if err != nil { - utils.SendAlert("xp/member.go - Setting last event", err.Error(), "time", t, "base_key", u.GenKey()) + utils.SendAlert("xp/member.go - Setting last event", err.Error(), "time", t, "base_key", c.GenKey("")) return } - err = client.Set(context.Background(), fmt.Sprintf( - "%s:%s", - u.GenKey(), - AlreadyRemoved, - ), "0", 0).Err() + err = client.Set(context.Background(), c.GenKey(AlreadyRemoved), "0", 0).Err() if err != nil { - utils.SendAlert("xp/member.go - Setting already removed to 0", err.Error(), "time", t, "base_key", u.GenKey()) + utils.SendAlert( + "xp/member.go - Setting already removed to 0", + err.Error(), + "time", + t, + "base_key", + c.GenKey(""), + ) return } } @@ -87,12 +115,11 @@ func (c *Copaing) HourSinceLastEvent() uint { utils.SendAlert("xp/member.go - Getting redis client (get)", err.Error()) return 0 } - u := c.GetUserBase() - res := client.Get(context.Background(), fmt.Sprintf("%s:%s", u.GenKey(), LastEvent)) + res := client.Get(context.Background(), fmt.Sprintf("%s:%s", c.GenKey(""), LastEvent)) if errors.Is(res.Err(), redis.Nil) { return 0 } else if res.Err() != nil { - utils.SendAlert("xp/member.go - Getting last event", res.Err().Error(), "base_key", u.GenKey()) + utils.SendAlert("xp/member.go - Getting last event", res.Err().Error(), "base_key", c.GenKey("")) return 0 } t := time.Now().Unix() @@ -102,7 +129,7 @@ func (c *Copaing) HourSinceLastEvent() uint { "xp/member.go - Converting time fetched into int (last event)", err.Error(), "base_key", - u.GenKey(), + c.GenKey(""), "val", res.Val(), ) @@ -120,13 +147,8 @@ func (c *Copaing) AddXPAlreadyRemoved(xp uint) uint { utils.SendAlert("xp/member.go - Getting redis client (set)", err.Error()) return 0 } - u := c.GetUserBase() exp := xp + c.XPAlreadyRemoved() - err = client.Set(context.Background(), fmt.Sprintf( - "%s:%s", - u.GenKey(), - AlreadyRemoved, - ), exp, 0).Err() + err = client.Set(context.Background(), c.GenKey(AlreadyRemoved), exp, 0).Err() if err != nil { utils.SendAlert( "xp/member.go - Setting already removed", @@ -134,7 +156,7 @@ func (c *Copaing) AddXPAlreadyRemoved(xp uint) uint { "xp already removed", exp, "base_key", - u.GenKey(), + c.GenKey(""), ) return 0 } @@ -147,12 +169,11 @@ func (c *Copaing) XPAlreadyRemoved() uint { utils.SendAlert("xp/member.go - Getting redis client (xp)", err.Error()) return 0 } - u := c.GetUserBase() - res := client.Get(context.Background(), fmt.Sprintf("%s:%s", u.GenKey(), AlreadyRemoved)) + res := client.Get(context.Background(), fmt.Sprintf("%s:%s", c.GenKey(""), AlreadyRemoved)) if errors.Is(res.Err(), redis.Nil) { return 0 } else if res.Err() != nil { - utils.SendAlert("xp/member.go - Getting already removed", res.Err().Error(), "base_key", u.GenKey()) + utils.SendAlert("xp/member.go - Getting already removed", res.Err().Error(), "base_key", c.GenKey("")) return 0 } xp, err := strconv.Atoi(res.Val()) @@ -161,7 +182,7 @@ func (c *Copaing) XPAlreadyRemoved() uint { "xp/member.go - Converting time fetched into int (already removed)", err.Error(), "base_key", - u.GenKey(), + c.GenKey(""), "val", res.Val(), ) @@ -172,7 +193,7 @@ func (c *Copaing) XPAlreadyRemoved() uint { "xp/member.go - Assertion xp >= 0", "xp is negative", "base_key", - u.GenKey(), + c.GenKey(""), "xp", xp, ) @@ -181,10 +202,6 @@ func (c *Copaing) XPAlreadyRemoved() uint { return uint(xp) } -func (c *Copaing) GetUserBase() *gokord.UserBase { - return &gokord.UserBase{DiscordID: c.DiscordID, GuildID: c.GuildID} -} - func (c *Copaing) Reset() { gokord.DB.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c) }