From 20f9048a5b6718dc818e5a85813b811e1945e9a1 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sun, 5 Feb 2023 12:06:01 +0000 Subject: [PATCH] refactor: improve logs and avoid bad requests --- src/bot.ts | 23 +++++++++++++---------- src/db/dbdataevent.ts | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index a2f7f2e0..0d6a793c 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1194,18 +1194,15 @@ export class DiscordBot { public async OnMessageReactionAdd(reaction: Discord.MessageReaction, user: Discord.User | Discord.PartialUser) { const message = reaction.message; - log.info(`Got message reaction add event for ${message.id} with ${reaction.emoji.name}`); + const reactionName = reaction.emoji.name; + log.verbose(`Got message reaction add event for ${message.id} with ${reactionName}`); let rooms: string[]; try { rooms = await this.channelSync.GetRoomIdsFromChannel(message.channel); - - if (rooms === null) { - throw Error(); - } } catch (err) { - log.verbose("No bridged rooms to send message to. Oh well."); + log.verbose(`No bridged rooms to forward reaction to. Reaction Event: ${reaction}`); MetricPeg.get.requestOutcome(message.id, true, "dropped"); return; } @@ -1219,6 +1216,7 @@ export class DiscordBot { }); if (!storeEvent?.Result) { + log.verbose(`Received add reaction event for untracked message. Dropping! Reaction Event: ${reaction}`); return; } @@ -1229,7 +1227,7 @@ export class DiscordBot { const reactionEventId = await intent.underlyingClient.unstableApis.addReactionToEvent( room, matrixIds[0], - reaction.emoji.id ? `:${reaction.emoji.name}:` : reaction.emoji.name + reaction.emoji.id ? `:${reactionName}:` : reactionName ); const event = new DbEvent(); @@ -1247,7 +1245,7 @@ export class DiscordBot { public async OnMessageReactionRemove(reaction: Discord.MessageReaction, user: Discord.User | Discord.PartialUser) { const message = reaction.message; - log.info(`Got message reaction remove event for ${message.id} with ${reaction.emoji.name}`); + log.verbose(`Got message reaction remove event for ${message.id} with ${reaction.emoji.name}`); const intent = this.GetIntentFromDiscordMember(user); await intent.ensureRegistered(); @@ -1258,6 +1256,7 @@ export class DiscordBot { }); if (!storeEvent?.Result) { + log.verbose(`Received remove reaction event for untracked message. Dropping! Reaction Event: ${reaction}`); return; } @@ -1280,6 +1279,7 @@ export class DiscordBot { }); if (!event) { + log.verbose(`Received remove reaction event for tracked message where the add reaction event was not bridged. Dropping! Reaction Event: ${reaction}`); return; } @@ -1299,13 +1299,14 @@ export class DiscordBot { } public async OnMessageReactionRemoveAll(message: Discord.Message | Discord.PartialMessage) { - log.info(`Got message reaction remove all event for ${message.id}`); + log.verbose(`Got message reaction remove all event for ${message.id}`); const storeEvent = await this.store.Get(DbEvent, { discord_id: message.id, }); if (!storeEvent?.Result) { + log.verbose(`Received remove all reaction event for untracked message. Dropping! Event: ${message}`); return; } @@ -1319,7 +1320,9 @@ export class DiscordBot { "m.annotation" ); - await Promise.all(chunk.map(async (event) => { + const filteredChunk = chunk.filter((event) => event.sender === this.bridge.botUserId); + + await Promise.all(filteredChunk.map(async (event) => { try { return await underlyingClient.redactEvent(event.room_id, event.event_id); } catch (ex) { diff --git a/src/db/dbdataevent.ts b/src/db/dbdataevent.ts index 38738a55..5d8fd9e5 100644 --- a/src/db/dbdataevent.ts +++ b/src/db/dbdataevent.ts @@ -19,7 +19,7 @@ import { IDbDataMany } from "./dbdatainterface"; import { ISqlCommandParameters } from "./connector"; export class DbEvent implements IDbDataMany { - /** Matrix ID of event. */ + /** ID associated with the event in the format "MatrixID:RoomID". */ public MatrixId: string; /** Discord ID of the relevant message associated with this event. */ public DiscordId: string;