feat(top): implements new kind of tops

This commit is contained in:
Anhgelus Morhtuuzh 2025-05-13 16:15:47 +02:00
parent 799df74fcd
commit 01bafe9bf1
Signed by: anhgelus
GPG key ID: CAD341EFA92DDDE5
7 changed files with 179 additions and 58 deletions

View file

@ -1,4 +1,4 @@
FROM docker.io/golang:1.23-alpine FROM docker.io/golang:1.24-alpine
WORKDIR /app WORKDIR /app

View file

@ -11,7 +11,6 @@ import (
func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) { func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) {
optMap := utils.GenerateOptionMap(i) optMap := utils.GenerateOptionMap(i)
c := user.GetCopaing(i.Member.User.ID, i.GuildID) // current user = member who used /rank c := user.GetCopaing(i.Member.User.ID, i.GuildID) // current user = member who used /rank
user.LastEventUpdate(s, c) // update exp and reset last event
msg := "Votre niveau" msg := "Votre niveau"
m := i.Member m := i.Member
var err error var err error
@ -21,13 +20,13 @@ func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) {
if u.Bot { if u.Bot {
err = resp.Message("Imagine si les bots avaient un niveau :rolling_eyes:").IsEphemeral().Send() err = resp.Message("Imagine si les bots avaient un niveau :rolling_eyes:").IsEphemeral().Send()
if err != nil { if err != nil {
utils.SendAlert("rank.go - Reply error user is a bot", err.Error()) utils.SendAlert("commands/rank.go - Reply error user is a bot", err.Error())
} }
} }
m, err = s.GuildMember(i.GuildID, u.ID) m, err = s.GuildMember(i.GuildID, u.ID)
if err != nil { if err != nil {
utils.SendAlert( utils.SendAlert(
"rank.go - Fetching guild member", "commands/rank.go - Fetching guild member",
err.Error(), err.Error(),
"discord_id", "discord_id",
u.ID, u.ID,
@ -36,24 +35,39 @@ func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) {
) )
err = resp.Message("Erreur : impossible de récupérer le membre").IsEphemeral().Send() err = resp.Message("Erreur : impossible de récupérer le membre").IsEphemeral().Send()
if err != nil { if err != nil {
utils.SendAlert("rank.go - Reply error fetching guild member", err.Error()) utils.SendAlert("commands/rank.go - Reply error fetching guild member", err.Error())
} }
return return
} }
c = user.GetCopaing(u.ID, i.GuildID) // current user = member targeted by member who wrote /rank c = user.GetCopaing(u.ID, i.GuildID) // current user = member targeted by member who wrote /rank
user.UpdateXP(s, c) // update exp without resetting event
msg = fmt.Sprintf("Le niveau de %s", m.DisplayName()) msg = fmt.Sprintf("Le niveau de %s", m.DisplayName())
} }
lvl := exp.Level(c.XP) xp, err := c.GetXP()
if err != nil {
utils.SendAlert(
"commands/rank.go - Fetching XP",
err.Error(),
"discord_id",
c.ID,
"guild_id",
i.GuildID,
)
err = resp.Message("Erreur : impossible de récupérer l'XP").IsEphemeral().Send()
if err != nil {
utils.SendAlert("commands/rank.go - Reply error fetching xp", err.Error())
}
return
}
lvl := exp.Level(xp)
nxtLvlXP := exp.LevelXP(lvl + 1) nxtLvlXP := exp.LevelXP(lvl + 1)
err = resp.Message(fmt.Sprintf( err = resp.Message(fmt.Sprintf(
"%s : **%d**\n> XP : %d\n> Prochain niveau dans %d XP", "%s : **%d**\n> XP : %d\n> Prochain niveau dans %d XP",
msg, msg,
lvl, lvl,
c.XP, xp,
nxtLvlXP-c.XP, nxtLvlXP-xp,
)).Send() )).Send()
if err != nil { if err != nil {
utils.SendAlert("rank.go - Sending rank", err.Error()) utils.SendAlert("commands/rank.go - Sending rank", err.Error())
} }
} }

View file

