add functionality to remove discord formatting

This commit is contained in:
Erik 2021-05-10 14:59:08 +03:00
parent 4620816cd0
commit 99e5cde911
No known key found for this signature in database
GPG Key ID: 7E862371D3409F16
2 changed files with 18 additions and 1 deletions

View File

@ -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 |

View File

@ -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');