AstralDev
AstralDev
DIAdiscord.js - Imagine an app
Created by AstralDev on 5/28/2024 in #djs-questions
Typescript
3 replies
DIAdiscord.js - Imagine an app
Created by SowerofSystems on 5/26/2024 in #djs-questions
Censor failing to detect words containing regex symbols
This
12 replies
DIAdiscord.js - Imagine an app
Created by SowerofSystems on 5/26/2024 in #djs-questions
Censor failing to detect words containing regex symbols
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;
let regex;

if (word.startsWith('regex:')) {

word = word.slice(6);


try {
regex = new RegExp(word, 'i');
} catch (error) {
console.error(`Invalid regular expression: ${word}`);
continue;
}
} else { // Handle non-regex cases
if (word.startsWith('*') && word.endsWith('*')) {
word = word.slice(1, -1);
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '\\b', 'i');
} else if (word.startsWith('*')) {
word = word.slice(1);
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '$', 'i');
} else if (word.endsWith('*')) {
word = word.slice(0, -1);
word = escapeRegExp(word);
regex = new RegExp('^' + word + '\\b', 'i');
} else {
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '\\b', 'i');
}
}

if (regex && regex.test(message.content)) {

message.delete();
break;
}
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;
let regex;

if (word.startsWith('regex:')) {

word = word.slice(6);


try {
regex = new RegExp(word, 'i');
} catch (error) {
console.error(`Invalid regular expression: ${word}`);
continue;
}
} else { // Handle non-regex cases
if (word.startsWith('*') && word.endsWith('*')) {
word = word.slice(1, -1);
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '\\b', 'i');
} else if (word.startsWith('*')) {
word = word.slice(1);
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '$', 'i');
} else if (word.endsWith('*')) {
word = word.slice(0, -1);
word = escapeRegExp(word);
regex = new RegExp('^' + word + '\\b', 'i');
} else {
word = escapeRegExp(word);
regex = new RegExp('\\b' + word + '\\b', 'i');
}
}

if (regex && regex.test(message.content)) {

message.delete();
break;
}
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
12 replies
DIAdiscord.js - Imagine an app
Created by SowerofSystems on 5/26/2024 in #djs-questions
Censor failing to detect words containing regex symbols
dah one sec
12 replies
DIAdiscord.js - Imagine an app
Created by SowerofSystems on 5/26/2024 in #djs-questions
Censor failing to detect words containing regex symbols
Try this
12 replies
DIAdiscord.js - Imagine an app
Created by SowerofSystems on 5/26/2024 in #djs-questions
Censor failing to detect words containing regex symbols
@SowerofSystems
12 replies
DIAdiscord.js - Imagine an app
Created by SowerofSystems on 5/26/2024 in #djs-questions
Censor failing to detect words containing regex symbols
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;

let escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\\\*/g, '.*');


let regex = new RegExp(`\\b${escapedWord}\\b`, 'gi');

if (regex.test(message.content)) {

message.delete();
break;
}
}
for (const bannedWord of bannedWords) {
let word = bannedWord.Word;

let escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\\\*/g, '.*');


let regex = new RegExp(`\\b${escapedWord}\\b`, 'gi');

if (regex.test(message.content)) {

message.delete();
break;
}
}
12 replies