mardi 6 avril 2021

Regex test not working as expected with my discord bot

I'm making a simple discord bot with discord.js. I am coding a feature where the bot repeats what the user says after a $say command, but I made an array of regexes that contain words or statements that I don't want the bot to repeat. What happens is that the bot should not repeat messages containing these words, and instead display a warning message, but the bot repeats the message typed by the user anyways. Here's the code snippet.

    let allwords = new RegExp(naughtywords.join("|"), "i") //a combined regular expression of all regexes of naughty words
  if(msg.content.startsWith('$say')) {
    if (allwords.test(msg.content) === false) {
        let thingToSay = msg.content.substring(5)
        msg.reply(thingToSay)
    } else {
      msg.reply("You can't say naughty words >:(")
    }
  }

I have been staring at this code for 10 minutes now and can't find what I did wrong. Any idea why this happens?

Aucun commentaire:

Enregistrer un commentaire