feat(command): generate plot and send

This commit is contained in:
Anhgelus Morhtuuzh 2025-08-21 16:50:04 +02:00
parent e00f05d7c6
commit a795e62723
Signed by: anhgelus
GPG key ID: CAD341EFA92DDDE5
3 changed files with 111 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package commands
import (
"bytes"
"time"
"git.anhgelus.world/anhgelus/les-copaings-bot/config"
@ -10,6 +11,10 @@ import (
"github.com/anhgelus/gokord/cmd"
"github.com/anhgelus/gokord/logger"
"github.com/bwmarrin/discordgo"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)
type data struct {
@ -19,7 +24,7 @@ type data struct {
copaing *user.Copaing
}
func Stats(_ *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionMap, resp *cmd.ResponseBuilder) {
func Stats(s *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionMap, resp *cmd.ResponseBuilder) {
cfg := config.GetGuildConfig(i.GuildID)
days := cfg.DaysXPRemains
if v, ok := opt["days"]; ok {
@ -32,7 +37,7 @@ func Stats(_ *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM
}
days = uint(in)
}
var stats []*data
var rawData []*data
res := gokord.DB.Raw(
`SELECT "created_at"::date::text, sum(xp) as xp, copaing_id FROM copaing_xps GROUP BY "created_at"::date `+
` WHERE guild_id = ? and created_at < ?`,
@ -42,12 +47,15 @@ func Stats(_ *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM
logger.Alert("commands/stats.go - Fetching XP data", res.Error.Error(), "guild_id", i.GuildID)
return
}
if err := res.Scan(&stats).Error; err != nil {
if err := res.Scan(&rawData).Error; err != nil {
logger.Alert("commands/stats.go - Scanning result", err.Error(), "res")
return
}
copaings := map[int]*user.Copaing{}
for _, s := range stats {
stats := map[int]*[]*plotter.XY{}
for _, s := range rawData {
c, ok := copaings[s.CopaingID]
if ok {
s.copaing = c
@ -58,5 +66,51 @@ func Stats(_ *discordgo.Session, i *discordgo.InteractionCreate, opt cmd.OptionM
}
copaings[s.CopaingID] = s.copaing
}
pts, ok := stats[s.CopaingID]
if !ok {
pts = &[]*plotter.XY{}
stats[s.CopaingID] = pts
}
t := float64(s.CreatedAt.Unix()-time.Now().Unix()) / (24 * 60 * 60)
*pts = append(*pts, &plotter.XY{
X: t,
Y: float64(s.XP),
})
}
p := plot.New()
p.Title.Text = "XP"
p.X.Label.Text = "Jours"
p.Y.Label.Text = "XP"
for in, c := range copaings {
m, err := s.GuildMember(i.GuildID, c.DiscordID)
if err != nil {
logger.Alert("commands/stats.go - Fetching guild member", err.Error())
return
}
err = plotutil.AddLinePoints(p, m.DisplayName(), stats[in])
if err != nil {
logger.Alert("commands/stats.go - Adding line points", err.Error())
return
}
}
w, err := p.WriterTo(4*vg.Inch, 4*vg.Inch, "png")
if err != nil {
logger.Alert("commands/stats.go - Generating png", err.Error())
return
}
b := new(bytes.Buffer)
_, err = w.WriteTo(b)
if err != nil {
logger.Alert("commands/stats.go - Writing png", err.Error())
}
err = resp.AddFile(&discordgo.File{
Name: "plot.png",
ContentType: "image/png",
Reader: b,
}).Send()
if err != nil {
logger.Alert("commands/stats.go - Sending response", err.Error())
}
}