fix(db): wrong relation and bad where condition
This commit is contained in:
parent
01bafe9bf1
commit
61c7bf4567
6 changed files with 26 additions and 19 deletions
|
@ -9,5 +9,6 @@ COPY . .
|
||||||
RUN go mod tidy && go build -o app .
|
RUN go mod tidy && go build -o app .
|
||||||
|
|
||||||
ENV TOKEN=""
|
ENV TOKEN=""
|
||||||
|
ENV TZ="Europe/Paris"
|
||||||
|
|
||||||
CMD ./app -token $TOKEN
|
CMD ./app -token $TOKEN
|
||||||
|
|
|
@ -4,11 +4,11 @@ Bot for the private server Discord "Les Copaings"
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Levels & XP
|
- Levels & CopaingXPs
|
||||||
- Roles management
|
- Roles management
|
||||||
- Purge command
|
- Purge command
|
||||||
|
|
||||||
### XP
|
### CopaingXPs
|
||||||
|
|
||||||
Functions:
|
Functions:
|
||||||
- $xp-message(x;y) = 0.025 x^{1.25}\sqrt{y}+1$ where $x$ is the length of the message and $y$ is the diversity of the
|
- $xp-message(x;y) = 0.025 x^{1.25}\sqrt{y}+1$ where $x$ is the length of the message and $y$ is the diversity of the
|
||||||
|
|
|
@ -45,7 +45,7 @@ func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
xp, err := c.GetXP()
|
xp, err := c.GetXP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendAlert(
|
utils.SendAlert(
|
||||||
"commands/rank.go - Fetching XP",
|
"commands/rank.go - Fetching xp",
|
||||||
err.Error(),
|
err.Error(),
|
||||||
"discord_id",
|
"discord_id",
|
||||||
c.ID,
|
c.ID,
|
||||||
|
|
2
main.go
2
main.go
|
@ -36,7 +36,7 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = gokord.DB.AutoMigrate(&user.Copaing{}, &config.GuildConfig{}, &config.XpRole{})
|
err = gokord.DB.AutoMigrate(&user.Copaing{}, &config.GuildConfig{}, &config.XpRole{}, &user.CopaingXP{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,14 +10,14 @@ import (
|
||||||
type Copaing struct {
|
type Copaing struct {
|
||||||
ID uint `gorm:"primarykey"`
|
ID uint `gorm:"primarykey"`
|
||||||
DiscordID string `gorm:"not null"`
|
DiscordID string `gorm:"not null"`
|
||||||
XP []CopaingXP `gorm:"constraint:OnDelete:SET NULL;"`
|
CopaingXPs []CopaingXP `gorm:"constraint:OnDelete:SET NULL;"`
|
||||||
GuildID string `gorm:"not null"`
|
GuildID string `gorm:"not null"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CopaingXP struct {
|
type CopaingXP struct {
|
||||||
ID uint `gorm:"primarykey"`
|
ID uint `gorm:"primarykey"`
|
||||||
XP uint `gorm:"default:0"`
|
XP uint `gorm:"default:0"`
|
||||||
CopaingID uint `gorm:"not null;constraint:OnDelete:CASCADE;"`
|
CopaingID uint
|
||||||
GuildID string `gorm:"not null;"`
|
GuildID string `gorm:"not null;"`
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ func GetCopaing(discordID string, guildID string) *Copaing {
|
||||||
func (c *Copaing) Load() error {
|
func (c *Copaing) Load() error {
|
||||||
return gokord.DB.
|
return gokord.DB.
|
||||||
Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).
|
Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).
|
||||||
Preload("XP").
|
Preload("CopaingXPs").
|
||||||
FirstOrCreate(c).
|
FirstOrCreate(c).
|
||||||
Error
|
Error
|
||||||
}
|
}
|
||||||
|
|
20
user/xp.go
20
user/xp.go
|
@ -28,13 +28,13 @@ func (c *cXP) GetXP() uint {
|
||||||
func (c *Copaing) AddXP(s *discordgo.Session, m *discordgo.Member, xp uint, fn func(uint, uint)) {
|
func (c *Copaing) AddXP(s *discordgo.Session, m *discordgo.Member, xp uint, fn func(uint, uint)) {
|
||||||
old, err := c.GetXP()
|
old, err := c.GetXP()
|
||||||
pastLevel := exp.Level(old)
|
pastLevel := exp.Level(old)
|
||||||
c.XP = append(c.XP, CopaingXP{CopaingID: c.ID, XP: xp, GuildID: c.GuildID})
|
c.CopaingXPs = append(c.CopaingXPs, CopaingXP{CopaingID: c.ID, XP: xp, GuildID: c.GuildID})
|
||||||
if err = c.Save(); err != nil {
|
if err = c.Save(); err != nil {
|
||||||
utils.SendAlert(
|
utils.SendAlert(
|
||||||
"user/xp.go - Saving user",
|
"user/xp.go - Saving user",
|
||||||
err.Error(),
|
err.Error(),
|
||||||
"exp",
|
"xp",
|
||||||
c.XP,
|
c.CopaingXPs,
|
||||||
"discord_id",
|
"discord_id",
|
||||||
c.DiscordID,
|
c.DiscordID,
|
||||||
"guild_id",
|
"guild_id",
|
||||||
|
@ -56,10 +56,16 @@ func (c *Copaing) GetXP() (uint, error) {
|
||||||
|
|
||||||
func (c *Copaing) GetXPForDays(n uint) (uint, error) {
|
func (c *Copaing) GetXPForDays(n uint) (uint, error) {
|
||||||
xp := uint(0)
|
xp := uint(0)
|
||||||
y, m, d := time.Unix(time.Now().Unix()-int64(n*24*60*60), 0).Date()
|
var y, d int
|
||||||
|
var m time.Month
|
||||||
|
if gokord.Debug {
|
||||||
|
y, m, d = time.Unix(time.Now().Unix()-int64(n*24), 0).Date() // reduce time for debug
|
||||||
|
} else {
|
||||||
|
y, m, d = time.Unix(time.Now().Unix()-int64(n*24*60*60), 0).Date()
|
||||||
|
}
|
||||||
rows, err := gokord.DB.
|
rows, err := gokord.DB.
|
||||||
Model(&CopaingXP{}).
|
Model(&CopaingXP{}).
|
||||||
Where(fmt.Sprintf("created_at >= '%d-%d-%d' and guild_id = ? and discord_id = ?", y, m, d), c.GuildID, c.DiscordID).
|
Where(fmt.Sprintf("created_at >= '%d-%d-%d' and guild_id = ? and copaing_id = ?", y, m, d), c.GuildID, c.DiscordID).
|
||||||
Rows()
|
Rows()
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -69,7 +75,7 @@ func (c *Copaing) GetXPForDays(n uint) (uint, error) {
|
||||||
var cXP CopaingXP
|
var cXP CopaingXP
|
||||||
err = gokord.DB.ScanRows(rows, &cXP)
|
err = gokord.DB.ScanRows(rows, &cXP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendAlert("user/xp.go - Scanning rows", err.Error(), "discord_id", c.DiscordID, "guild_id", c.GuildID)
|
utils.SendAlert("user/xp.go - Scanning rows", err.Error(), "copaing_id", c.DiscordID, "guild_id", c.GuildID)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
xp += cXP.XP
|
xp += cXP.XP
|
||||||
|
@ -96,7 +102,7 @@ func GetBestXP(guildId string, n uint, d int) ([]CopaingAccess, error) {
|
||||||
var c Copaing
|
var c Copaing
|
||||||
err = gokord.DB.ScanRows(rows, &c)
|
err = gokord.DB.ScanRows(rows, &c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.SendAlert("user/xp.go - Scanning rows", err.Error(), "discord_id", c.DiscordID, "guild_id", guildId)
|
utils.SendAlert("user/xp.go - Scanning rows", err.Error(), "guild_id", guildId)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue