Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
fix: onDelete handles cleanup, so no db work needed in OnUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-mba committed Jul 1, 2024
1 parent 273d913 commit f6a7924
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/events/onUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,6 @@ export const onUpdate = async (
channelId: newMessage.channel.id,
});

// When message has existing warning, but no new warning, it is fixed or
// contains an invalid link & has been deleted
if (savedWarning && (!triggeredWarnings.length || linkMessage)) {
const notificationMessage = await newMessage.channel.messages.fetch(
savedWarning.warningId,
);
if (notificationMessage) {
await notificationMessage.delete();
}
await savedWarning.deleteOne();

await Statistics.findOneAndUpdate(
{
serverId: newMessage.guild.id,
},
{ $inc: { totalTriggersFixed: 1 } },
{ upsert: true },
).exec();
return;
}

// when edit results in new warning, but no existing warning
if (!savedWarning && triggeredWarnings.length) {
const sent = await newMessage.channel.send({
Expand All @@ -143,6 +122,26 @@ export const onUpdate = async (
).exec();
}

// when edit results in no new warning, but has existing warning, so fixed
if (savedWarning && !triggeredWarnings.length) {
const notificationMessage = await newMessage.channel.messages.fetch(
savedWarning.warningId,
);
if (notificationMessage) {
await notificationMessage.delete();
}
await savedWarning.deleteOne();

await Statistics.findOneAndUpdate(
{
serverId: newMessage.guild.id,
},
{ $inc: { totalTriggersFixed: 1 } },
{ upsert: true },
).exec();
return;
}

// when edit results in new warning AND has existing warning
if (savedWarning && triggeredWarnings.length) {
const notificationMessage = await newMessage.channel.messages.fetch(
Expand Down

0 comments on commit f6a7924

Please sign in to comment.