diff --git a/README.md b/README.md index e029932..d9beb48 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,4 @@ Discord bot to measure emoji usage /show-top-emojis # Shows top 5 emojis and their 3 biggest users ``` + diff --git a/src/internal/bot/commands.go b/src/internal/bot/commands.go index ccd0fc9..6394be6 100644 --- a/src/internal/bot/commands.go +++ b/src/internal/bot/commands.go @@ -67,9 +67,9 @@ var ( } commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ - "show-top-emojis": showTopEmojis, - "show-top-users": showTopUsers, - "purge-emojis": purgeRecentEmojis, + "show-top-emojis": showTopEmojis, + "show-top-users": showTopUsers, + "purge-recent-emojis": purgeRecentEmojis, } ) @@ -137,7 +137,12 @@ func showTopEmojis(s *discordgo.Session, i *discordgo.InteractionCreate) { sort.Ints(subkeys) users := []string{} - msg += fmt.Sprintf("%s: %d", top[v].EmojiID, top[v].Count) + if top[v].EmojiID == top[v].EmojiName { + // Handle bad data with stock emojis + msg += fmt.Sprintf(":%s: %d", top[v].EmojiName, top[v].Count) + } else { + msg += fmt.Sprintf("<:%s:%s> %d", top[v].EmojiName, top[v].EmojiID, top[v].Count) + } for _, sv := range subkeys { users = append(users, fmt.Sprintf("<@%s>: %d", topUsers[sv].EmojiID, topUsers[sv].Count)) } @@ -197,7 +202,12 @@ func showTopUsers(s *discordgo.Session, i *discordgo.InteractionCreate) { users := []string{} msg += fmt.Sprintf("<@%s>: %d", top[v].EmojiID, top[v].Count) for _, sv := range subkeys { - users = append(users, fmt.Sprintf("%s: %d", topUsers[sv].EmojiID, topUsers[sv].Count)) + if topUsers[sv].EmojiID == topUsers[sv].EmojiName { + // Handle bad data with stock emojis + users = append(users, fmt.Sprintf(":%s: %d", topUsers[sv].EmojiName, topUsers[sv].Count)) + } else { + users = append(users, fmt.Sprintf("<:%s:%s> %d", topUsers[sv].EmojiName, topUsers[sv].EmojiID, topUsers[sv].Count)) + } } msg += " (" + strings.Join(users, ", ") + ")\n" } @@ -235,9 +245,24 @@ func purgeRecentEmojis(s *discordgo.Session, i *discordgo.InteractionCreate) { return } - emojis, err := b.Db.GetRecentEmojisForUser(i.GuildID, user.ID, 1) + hours := int64(24) + if opt, ok := optionMap["hours"]; ok { + hours = opt.IntValue() + } else { + slog.Error("Invalid hours option provided") + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "No hours specified", + AllowedMentions: &discordgo.MessageAllowedMentions{}, + }, + }) + return + } + + emojis, err := b.Db.GetRecentEmojisForUser(i.GuildID, user.ID, hours) if err != nil { - slog.Error("Error getting top users", "err", err) + slog.Error("Error getting recent emojis for user", "err", err) return } diff --git a/src/internal/bot/main.go b/src/internal/bot/main.go index bc1e0ce..cf49644 100644 --- a/src/internal/bot/main.go +++ b/src/internal/bot/main.go @@ -88,7 +88,7 @@ func (bot *Bot) HandleAddReaction(discord *discordgo.Session, reaction *discordg return } - err := bot.Db.LogEmojiUsage(reaction.GuildID, reaction.ChannelID, reaction.MessageID, reaction.UserID, reaction.Emoji.Name) + err := bot.Db.LogEmojiUsage(reaction.GuildID, reaction.ChannelID, reaction.MessageID, reaction.UserID, reaction.Emoji.ID, reaction.Emoji.Name) if err != nil { slog.Error("Failed to log emoji usage", "err", err) } @@ -101,7 +101,7 @@ func (bot *Bot) HandleRemoveReaction(discord *discordgo.Session, reaction *disco return } - err := bot.Db.DeleteEmojiUsage(reaction.GuildID, reaction.ChannelID, reaction.MessageID, reaction.UserID, reaction.Emoji.Name) + err := bot.Db.DeleteEmojiUsage(reaction.GuildID, reaction.ChannelID, reaction.MessageID, reaction.UserID, reaction.Emoji.ID) if err != nil { slog.Error("Failed to delete single emoji usage", "err", err) } diff --git a/src/internal/db/database.go b/src/internal/db/database.go index 691ac40..26a78c9 100644 --- a/src/internal/db/database.go +++ b/src/internal/db/database.go @@ -44,6 +44,7 @@ func (db *Database) runMigrations() (*Database, error) { "`message_id` TEXT, " + "`user_id` TEXT, " + "`emoji_id` TEXT, " + + "`emoji_name` TEXT, " + "`timestamp` DATETIME" + ")") if err != nil { @@ -60,9 +61,21 @@ func (db *Database) runMigrations() (*Database, error) { return db, err } - _, err = db.db.Exec("CREATE INDEX IF NOT EXISTS `idx_emoji_usage_emoji_id,guild_id` ON `emoji_usage` (`guild_id`, `emoji_id`)") + // emojitest := map[string]string{ + // "pepe_analyze": "579431592624390147", + // "kekw": "1317987954102112347", + // "pepe_kek": "1300985103182336010", + // "KEKW": "649918246119669770", + // } - return db, err + // for name, id := range emojitest { + // _, err = db.db.Exec("UPDATE `emoji_usage` SET `emoji_id` = '" + id + "' WHERE `emoji_name` = '" + name + "'") + // if err != nil { + // return db, err + // } + // } + + return db, nil } // CloseDbConn - Closes DB connection diff --git a/src/internal/db/emoji_usage.go b/src/internal/db/emoji_usage.go index 595c628..325121f 100644 --- a/src/internal/db/emoji_usage.go +++ b/src/internal/db/emoji_usage.go @@ -1,8 +1,11 @@ package db +import "fmt" + type EmojiMap struct { - EmojiID string - Count int64 + EmojiID string + EmojiName string + Count int64 } type EmojiUsage struct { @@ -15,10 +18,10 @@ type EmojiUsage struct { } // LogEmojiUsage - Log usage -func (db *Database) LogEmojiUsage(guildID, channelID, messageID, userID, emojiID string) error { +func (db *Database) LogEmojiUsage(guildID, channelID, messageID, userID, emojiID, emojiName string) error { _, err := db.db.Exec( - "INSERT INTO `emoji_usage` (`guild_id`, `channel_id`, `message_id`, `user_id`, `emoji_id`, `timestamp`) VALUES (?,?,?,?,?, datetime())", - guildID, channelID, messageID, userID, emojiID, + "INSERT INTO `emoji_usage` (`guild_id`, `channel_id`, `message_id`, `user_id`, `emoji_id`, `emoji_name`, `timestamp`) VALUES (?,?,?,?,?,?, datetime())", + guildID, channelID, messageID, userID, emojiID, emojiName, ) return err @@ -100,7 +103,7 @@ func (db *Database) GetTopUsersForGuildEmoji(guildID string, emojiID string, num func (db *Database) GetTopEmojisForGuild(guildID string, num int64) (map[int]EmojiMap, error) { data := make(map[int]EmojiMap) row, err := db.db.Query( - "SELECT emoji_id, count(*) FROM `emoji_usage` WHERE `guild_id` = ? GROUP BY emoji_id ORDER BY count(*) DESC LIMIT ?", + "SELECT emoji_name, emoji_id, count(*) FROM `emoji_usage` WHERE `guild_id` = ? GROUP BY emoji_id ORDER BY count(*) DESC LIMIT ?", guildID, num, ) @@ -112,10 +115,11 @@ func (db *Database) GetTopEmojisForGuild(guildID string, num int64) (map[int]Emo defer row.Close() i := 0 for row.Next() { - var emoji string + var emojiName string + var emojiID string var count int64 - row.Scan(&emoji, &count) - data[i] = EmojiMap{EmojiID: emoji, Count: count} + row.Scan(&emojiName, &emojiID, &count) + data[i] = EmojiMap{EmojiID: emojiID, EmojiName: emojiName, Count: count} i++ } @@ -126,7 +130,7 @@ func (db *Database) GetTopEmojisForGuild(guildID string, num int64) (map[int]Emo func (db *Database) GetTopEmojisForGuildUser(guildID string, userID string, num int) (map[int]EmojiMap, error) { data := make(map[int]EmojiMap) row, err := db.db.Query( - "SELECT emoji_id, count(*) FROM `emoji_usage` WHERE `guild_id` = ? AND `user_id` = ? GROUP BY emoji_id ORDER BY count(*) DESC LIMIT ?", + "SELECT emoji_name, emoji_id, count(*) FROM `emoji_usage` WHERE `guild_id` = ? AND `user_id` = ? GROUP BY emoji_name ORDER BY count(*) DESC LIMIT ?", guildID, userID, num, @@ -139,10 +143,11 @@ func (db *Database) GetTopEmojisForGuildUser(guildID string, userID string, num defer row.Close() i := 0 for row.Next() { - var emoji string + var emojiName string + var emojiID string var count int64 - row.Scan(&emoji, &count) - data[i] = EmojiMap{EmojiID: emoji, Count: count} + row.Scan(&emojiName, &emojiID, &count) + data[i] = EmojiMap{EmojiID: emojiID, EmojiName: emojiName, Count: count} i++ } @@ -153,12 +158,11 @@ func (db *Database) GetTopEmojisForGuildUser(guildID string, userID string, num func (db *Database) GetRecentEmojisForUser(guildID string, userID string, hours int64) ([]EmojiUsage, error) { var data []EmojiUsage row, err := db.db.Query( - "SELECT emoji_id, channel_id, message_id, user_id, emoji_id, timestamp "+ - "FROM `emoji_usage` WHERE `guild_id` = ? AND `user_id` = ? AND timestamp >= NOW() - INTERVAL ? HOUR "+ + "SELECT guild_id, channel_id, message_id, user_id, emoji_id, timestamp "+ + "FROM `emoji_usage` WHERE `guild_id` = ? AND `user_id` = ? AND timestamp >= datetime('now', '-"+fmt.Sprintf("%d", hours)+" hours') "+ "ORDER BY timestamp DESC", guildID, userID, - hours, ) if err != nil {