From 1da9d79d70eb7d200e7728e5ed77eac8eda58d5b Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Tue, 11 Feb 2025 20:59:01 +1300 Subject: [PATCH] Allow custom counts --- src/internal/bot/commands.go | 51 ++++++++++++++++++++++++++++++++-- src/internal/db/emoji_usage.go | 4 +-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/internal/bot/commands.go b/src/internal/bot/commands.go index af76d7b..f50c9e2 100644 --- a/src/internal/bot/commands.go +++ b/src/internal/bot/commands.go @@ -9,14 +9,37 @@ import ( ) var ( + integerOptionMinValue = 5.0 + amountKey = "amount" + commands = []*discordgo.ApplicationCommand{ { Name: "show-top-emojis", Description: "Show top emojis", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionInteger, + Name: amountKey, + Description: "Amount to show", + MinValue: &integerOptionMinValue, + MaxValue: 20, + Required: false, + }, + }, }, { Name: "show-top-users", Description: "Show top users", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionInteger, + Name: amountKey, + Description: "Amount to show", + MinValue: &integerOptionMinValue, + MaxValue: 20, + Required: false, + }, + }, }, } @@ -51,7 +74,19 @@ func (bot *Bot) DeregisterCommands() { // showTopEmojis - Show top emojis with users func showTopEmojis(s *discordgo.Session, i *discordgo.InteractionCreate) { - top, err := b.Db.GetTopEmojisForGuild(i.GuildID, 5) + // Access options in the order provided by the user. + options := i.ApplicationCommandData().Options + optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options)) + for _, opt := range options { + optionMap[opt.Name] = opt + } + + amount := int64(5) + if opt, ok := optionMap[amountKey]; ok { + amount = opt.IntValue() + } + + top, err := b.Db.GetTopEmojisForGuild(i.GuildID, amount) if err != nil { slog.Error("Error getting top emojis", "err", err) return @@ -84,7 +119,19 @@ func showTopEmojis(s *discordgo.Session, i *discordgo.InteractionCreate) { // showTopUsers - Show top users with emojis func showTopUsers(s *discordgo.Session, i *discordgo.InteractionCreate) { - top, err := b.Db.GetTopUsersForGuild(i.GuildID, 5) + // Access options in the order provided by the user. + options := i.ApplicationCommandData().Options + optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options)) + for _, opt := range options { + optionMap[opt.Name] = opt + } + + amount := int64(5) + if opt, ok := optionMap[amountKey]; ok { + amount = opt.IntValue() + } + + top, err := b.Db.GetTopUsersForGuild(i.GuildID, amount) if err != nil { slog.Error("Error getting top users", "err", err) return diff --git a/src/internal/db/emoji_usage.go b/src/internal/db/emoji_usage.go index 29d9e3c..a67905a 100644 --- a/src/internal/db/emoji_usage.go +++ b/src/internal/db/emoji_usage.go @@ -11,7 +11,7 @@ func (db *Database) LogEmojiUsage(guildID, channelID, userID, emojiID string) er } // GetTopUsersForGuild - Report usage -func (db *Database) GetTopUsersForGuild(guildID string, num int) (map[string]int64, error) { +func (db *Database) GetTopUsersForGuild(guildID string, num int64) (map[string]int64, error) { data := make(map[string]int64) row, err := db.db.Query( "SELECT user_id, count(*) FROM `emoji_usage` WHERE `guild_id` = ? GROUP BY user_id ORDER BY count(*) DESC LIMIT ?", @@ -60,7 +60,7 @@ func (db *Database) GetTopUsersForGuildEmoji(guildID string, emojiID string, num } // GetTopEmojisForGuild - Report usage -func (db *Database) GetTopEmojisForGuild(guildID string, num int) (map[string]int64, error) { +func (db *Database) GetTopEmojisForGuild(guildID string, num int64) (map[string]int64, error) { data := make(map[string]int64) row, err := db.db.Query( "SELECT emoji_id, count(*) FROM `emoji_usage` WHERE `guild_id` = ? GROUP BY emoji_id ORDER BY count(*) DESC LIMIT ?",