aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--commands/top.go41
-rw-r--r--main.go7
2 files changed, 47 insertions, 1 deletions
diff --git a/commands/top.go b/commands/top.go
new file mode 100644
index 0000000..bc444a3
--- /dev/null
+++ b/commands/top.go
@@ -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())
+ }
+ }()
+}
diff --git a/main.go b/main.go
index 4cd19fa..ed84b8a 100644
--- a/main.go
+++ b/main.go
@@ -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,
}