@ -34,8 +34,15 @@ func ResetUser(s *discordgo.Session, i *discordgo.InteractionCreate) {
} }
return return
} }
user.GetCopaing(m.ID, i.GuildID).Reset() err := user.GetCopaing(m.ID, i.GuildID).Delete()
if err := resp.Message("Le user bien été reset.").Send(); err != nil { if err != nil {
utils.SendAlert("commands/reset.go - Copaing not deleted", err.Error(), "discord_id", m.ID, "guild_id", i.GuildID)
err = resp.Message("Erreur : impossible de reset l'utilisateur").Send()
if err != nil {
utils.SendAlert("commands/reset.go - Error deleting", err.Error())
}
}
if err = resp.Message("Le user bien été reset.").Send(); err != nil {
utils.SendAlert("commands/reset.go - Sending success (user)", err.Error()) utils.SendAlert("commands/reset.go - Sending success (user)", err.Error())
} }
} }

View file

@ -2,15 +2,14 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/utils" "github.com/anhgelus/gokord/utils"
"github.com/anhgelus/les-copaings-bot/exp" "github.com/anhgelus/les-copaings-bot/exp"
"github.com/anhgelus/les-copaings-bot/user" "github.com/anhgelus/les-copaings-bot/user"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"sync"
) )
func Top(s *discordgo.Session, i *discordgo.InteractionCreate) { func Top(s *discordgo.Session, i *discordgo.InteractionCreate) {
user.LastEventUpdate(s, user.GetCopaing(i.Member.User.ID, i.GuildID))
resp := utils.ResponseBuilder{C: s, I: i} resp := utils.ResponseBuilder{C: s, I: i}
err := resp.IsDeferred().Send() err := resp.IsDeferred().Send()
if err != nil { if err != nil {
@ -18,26 +17,48 @@ func Top(s *discordgo.Session, i *discordgo.InteractionCreate) {
return return
} }
resp.NotDeferred().IsEdit() resp.NotDeferred().IsEdit()
go func() { embeds := make([]*discordgo.MessageEmbed, 3)
var tops []user.Copaing wg := sync.WaitGroup{}
gokord.DB.Where("guild_id = ?", i.GuildID).Limit(10).Order("exp desc").Find(&tops)
msg := "" fn := func(s string, n uint, d int, id int) {
for i, c := range tops { defer wg.Done()
if i == 9 { tops, err := user.GetBestXP(i.GuildID, n, d)
msg += fmt.Sprintf("%d. **<@%s>** - niveau %d", i+1, c.DiscordID, exp.Level(c.XP)) if err != nil {
} else { utils.SendAlert("commands/top.go - Fetching best xp", err.Error(), "n", n, "d", d, "id", id, "guild_id", i.GuildID)
msg += fmt.Sprintf("%d. **<@%s>** - niveau %d\n", i+1, c.DiscordID, exp.Level(c.XP)) embeds[id] = &discordgo.MessageEmbed{
Title: s,
Description: "Erreur : impossible de récupérer la liste",
Color: utils.Error,
} }
return
} }
err = resp.Embeds([]*discordgo.MessageEmbed{ embeds[id] = &discordgo.MessageEmbed{
{ Title: s,
Title: "Top", Description: genTopsMessage(tops),
Description: msg,
Color: utils.Success, Color: utils.Success,
}, }
}).Send() }
wg.Add(3)
go fn("Top full time", 10, -1, 0)
go fn("Top 30 jours", 5, 30, 1)
go fn("Top 7 jours", 5, 7, 2)
go func() {
wg.Wait()
err = resp.Embeds(embeds).Send()
if err != nil { if err != nil {
utils.SendAlert("commands/top.go - Sending response top", err.Error()) utils.SendAlert("commands/top.go - Sending response top", err.Error())
} }
}() }()
} }
func genTopsMessage(tops []user.CopaingAccess) string {
msg := ""
for i, c := range tops {
msg += fmt.Sprintf("%d. **<@%s>** - niveau %d", i+1, c.ToCopaing().DiscordID, exp.Level(c.GetXP()))
if i != len(tops)-1 {
msg += "\n"
}
}
return msg
}

View file

@ -17,10 +17,10 @@ var (
//go:embed updates.json //go:embed updates.json
updatesData []byte updatesData []byte
Version = gokord.Version{ Version = gokord.Version{
Major: 2, Major: 3,
Minor: 4, Minor: 0,
Patch: 0, Patch: 0,
} // git version: 0.4.0 (it's the v2 of the bot) }
stopPeriodicReducer chan<- interface{} stopPeriodicReducer chan<- interface{}
) )

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"github.com/anhgelus/gokord" "github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/utils" "github.com/anhgelus/gokord/utils"
"github.com/anhgelus/les-copaings-bot/config"
"time" "time"
) )
@ -23,6 +22,11 @@ type CopaingXP struct {
CreatedAt time.Time CreatedAt time.Time
} }
type CopaingAccess interface {
ToCopaing() *Copaing
GetXP() uint
}
const ( const (
LastEvent = "last_event" LastEvent = "last_event"
AlreadyRemoved = "already_removed" AlreadyRemoved = "already_removed"
@ -52,30 +56,6 @@ func (c *Copaing) Load() error {
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 { func (c *Copaing) Save() error {
return gokord.DB.Save(c).Error return gokord.DB.Save(c).Error
} }

View file

@ -1,11 +1,30 @@
package user package user
import ( import (
"fmt"
"github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/utils" "github.com/anhgelus/gokord/utils"
"github.com/anhgelus/les-copaings-bot/config"
"github.com/anhgelus/les-copaings-bot/exp" "github.com/anhgelus/les-copaings-bot/exp"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"slices"
"sync"
"time"
) )
type cXP struct {
Cxp uint
*Copaing
}
func (c *cXP) ToCopaing() *Copaing {
return c.Copaing
}
func (c *cXP) GetXP() uint {
return c.Cxp
}
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)
@ -29,3 +48,83 @@ func (c *Copaing) AddXP(s *discordgo.Session, m *discordgo.Member, xp uint, fn f
onNewLevel(s, m, newLevel) onNewLevel(s, m, newLevel)
} }
} }
func (c *Copaing) GetXP() (uint, error) {
cfg := config.GetGuildConfig(c.GuildID)
return c.GetXPForDays(cfg.DaysXPRemains)
}
func (c *Copaing) GetXPForDays(n uint) (uint, error) {
xp := uint(0)
y, m, d := time.Unix(time.Now().Unix()-int64(n*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/xp.go - Scanning rows", err.Error(), "discord_id", c.DiscordID, "guild_id", c.GuildID)
continue
}
xp += cXP.XP
}
return xp, nil
}
// GetBestXP returns n Copaing with the best XP within d days (d <= cfg.DaysXPRemain; d < 0 <=> d = cfg.DaysXPRemain)
//
// This function is slow
func GetBestXP(guildId string, n uint, d int) ([]CopaingAccess, error) {
if d < 0 {
cfg := config.GetGuildConfig(guildId)
d = int(cfg.DaysXPRemains)
}
rows, err := gokord.DB.Model(&Copaing{}).Where("guild_id = ?", guildId).Rows()
defer rows.Close()
if err != nil {
return nil, err
}
var l []*cXP
wg := sync.WaitGroup{}
for rows.Next() {
var c Copaing
err = gokord.DB.ScanRows(rows, &c)
if err != nil {
utils.SendAlert("user/xp.go - Scanning rows", err.Error(), "discord_id", c.DiscordID, "guild_id", guildId)
continue
}
wg.Add(1)
go func() {
defer wg.Done()
xp, err := c.GetXPForDays(uint(d))
if err != nil {
utils.SendAlert("user/xp.go - Fetching xp", err.Error(), "discord_id", c.DiscordID, "guild_id", guildId)
return
}
l = append(l, &cXP{Cxp: xp, Copaing: &c})
}()
}
wg.Wait()
slices.SortFunc(l, func(a, b *cXP) int {
// desc order
if a.Cxp < b.Cxp {
return 1
}
if a.Cxp > b.Cxp {
return -1
}
return 0
})
m := min(len(l), int(n))
cs := make([]CopaingAccess, m)
for i, c := range l[:m] {
cs[i] = c
}
return cs, nil
}