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

Commit

Permalink
feat: log removed messages to admin channel (#793)
Browse files Browse the repository at this point in the history
* feat: log removed links to admin channel

* fix: remove unused import
  • Loading branch information
eddiejaoude authored May 8, 2024
1 parent e6bb32d commit 614fc53
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/database/connectDb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from 'fs';
import { connect } from 'mongoose';
import { ExtendedClient } from '../interfaces/ExtendedClient';
import { errorHandler } from '../utils/errorHandler';
Expand Down
12 changes: 10 additions & 2 deletions src/events/onMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBuilder, Message } from 'discord.js';
import { EmbedBuilder, Message, TextChannel } from 'discord.js';
import { checkContent } from '../alexjs/checkContent';
import { checkBannedWords } from '../alexjs/checkBannedWords';
import { stripSpecialCharacters } from '../alexjs/stripSpecialCharacters';
Expand All @@ -15,7 +15,15 @@ export const onMessage = async (bot: ExtendedClient, message: Message) => {
return;
}

await checkLinks(bot, message);
const linkMessage = await checkLinks(bot, message);
if (linkMessage) {
const adminChannel = bot.channels.cache.get(
process.env.ADMIN_CHANNEL!,

Check warning on line 21 in src/events/onMessage.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
) as TextChannel;
await adminChannel.send({
embeds: [linkMessage],
});
}

const cleaned = await sentenceTypoFixer(
stripSpecialCharacters(message.content),
Expand Down
18 changes: 12 additions & 6 deletions src/links/checkLinks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, PartialMessage } from 'discord.js';
import { EmbedBuilder, Message, PartialMessage } from 'discord.js';

import { ExtendedClient } from '../interfaces/ExtendedClient';
import { errorHandler } from '../utils/errorHandler';
Expand All @@ -9,26 +9,32 @@ const urlPattern = /(http|https):\/\/([\w-]+(\.[\w-]+)+)(\/[\w-./?%&=]*)?/;
export const checkLinks = async (
bot: ExtendedClient,
message: Message | PartialMessage,
): Promise<void> => {
): Promise<EmbedBuilder | null> => {
const content = message.content;

try {
const urlMatch = content?.match(urlPattern);
if (!urlMatch) {
return;
return null;
}

if (urlMatch) {
if (allowedLinks.every((link) => urlMatch[0].includes(link))) {
return;
return null;
}
}

await message.delete();

return;
const embed = new EmbedBuilder();
embed.setTitle(
`The user "${message.author?.username}" message contains a link`,
);
embed.setDescription(message.content);

return embed;
} catch (error) {
await errorHandler(bot, error, 'link checking');
return;
return null;
}
};

0 comments on commit 614fc53

Please sign in to comment.