Can't get multiple prefixes to work with discord.js. How can I rewrite this so that it recognizes both prefixes?

97 views Asked by At

the title is pretty self-explanatory. Tried messing around with diff iterations of the below code. This version recognizes firstPrefix, but not secondPrefix. I just want my djs bot to be able to recognize both prefixes and run the Args split accordingly.

const firstPrefix = '!test ';
const secondPrefix = '!testtwo ';

//Prefix
bot.on('message', message => {
    message.content = message.content.toLowerCase();
    if (message.author.bot || !message.content.startsWith(firstPrefix || secondPrefix))  {
        return;
    }

//Args split
    if (message.content.startsWith(firstPrefix)) {
        console.log("A")
        var args = message.content.slice(firstPrefix.length).split(/ +/);
    }
    else if (message.content.startsWith(secondPrefix)) {
        console.log("B")
        var args = message.content.slice(secondPrefix.length).split(/ +/);
    } 

I've tried doing:

if (message.author.bot || !message.content.startsWith(firstPrefix) || !message.content.startsWith(secondPrefix))

But that didn't work at all. Pretty stumped here, any help would be great. Thanks.

2

There are 2 answers

0
theusaf On BEST ANSWER

Your current code (second code block) will not work, as if it starts with one prefix, it does not start with the other prefix, causing it to result in a true if statement, and does not execute your command.

In the first code block, since both firstPrefix and secondPrefix are defined with values, firstPrefix || secondPrefix will evaluate to firstPrefix.

Since you want to include both firstPrefix AND secondPrefix, you should do something like this:

if (
  message.author.bot || /* if it is a bot */
  (!message.content.startsWith(firstPrefix) && /* does not start with first */
  !message.content.startsWith(secondPrefix))) /* and does not start with second */ {
  return;
}
0
Elitezen On

You can store your prefixes in an array, then check if the content starts with either prefix using Array#some()

const prefixes = ['!test ', '!testtwo '];

bot.on('message', message => {
   ...
   if (prefixes.some(prefix => message.content.startsWith(prefix)) {
      ...
   }
});