From c408afc8797b0da5e1d73d190a8f5884870b510c Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 13 May 2025 12:50:20 +0200 Subject: style(files): reorganize everything --- user/member.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 user/member.go (limited to 'user/member.go') diff --git a/user/member.go b/user/member.go new file mode 100644 index 0000000..8a33ed3 --- /dev/null +++ b/user/member.go @@ -0,0 +1,107 @@ +package user + +import ( + "fmt" + "github.com/anhgelus/gokord" + "github.com/anhgelus/gokord/utils" + "gorm.io/gorm" +) + +type Copaing struct { + gorm.Model + DiscordID string `gorm:"not null"` + //XP []CopaingXP + XP uint `gorm:"default:0"` + GuildID string `gorm:"not null"` +} + +type leftCopaing struct { + ID uint + StopDelete chan<- interface{} +} + +//type CopaingXP struct { +// gorm.Model +// XP uint `gorm:"default:0"` +// CopaingID uint +//} + +var ( + leftCopaingsMap = map[string]*leftCopaing{} +) + +const ( + LastEvent = "last_event" + AlreadyRemoved = "already_removed" +) + +func GetCopaing(discordID string, guildID string) *Copaing { + c := Copaing{DiscordID: discordID, GuildID: guildID} + if err := c.Load(); err != nil { + utils.SendAlert( + "exp/member.go - Loading user", + err.Error(), + "discord_id", + discordID, + "guild_id", + guildID, + ) + return nil + } + return &c +} + +func (c *Copaing) Load() error { + // check if user left in the past 48 hours + k := c.GuildID + ":" + c.DiscordID + l, ok := leftCopaingsMap[k] + if !ok || l == nil { + // if not, common first or create + return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error + } + // else, getting last data + tmp := Copaing{ + Model: gorm.Model{ + ID: c.ID, + }, + DiscordID: c.DiscordID, + GuildID: c.GuildID, + } + if err := gokord.DB.Unscoped().Find(&tmp).Error; err != nil { + // if error, avoid getting old data and use new one + utils.SendAlert( + "exp/member.go - Getting user in soft delete", err.Error(), + "discord_id", c.DiscordID, + "guild_id", c.DiscordID, + "last_id", l.ID, + ) + return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error + } + // resetting internal data + tmp.Model = gorm.Model{} + l.StopDelete <- true + leftCopaingsMap[k] = nil + // creating new data + err := gokord.DB.Create(&tmp).Error + if err != nil { + return err + } + // delete old data + if err = gokord.DB.Unscoped().Delete(&tmp).Error; err != nil { + utils.SendAlert( + "exp/member.go - Deleting user in soft delete", err.Error(), + "discord_id", c.DiscordID, + "guild_id", c.DiscordID, + "last_id", l.ID, + ) + } + return nil +} + +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) +} -- cgit v1.2.3 From d38c57b83009bf0a8b22bede7ee786d6f54fc66a Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 13 May 2025 13:00:09 +0200 Subject: fix(log): wrong pos in many alerts --- user/member.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'user/member.go') diff --git a/user/member.go b/user/member.go index 8a33ed3..91327ad 100644 --- a/user/member.go +++ b/user/member.go @@ -39,7 +39,7 @@ func GetCopaing(discordID string, guildID string) *Copaing { c := Copaing{DiscordID: discordID, GuildID: guildID} if err := c.Load(); err != nil { utils.SendAlert( - "exp/member.go - Loading user", + "user/member.go - Loading user", err.Error(), "discord_id", discordID, @@ -70,7 +70,7 @@ func (c *Copaing) Load() error { if err := gokord.DB.Unscoped().Find(&tmp).Error; err != nil { // if error, avoid getting old data and use new one utils.SendAlert( - "exp/member.go - Getting user in soft delete", err.Error(), + "user/member.go - Getting user in soft delete", err.Error(), "discord_id", c.DiscordID, "guild_id", c.DiscordID, "last_id", l.ID, @@ -89,7 +89,7 @@ func (c *Copaing) Load() error { // delete old data if err = gokord.DB.Unscoped().Delete(&tmp).Error; err != nil { utils.SendAlert( - "exp/member.go - Deleting user in soft delete", err.Error(), + "user/member.go - Deleting user in soft delete", err.Error(), "discord_id", c.DiscordID, "guild_id", c.DiscordID, "last_id", l.ID, -- cgit v1.2.3 From 6b6c775ab3af659e26f0c79dbabd86d88d2451d5 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 13 May 2025 13:15:59 +0200 Subject: refactor(gorm): remove ...At fields from model --- user/member.go | 80 +++++++++++++--------------------------------------------- 1 file changed, 17 insertions(+), 63 deletions(-) (limited to 'user/member.go') diff --git a/user/member.go b/user/member.go index 91327ad..dc93979 100644 --- a/user/member.go +++ b/user/member.go @@ -4,32 +4,21 @@ import ( "fmt" "github.com/anhgelus/gokord" "github.com/anhgelus/gokord/utils" - "gorm.io/gorm" ) type Copaing struct { - gorm.Model - DiscordID string `gorm:"not null"` - //XP []CopaingXP - XP uint `gorm:"default:0"` - GuildID string `gorm:"not null"` + ID uint `gorm:"primarykey"` + DiscordID string `gorm:"not null"` + XP []CopaingXP `gorm:"constraint:OnDelete:SET NULL;"` + GuildID string `gorm:"not null"` } -type leftCopaing struct { - ID uint - StopDelete chan<- interface{} +type CopaingXP struct { + ID uint `gorm:"primarykey"` + XP uint `gorm:"default:0"` + CopaingID uint `gorm:"not null;constraint:OnDelete:CASCADE;"` } -//type CopaingXP struct { -// gorm.Model -// XP uint `gorm:"default:0"` -// CopaingID uint -//} - -var ( - leftCopaingsMap = map[string]*leftCopaing{} -) - const ( LastEvent = "last_event" AlreadyRemoved = "already_removed" @@ -52,50 +41,11 @@ func GetCopaing(discordID string, guildID string) *Copaing { } func (c *Copaing) Load() error { - // check if user left in the past 48 hours - k := c.GuildID + ":" + c.DiscordID - l, ok := leftCopaingsMap[k] - if !ok || l == nil { - // if not, common first or create - return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error - } - // else, getting last data - tmp := Copaing{ - Model: gorm.Model{ - ID: c.ID, - }, - DiscordID: c.DiscordID, - GuildID: c.GuildID, - } - if err := gokord.DB.Unscoped().Find(&tmp).Error; err != nil { - // if error, avoid getting old data and use new one - utils.SendAlert( - "user/member.go - Getting user in soft delete", err.Error(), - "discord_id", c.DiscordID, - "guild_id", c.DiscordID, - "last_id", l.ID, - ) - return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error - } - // resetting internal data - tmp.Model = gorm.Model{} - l.StopDelete <- true - leftCopaingsMap[k] = nil - // creating new data - err := gokord.DB.Create(&tmp).Error - if err != nil { - return err - } - // delete old data - if err = gokord.DB.Unscoped().Delete(&tmp).Error; err != nil { - utils.SendAlert( - "user/member.go - Deleting user in soft delete", err.Error(), - "discord_id", c.DiscordID, - "guild_id", c.DiscordID, - "last_id", l.ID, - ) - } - return nil + return gokord.DB. + Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID). + Preload("XP"). + FirstOrCreate(c). + Error } func (c *Copaing) Save() error { @@ -105,3 +55,7 @@ func (c *Copaing) Save() error { func (c *Copaing) GenKey(key string) string { return fmt.Sprintf("%s:%s:%s", c.GuildID, c.DiscordID, key) } + +func (c *Copaing) Delete() error { + return gokord.DB.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c).Error +} -- cgit v1.2.3 From 799df74fcda5266fd295b49fc759c605c815cad9 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 13 May 2025 14:11:11 +0200 Subject: feat(xp): new add --- user/member.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'user/member.go') diff --git a/user/member.go b/user/member.go index dc93979..b61aa76 100644 --- a/user/member.go +++ b/user/member.go @@ -4,6 +4,8 @@ import ( "fmt" "github.com/anhgelus/gokord" "github.com/anhgelus/gokord/utils" + "github.com/anhgelus/les-copaings-bot/config" + "time" ) type Copaing struct { @@ -14,9 +16,11 @@ type Copaing struct { } type CopaingXP struct { - ID uint `gorm:"primarykey"` - XP uint `gorm:"default:0"` - CopaingID uint `gorm:"not null;constraint:OnDelete:CASCADE;"` + ID uint `gorm:"primarykey"` + XP uint `gorm:"default:0"` + CopaingID uint `gorm:"not null;constraint:OnDelete:CASCADE;"` + GuildID string `gorm:"not null;"` + CreatedAt time.Time } const ( @@ -48,6 +52,30 @@ func (c *Copaing) Load() error { Error } +func (c *Copaing) GetXP() (uint, error) { + cfg := config.GetGuildConfig(c.GuildID) + xp := uint(0) + y, m, d := time.Unix(time.Now().Unix()-int64(cfg.DaysXPRemains*24*60*60), 0).Date() + rows, err := gokord.DB. + Model(&CopaingXP{}). + Where(fmt.Sprintf("created_at >= '%d-%d-%d' and guild_id = ? and discord_id = ?", y, m, d), c.GuildID, c.DiscordID). + Rows() + defer rows.Close() + if err != nil { + return 0, err + } + for rows.Next() { + var cXP CopaingXP + err = gokord.DB.ScanRows(rows, &cXP) + if err != nil { + utils.SendAlert("user/member.go - Scaning rows", err.Error(), "discord_id", c.DiscordID, "guild_id", c.GuildID) + continue + } + xp += cXP.XP + } + return xp, nil +} + func (c *Copaing) Save() error { return gokord.DB.Save(c).Error } -- cgit v1.2.3 From 01bafe9bf1de5be4e770b9500480807d4973d8d6 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 13 May 2025 16:15:47 +0200 Subject: feat(top): implements new kind of tops --- user/member.go | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) (limited to 'user/member.go') diff --git a/user/member.go b/user/member.go index b61aa76..cf7c4a4 100644 --- a/user/member.go +++ b/user/member.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/anhgelus/gokord" "github.com/anhgelus/gokord/utils" - "github.com/anhgelus/les-copaings-bot/config" "time" ) @@ -23,6 +22,11 @@ type CopaingXP struct { CreatedAt time.Time } +type CopaingAccess interface { + ToCopaing() *Copaing + GetXP() uint +} + const ( LastEvent = "last_event" AlreadyRemoved = "already_removed" @@ -52,30 +56,6 @@ func (c *Copaing) Load() error { Error } -func (c *Copaing) GetXP() (uint, error) { - cfg := config.GetGuildConfig(c.GuildID) - xp := uint(0) - y, m, d := time.Unix(time.Now().Unix()-int64(cfg.DaysXPRemains*24*60*60), 0).Date() - rows, err := gokord.DB. - Model(&CopaingXP{}). - Where(fmt.Sprintf("created_at >= '%d-%d-%d' and guild_id = ? and discord_id = ?", y, m, d), c.GuildID, c.DiscordID). - Rows() - defer rows.Close() - if err != nil { - return 0, err - } - for rows.Next() { - var cXP CopaingXP - err = gokord.DB.ScanRows(rows, &cXP) - if err != nil { - utils.SendAlert("user/member.go - Scaning rows", err.Error(), "discord_id", c.DiscordID, "guild_id", c.GuildID) - continue - } - xp += cXP.XP - } - return xp, nil -} - func (c *Copaing) Save() error { return gokord.DB.Save(c).Error } -- cgit v1.2.3 From 61c7bf4567249da0d13c2f738e56754a2c181c99 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 13 May 2025 16:52:04 +0200 Subject: fix(db): wrong relation and bad where condition --- user/member.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'user/member.go') diff --git a/user/member.go b/user/member.go index cf7c4a4..71a369b 100644 --- a/user/member.go +++ b/user/member.go @@ -8,16 +8,16 @@ import ( ) type Copaing struct { - ID uint `gorm:"primarykey"` - DiscordID string `gorm:"not null"` - XP []CopaingXP `gorm:"constraint:OnDelete:SET NULL;"` - GuildID string `gorm:"not null"` + ID uint `gorm:"primarykey"` + DiscordID string `gorm:"not null"` + CopaingXPs []CopaingXP `gorm:"constraint:OnDelete:SET NULL;"` + GuildID string `gorm:"not null"` } type CopaingXP struct { - ID uint `gorm:"primarykey"` - XP uint `gorm:"default:0"` - CopaingID uint `gorm:"not null;constraint:OnDelete:CASCADE;"` + ID uint `gorm:"primarykey"` + XP uint `gorm:"default:0"` + CopaingID uint GuildID string `gorm:"not null;"` CreatedAt time.Time } @@ -51,7 +51,7 @@ func GetCopaing(discordID string, guildID string) *Copaing { func (c *Copaing) Load() error { return gokord.DB. Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID). - Preload("XP"). + Preload("CopaingXPs"). FirstOrCreate(c). Error } -- cgit v1.2.3