From 99e5cde91144f054b09a00090fd94c9ea161e0a8 Mon Sep 17 00:00:00 2001 From: Navy Date: Mon, 10 May 2021 14:59:08 +0300 Subject: [PATCH] add functionality to remove discord formatting --- .../components/observers/Automoderation.js | 2 +- util/FilterUtility.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/structure/client/components/observers/Automoderation.js b/structure/client/components/observers/Automoderation.js index a7148cb..8a21ebf 100644 --- a/structure/client/components/observers/Automoderation.js +++ b/structure/client/components/observers/Automoderation.js @@ -99,7 +99,7 @@ module.exports = class AutoModeration extends Observer { const msg = edited || message; let log = `Message filter debug:`; log += `\nPre norm: ${msg.cleanContent}`; - const content = FilterUtil.normalise(msg.cleanContent); + const content = FilterUtil.normalise(FilterUtil.removeDiscordFormatting(msg.cleanContent)); log += `\nNormalised: ${content}`; // match: what was matched | diff --git a/util/FilterUtility.js b/util/FilterUtility.js index 315bc5e..3242838 100644 --- a/util/FilterUtility.js +++ b/util/FilterUtility.js @@ -176,6 +176,23 @@ module.exports = class FilterUtility { }; } + static get formattingPatterns() { + return [ + ['\\*{1,3}([^*]*)\\*{1,3}', '$1'], + ['_{1,3}([^_]*)_{1,3}', '$1'], + ['`{1,3}([^`]*)`{1,3}', '$1'], + ['~~([^~])~~', '$1'] + ]; + } + + static removeDiscordFormatting(content) { + if (!content) throw new Error('Missing content'); + this.formattingPatterns.forEach(([pattern, replacer]) => { + content = content.replace(new RegExp(pattern, 'gu'), replacer); + }); + return content; + } + static normalise(content) { if (typeof content !== 'string') throw new Error('Invalid input type, must be of type string');