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

Commit

Permalink
fix: invalid link deletes message, so stop checking
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-mba committed Jul 1, 2024
1 parent 7d106c3 commit 273d913
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/events/onMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const onMessage = async (bot: ExtendedClient, message: Message) => {
await adminChannel.send({
embeds: [linkMessage],
});
return; // Return as message is deleted
}

const cleaned = await sentenceTypoFixer(
Expand Down
52 changes: 31 additions & 21 deletions src/events/onUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ export const onUpdate = async (
return;
}

await checkLinks(bot, newMessage);
const linkMessage = await checkLinks(bot, newMessage);
if (linkMessage) {
const adminChannel = bot.channels.cache.get(process.env.ADMIN_CHANNEL!);
if (adminChannel && adminChannel.isTextBased()) {
await adminChannel.send({
embeds: [linkMessage],
});
}
return; // Return as message is deleted
}

// log to admin channel updates
const oldContent = oldMessage.content;
Expand Down Expand Up @@ -92,6 +101,27 @@ 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 @@ -113,26 +143,6 @@ 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 273d913

Please sign in to comment.