aboutsummaryrefslogtreecommitdiff
path: root/user/member.go
blob: b61aa769a749c5881ff8435612126db6008411e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package user

import (
	"fmt"
	"github.com/anhgelus/gokord"
	"github.com/anhgelus/gokord/utils"
	"github.com/anhgelus/les-copaings-bot/config"
	"time"
)

type Copaing struct {
	ID        uint        `gorm:"primarykey"`
	DiscordID string      `gorm:"not null"`
	XP        []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;"`
	GuildID   string `gorm:"not null;"`
	CreatedAt time.Time
}

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(
			"user/member.go - Loading user",
			err.Error(),
			"discord_id",
			discordID,
			"guild_id",
			guildID,
		)
		return nil
	}
	return &c
}

func (c *Copaing) Load() error {
	return gokord.DB.
		Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).
		Preload("XP").
		FirstOrCreate(c).
		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
}

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
}