From 614fc53870d6cfa2d00d60a6eb1d7e02b92a91d9 Mon Sep 17 00:00:00 2001 From: Eddie Jaoude Date: Wed, 8 May 2024 07:02:19 +0100 Subject: [PATCH] feat: log removed messages to admin channel (#793) * feat: log removed links to admin channel * fix: remove unused import --- src/database/connectDb.ts | 1 - src/events/onMessage.ts | 12 ++++++++++-- src/links/checkLinks.ts | 18 ++++++++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/database/connectDb.ts b/src/database/connectDb.ts index 65d5b27b..cf1bda22 100644 --- a/src/database/connectDb.ts +++ b/src/database/connectDb.ts @@ -1,4 +1,3 @@ -import fs from 'fs'; import { connect } from 'mongoose'; import { ExtendedClient } from '../interfaces/ExtendedClient'; import { errorHandler } from '../utils/errorHandler'; diff --git a/src/events/onMessage.ts b/src/events/onMessage.ts index 3efb8ec5..c7bb367b 100644 --- a/src/events/onMessage.ts +++ b/src/events/onMessage.ts @@ -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'; @@ -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), diff --git a/src/links/checkLinks.ts b/src/links/checkLinks.ts index 513bcc4d..c3f52341 100644 --- a/src/links/checkLinks.ts +++ b/src/links/checkLinks.ts @@ -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'; @@ -9,26 +9,32 @@ const urlPattern = /(http|https):\/\/([\w-]+(\.[\w-]+)+)(\/[\w-./?%&=]*)?/; export const checkLinks = async ( bot: ExtendedClient, message: Message | PartialMessage, -): Promise => { +): Promise => { 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; } };