aboutsummaryrefslogtreecommitdiff
path: root/user/member.go
diff options
context:
space:
mode:
Diffstat (limited to 'user/member.go')
-rw-r--r--user/member.go34
1 files changed, 31 insertions, 3 deletions
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
}