From 21f59e3af92f24030a63828e717c9fd86f9724b0 Mon Sep 17 00:00:00 2001 From: Adam Basha <110662505+Bashamega@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:39:00 +0300 Subject: [PATCH] fix: remove URLs from content before checking for banned words (#816) * fix: remove URLs from content before checking for banned words * fix: updated url pattern * fix: urlregex fix --- src/alexjs/checkBannedWords.ts | 5 ++++- src/config/UrlRegex.ts | 2 ++ src/links/checkLinks.ts | 3 +-- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 src/config/UrlRegex.ts diff --git a/src/alexjs/checkBannedWords.ts b/src/alexjs/checkBannedWords.ts index 6c738a65..a88bfaae 100644 --- a/src/alexjs/checkBannedWords.ts +++ b/src/alexjs/checkBannedWords.ts @@ -4,6 +4,7 @@ import type { ExtendedClient } from '../interfaces/ExtendedClient.js'; import { errorHandler } from '../utils/errorHandler.js'; import { BannedWordsOptions } from '../config/BannedWordsOptions.js'; import { getBannedWordConfig } from '../utils/getBannedWordConfig.js'; +import { urlPattern } from '../config/UrlRegex.js'; export const checkBannedWords = async ( bot: ExtendedClient, @@ -12,11 +13,13 @@ export const checkBannedWords = async ( ): Promise => { const embeds: EmbedBuilder[] = []; try { + const text: string = content.replace(urlPattern, ''); const config = await getBannedWordConfig(bot, serverId); const checkWords = config?.bannedWordConfig ? config.bannedWordConfig : BannedWordsOptions; - content.split(' ').forEach((word) => { + + text.split(' ').forEach((word) => { if (checkWords.includes(word.toLowerCase())) { const embed = new EmbedBuilder(); embed.setTitle(`You used the word "${word}"`); diff --git a/src/config/UrlRegex.ts b/src/config/UrlRegex.ts new file mode 100644 index 00000000..97c04615 --- /dev/null +++ b/src/config/UrlRegex.ts @@ -0,0 +1,2 @@ +export const urlPattern = + /[Hh][Tt][Tt][Pp][Ss]?:\/\/([\w-]+(\.[\w-]+)+)(\/[\w-./?%&=]*)?/g; diff --git a/src/links/checkLinks.ts b/src/links/checkLinks.ts index ab3ab8ba..b9845075 100644 --- a/src/links/checkLinks.ts +++ b/src/links/checkLinks.ts @@ -2,10 +2,9 @@ import { EmbedBuilder, Message, type PartialMessage } from 'discord.js'; import type { ExtendedClient } from '../interfaces/ExtendedClient.js'; import { errorHandler } from '../utils/errorHandler.js'; +import { urlPattern } from '../config/UrlRegex.js'; const allowedLinks = ['github.com', 'eddiejaoude.io', 'gitlab.com']; -const urlPattern = - /[Hh][Tt][Tt][Pp][Ss]?:\/\/([\w-]+(\.[\w-]+)+)(\/[\w-./?%&=]*)?/g; export const checkLinks = async ( bot: ExtendedClient,