From e00f05d7c636f9a1b7a10930402cfeb9331f6631 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 21 Aug 2025 15:41:40 +0200 Subject: [PATCH] feat(command): fetch stats --- commands/credits.go | 2 +- commands/stats.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ create_db.sh | 4 +-- main.go | 4 +++ updates.json | 4 ++- 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 commands/stats.go diff --git a/commands/credits.go b/commands/credits.go index 9682d0e..5c3c4bd 100644 --- a/commands/credits.go +++ b/commands/credits.go @@ -10,7 +10,7 @@ import ( func Credits(_ *discordgo.Session, i *discordgo.InteractionCreate, _ cmd.OptionMap, resp *cmd.ResponseBuilder) { msg := "**Les Copaings**, le bot gérant les serveurs privés de [anhgelus]().\n" msg += "Code source : \n\n" - msg += "Host du bot : " + gokord.BaseCfg.GetAuthor() + "\n\n" + msg += "Host du bot : " + gokord.BaseCfg.GetAuthor() + ".\n\n" msg += "Utilise :\n- [anhgelus/gokord]()" err := resp.SetMessage(msg).Send() if err != nil { diff --git a/commands/stats.go b/commands/stats.go new file mode 100644 index 0000000..80a9b1b --- /dev/null +++ b/commands/stats.go @@ -0,0 +1,62 @@ +package commands + +import ( + "time" + + "git.anhgelus.world/anhgelus/les-copaings-bot/config" + "git.anhgelus.world/anhgelus/les-copaings-bot/exp" + "git.anhgelus.world/anhgelus/les-copaings-bot/user" + "github.com/anhgelus/gokord" + "github.com/anhgelus/gokord/cmd" + "github.com/anhgelus/gokord/logger" + "github.com/bwmarrin/discordgo" +) + +type data struct { + CreatedAt time.Time + XP int + CopaingID int + copaing *user.Copaing +} + +func Stats(_ *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 { + in := v.IntValue() + if in < 0 || uint(in) > days { + if err := resp.SetMessage("Nombre de jours invalide").IsEphemeral().Send(); err != nil { + logger.Alert("commands/stats.go - Sending invalid days", err.Error()) + } + return + } + days = uint(in) + } + var stats []*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 < ?`, + i.GuildID, exp.TimeStampNDaysBefore(days), + ) + if res.Error != nil { + logger.Alert("commands/stats.go - Fetching XP data", res.Error.Error(), "guild_id", i.GuildID) + return + } + if err := res.Scan(&stats).Error; err != nil { + logger.Alert("commands/stats.go - Scanning result", err.Error(), "res") + return + } + copaings := map[int]*user.Copaing{} + for _, s := range stats { + c, ok := copaings[s.CopaingID] + if ok { + s.copaing = c + } else { + if err := gokord.DB.First(s.copaing, s.CopaingID).Error; err != nil { + logger.Alert("commands/stats.go - Finding copaing", err.Error(), "id", s.CopaingID) + return + } + copaings[s.CopaingID] = s.copaing + } + } +} diff --git a/create_db.sh b/create_db.sh index b098405..07deca1 100644 --- a/create_db.sh +++ b/create_db.sh @@ -1,5 +1,5 @@ #!/usr/bin/bash podman network create db -podman run -p 5432:5432 --rm --network db --name postgres --env-file .env -v ./data:/var/lib/postgres/data -d postgres:alpine -podman run -p 8080:8080 --rm --network db --name adminer -d adminer \ No newline at end of file +podman run -p 5432:5432 --rm --network db --name postgres --env-file .env -v ./data:/var/lib/postgresql/data -d postgres:alpine +podman run -p 8080:8080 --rm --network db --name adminer -d adminer diff --git a/main.go b/main.go index 9c0a3b7..e2856eb 100644 --- a/main.go +++ b/main.go @@ -84,6 +84,9 @@ func main() { creditsCmd := cmd.New("credits", "Crédits"). SetHandler(commands.Credits) + statsCmd := cmd.New("stats", "Affiche des stats :D"). + SetHandler(commands.Stats) + innovations, err := gokord.LoadInnovationFromJson(updatesData) if err != nil { panic(err) @@ -116,6 +119,7 @@ func main() { resetCmd, resetUserCmd, creditsCmd, + statsCmd, }, AfterInit: func(dg *discordgo.Session) { d := 24 * time.Hour diff --git a/updates.json b/updates.json index debf5a5..452a306 100644 --- a/updates.json +++ b/updates.json @@ -32,7 +32,9 @@ { "version": "3.2.0", "commands": { - "added": [], + "added": [ + "stats" + ], "removed": [], "updated": [ "config"