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 links to admin channel
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiejaoude committed May 7, 2024
1 parent e6bb32d commit c7480a1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
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!,
) 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 c7480a1

Please sign in to comment.