feat(command): top

This commit is contained in:
Anhgelus Morhtuuzh 2024-04-15 18:30:58 +02:00
parent 7d4e050c3f
commit 75df974f6f
No known key found for this signature in database
GPG key ID: CF4550297832A29F
2 changed files with 47 additions and 1 deletions

41
commands/top.go Normal file
View file

@ -0,0 +1,41 @@
package commands
import (
"fmt"
"github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/utils"
"github.com/anhgelus/les-copaings-bot/xp"
"github.com/bwmarrin/discordgo"
)
func Top(s *discordgo.Session, i *discordgo.InteractionCreate) {
resp := utils.ResponseBuilder{C: s, I: i}
err := resp.IsDeferred().Send()
if err != nil {
utils.SendAlert("commands/top.go - Sending deferred", err.Error())
return
}
resp.NotDeferred().IsEdit()
go func() {
var tops []xp.Copaing
gokord.DB.Where("guild_id = ?", i.GuildID).Limit(10).Order("xp desc").Find(&tops)
msg := ""
for i, c := range tops {
if i == 9 {
msg += fmt.Sprintf("%d. **<@%s>** - niveau %d", i+1, c.DiscordID, xp.Level(c.XP))
} else {
msg += fmt.Sprintf("%d. **<@%s>** - niveau %d\n", i+1, c.DiscordID, xp.Level(c.XP))
}
}
err = resp.Embeds([]*discordgo.MessageEmbed{
{
Title: "Top",
Description: msg,
Color: utils.Success,
},
}).Send()
if err != nil {
utils.SendAlert("commands/top.go - Sending response top", err.Error())
}
}()
}

View file

@ -92,7 +92,11 @@ func main() {
"Salon textuel par défaut",
).IsRequired()).
SetHandler(commands.ConfigFallbackChannel),
)
).SetPermission(gokord.AdminPermission)
topCmd := gokord.NewCommand("top", "Copaings les plus actifs").
HasOption().
SetHandler(commands.Top)
bot := gokord.Bot{
Token: token,
@ -106,6 +110,7 @@ func main() {
Commands: []*gokord.GeneralCommand{
rankCmd,
configCmd,
topCmd,
},
AfterInit: afterInit,
}