diff --git a/src/commands/emoteStats.ts b/src/commands/emoteStats.ts index 05759fb8..66752dec 100644 --- a/src/commands/emoteStats.ts +++ b/src/commands/emoteStats.ts @@ -6,19 +6,19 @@ import * as emoteLogging from "../service/emoteLogging.js"; export default class EmoteStatsCommand implements ApplicationCommand { name = "emote-stats"; - description = "Schickt dir deine persönlichen Emote-Statistiken."; + description = "Schickt Emote-Statistiken."; applicationCommand = new SlashCommandBuilder() .setName(this.name) .setDescription(this.description); async handleInteraction(command: CommandInteraction, context: BotContext): Promise { - const stats = await emoteLogging.getUserStats(command.user, 10); + const stats = await emoteLogging.getGlobalStats(10); const totalEmotes = Math.sumPrecise(stats.map(s => s.count)) | 0; await command.reply({ embeds: [ { - title: "Deine Emote-Statistiken", + title: "Emote-Statistiken", author: { name: command.user.username, icon_url: command.user.displayAvatarURL(), @@ -28,7 +28,7 @@ export default class EmoteStatsCommand implements ApplicationCommand { value: `${s.count} mal`, })), footer: { - text: `Du hast insgesamt ${totalEmotes} Emotes verwendet.`, + text: `Es wurden insgesamt ${totalEmotes} Emotes verwendet.`, }, }, ], diff --git a/src/handler/logEmotesReactionHandler.ts b/src/handler/logEmotesReactionHandler.ts index b2033973..2905965c 100644 --- a/src/handler/logEmotesReactionHandler.ts +++ b/src/handler/logEmotesReactionHandler.ts @@ -5,17 +5,15 @@ import * as emoteLogging from "../service/emoteLogging.js"; export default { displayName: "Log Emotes Reaction Handler", async execute(reactionEvent, invoker, context, reactionWasRemoved) { - return; - - // biome-ignore lint/correctness/noUnreachable: Deactivated due to privacy concerns if (invoker.bot) { return; } + // Not supported if (reactionWasRemoved) { - await emoteLogging.processReactionRemove(reactionEvent, invoker, context); - } else { - await emoteLogging.processReactionAdd(reactionEvent, invoker, context); + return; } + + await emoteLogging.processReactionAdd(reactionEvent, context); }, } satisfies ReactionHandler; diff --git a/src/service/emoteLogging.ts b/src/service/emoteLogging.ts index c016a823..053de17f 100644 --- a/src/service/emoteLogging.ts +++ b/src/service/emoteLogging.ts @@ -8,11 +8,7 @@ import * as emoteService from "./emoteService.js"; import log from "@log"; -export async function processReactionAdd( - reactionEvent: MessageReaction, - invoker: User, - _context: BotContext, -) { +export async function processReactionAdd(reactionEvent: MessageReaction, _context: BotContext) { const message = reactionEvent.message; if (!message.inGuild()) { return; @@ -37,19 +33,9 @@ export async function processReactionAdd( parsedEmote.animated, emoteService.getEmoteUrl(parsedEmote), message, - invoker, ); } -export async function processReactionRemove( - reactionEvent: MessageReaction, - invoker: User, - _context: BotContext, -) { - // TODO: Implement - log.info({ emoji: reactionEvent.emoji }, "Reaction removed"); -} - export async function processMessage(message: ProcessableMessage, context: BotContext) { const emotes = emoteService.extractEmotesFromMessage(message.content); for (const emote of emotes) { @@ -96,6 +82,6 @@ export async function persistCurrentGuildEmotes(context: BotContext) { } } -export async function getUserStats(user: User, limit: number) { - return dbEmote.getUsage(user, limit | 0); +export async function getGlobalStats(limit: number) { + return dbEmote.getGlobalUsage(limit | 0); } diff --git a/src/storage/db/model.ts b/src/storage/db/model.ts index 6bb14d3f..00724711 100644 --- a/src/storage/db/model.ts +++ b/src/storage/db/model.ts @@ -236,11 +236,6 @@ export interface EmoteUseTable extends AuditedTable { messageGuildId: ColumnType; channelId: ColumnType; - messageId: ColumnType; emoteId: ColumnType; - usedByUserId: ColumnType; - usedByUserName: string; isReaction: boolean; - - deletedAt: ColumnType; // TODO: Date is not supported by the DB driver } diff --git a/src/storage/emote.ts b/src/storage/emote.ts index f926ef46..7c8b74c4 100644 --- a/src/storage/emote.ts +++ b/src/storage/emote.ts @@ -22,10 +22,7 @@ export async function logMessageUse( // not using emote.guild.id because the emote can originate from a different server messageGuildId: message.guildId, channelId: message.channelId, - messageId: message.id, emoteId: existingEmote.id, - usedByUserId: message.author.id, - usedByUserName: message.author.displayName, isReaction: false, }) .execute(); @@ -38,7 +35,6 @@ export async function logReactionUse( isAnimated: boolean, url: string, message: Message, - invoker: User, ctx = db(), ) { await ctx.transaction().execute(async ctx => { @@ -49,10 +45,7 @@ export async function logReactionUse( // not using emote.guild.id because the emote can originate from a different server messageGuildId: message.guildId, channelId: message.channelId, - messageId: message.id, emoteId: existingEmote.id, - usedByUserId: invoker.id, - usedByUserName: invoker.displayName, isReaction: true, }) .execute(); @@ -107,11 +100,10 @@ export async function markAsDeleted(emoteId: Emote["id"], ctx = db()): Promise) { + db.schema.dropTable("emoteUse"); + + await db.schema + .createTable("emoteUse") + .addColumn("id", "integer", c => c.primaryKey().autoIncrement()) + .addColumn("messageGuildId", "text", c => c.notNull()) + .addColumn("channelId", "text", c => c.notNull()) + .addColumn("emoteId", "integer", c => c.notNull()) + .addColumn("isReaction", "boolean", c => c.notNull()) + .addColumn("createdAt", "timestamp", c => c.notNull().defaultTo(sql`current_timestamp`)) + .addColumn("updatedAt", "timestamp", c => c.notNull().defaultTo(sql`current_timestamp`)) + .execute(); + + await db.schema + .createIndex("emoteUse_emoteId_isReaction_deletedAt") + .on("emoteUse") + .columns(["emoteId", "isReaction", "deletedAt"]) + .unique() + .execute(); + + await createUpdatedAtTrigger(db, "emoteUse"); +} + +function createUpdatedAtTrigger(db: Kysely, tableName: string) { + return sql + .raw(` + create trigger ${tableName}_updatedAt + after update on ${tableName} for each row + begin + update ${tableName} + set updatedAt = current_timestamp + where id = old.id; + end; + `) + .execute(db); +} + +export async function down(_db: Kysely) { + throw new Error("Not supported lol"); +}