#Censor failing to detect words containing regex symbols

12 messages · Page 1 of 1 (latest)

quiet swan
#

I have a censor event that pulls the list of banned words, which may be preceded and/or followed by an * to mimic the Discord censor behavior. However, while I am escaping regex symbols before testing the word, my censor is failing to pick up words that contain regex symbols, and I can't for the life of me figure out why.

        // Fetch all banned words from the database for the current server
        const bannedWords = await Database.BannedWord.findAll({
            include: [{
                model: Database.Server,
                where: { ServerId: message.guild.id }
            }]
        });

        // Check if the message includes any banned word
        for (const bannedWord of bannedWords) {
            let word = bannedWord.Word;
            let regex;
shadow pastureBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
quiet swan
#
            // Check if the word starts with 'regex:'
            if (word.startsWith('regex:')) {
                // Remove 'regex:' from the beginning
                word = word.slice(6);
                // Use the word as a regular expression
                try {
                    regex = new RegExp(word, 'i');
                } catch (error) {
                    console.error(`Invalid regular expression: ${word}`);
                    continue; // Skip this iteration and move to the next banned word
                }
            } else if (word.startsWith('*') && word.endsWith('*')) {
                // Remove * from the beginning and end
                word = word.slice(1, -1);
                // Escape special characters
                word = escapeRegExp(word);
                // Match the word anywhere in the string
                regex = new RegExp('\\b' + word + '\\b', 'i');
            } else if (word.startsWith('*')) {
                // Remove * from the beginning
                word = word.slice(1);
                // Escape special characters
                word = escapeRegExp(word);
                // Match the word at the end of a string
                regex = new RegExp('\\b' + word + '$', 'i');
            } else if (word.endsWith('*')) {
                // Remove * from the end
                word = word.slice(0, -1);
                // Escape special characters
                word = escapeRegExp(word);
                // Match the word at the beginning of a string
                regex = new RegExp('^' + word + '\\b', 'i');
            } else {
                // Escape special characters
                word = escapeRegExp(word);
                // Match the word as a whole word
                regex = new RegExp('\\b' + word + '\\b', 'i');
            }
#
function escapeRegExp(string) {
    return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
drowsy comet
#

@quiet swan

#

Try this

#

dah one sec

#

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, '\\$&'); 
}
#

This

quiet swan
#

That didn't fix it unfortunately. I is also failing to find partial words based on the *. The * were working correctly until I added the escapeRegExp, but I need it to make the censor find regex symbols as part of words, like shi+ for example.

chilly yarrow