Home > database >  Filter for message collector that would find a specific text on a message.content, discord.js
Filter for message collector that would find a specific text on a message.content, discord.js

Time:12-02

I have this filter and array for a message collector.

const answers = ["Rock", "Paper", "Scissors"];   
const filter = msg => answers.includes(msg.content());

That filter only detects a content that exactly matches what's in the array. So if a message say, "Scissors!!", it isn't detected because of the extra text (!!). The design is it should still be detected.

Is it correct that I should use a for loop with the filter? How do I exactly do it? Thanks in advance.

CodePudding user response:

If you want to use a for loop with the filter, you can try something like this:

const answers = ["Rock", "Paper", "Scissors"];
const filter = msg => {
  for (let answer of answers) {
    if (msg.content.includes(answer)) {
      return true;
    }
  }
  return false;
}

This will check if any of the answers are included in the message content, instead of checking for an exact match. You can also use the Array.some() method to simplify the code:

const answers = ["Rock", "Paper", "Scissors"];
const filter = msg => answers.some(answer => msg.content.includes(answer));

CodePudding user response:

It's quite simple. You can use the .some() function which will return true if the condition passed in the callback matches for any of the values and will return false if otherwise. An example:

const string = "test!!";
const answers = ["Rock", "Paper", "Scissors"];

console.log(answers.some((val) => string.includes(val.toLowerCase())));

  • Related