Skip to content

Commit

Permalink
refactor: improve logs and avoid bad requests
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Feb 5, 2023
1 parent ff24df5 commit 20f9048
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -1219,6 +1216,7 @@ export class DiscordBot {
});

if (!storeEvent?.Result) {
log.verbose(`Received add reaction event for untracked message. Dropping! Reaction Event: ${reaction}`);
return;
}

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -1258,6 +1256,7 @@ export class DiscordBot {
});

if (!storeEvent?.Result) {
log.verbose(`Received remove reaction event for untracked message. Dropping! Reaction Event: ${reaction}`);
return;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/db/dbdataevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 20f9048

Please sign in to comment.