aboutsummaryrefslogtreecommitdiff
path: root/user/member.go
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-01-17 20:52:13 +0000
committerAnhgelus Morhtuuzh <william@herges.fr>2026-01-17 20:52:13 +0000
commita3543d79561a3754540b921c54c3c177016c2397 (patch)
tree318b29652c4f59a7f6a16ff7a566b1a9935d069d /user/member.go
parent05ec1c26fe884097efe8fe1490916c518028e597 (diff)
parentc661541e45dddd6a082af66fcf7df7ba7dfdc6a6 (diff)
Merge pull request '[Perf] Member state' (#5) from perf/member-state into main
Reviewed-on: https://git.anhgelus.world/anhgelus/les-copaings-bot/pulls/5
Diffstat (limited to 'user/member.go')
-rw-r--r--user/member.go33
1 files changed, 25 insertions, 8 deletions
diff --git a/user/member.go b/user/member.go
index 9068a6f..4969f8a 100644
--- a/user/member.go
+++ b/user/member.go
@@ -1,6 +1,7 @@
package user
import (
+ "context"
"time"
"github.com/anhgelus/gokord"
@@ -22,24 +23,33 @@ type CopaingXP struct {
}
type CopaingAccess interface {
- ToCopaing() *Copaing
+ Copaing() *Copaing
GetXP() uint
}
-func GetCopaing(discordID string, guildID string) *Copaing {
- c := Copaing{DiscordID: discordID, GuildID: guildID}
- if err := c.Load(); err != nil {
- panic(err)
+func GetCopaing(ctx context.Context, discordID string, guildID string) *CopaingCached {
+ state := GetState(ctx)
+ cc, err := state.Copaing(guildID, discordID)
+ if err != nil {
+ c := Copaing{DiscordID: discordID, GuildID: guildID}
+ if err := c.load(); err != nil {
+ panic(err)
+ }
+ cc = FromCopaing(&c)
}
- return &c
+ return cc
}
-func (c *Copaing) Load() error {
- return gokord.DB.
+func (c *Copaing) load() error {
+ err := gokord.DB.
Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).
Preload("CopaingXPs").
FirstOrCreate(c).
Error
+ if err != nil {
+ return err
+ }
+ return err
}
func (c *Copaing) Save() error {
@@ -47,5 +57,12 @@ func (c *Copaing) Save() error {
}
func (c *Copaing) Delete() error {
+ err := gokord.DB.
+ Where("copaing_id = ? and guild_id = ?", c.ID, c.GuildID).
+ Delete(&CopaingXP{}).
+ Error
+ if err != nil {
+ return err
+ }
return gokord.DB.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c).Error
}