Skip to content Skip to sidebar Skip to footer

Is There A Way To Make My Prefix Non Case Sensitive (discord.js)

Im making a discord bot and my prefix is xok, the code im currently using makes it so the xok always has to be written as 'xok', I think it would be a massive quality of life impro

Solution 1:

Simply test the lowercased message content instead of the varying cased one:

if(!message.content.toLowerCase().startsWith(client.config.prefix)) return;

that way regardless of what casing the prefix was entered with, it will always enter the rest of the function.

Solution 2:

Try this:

(message.content.toLowerCase().indexOf(client.config.prefix) !== 0) return;

This will push your message to LowerCase, ignoring capitalization.

For my bots, I use:

var commandPrefix = xok

const command = args.shift().slice(commandPrefix.toLowerCase().length).toLowerCase();

That turns the message into an array, checks for the prefix, and pushes the command to lowercase.

So that way I would be able to do things like this:

if (command === "ping") {
    message.channel.send("pong");
};

That way it's looking for the prefix and if the message doesn't have one, it ignores it. If it does, it looks for the corresponding command. That way, it's not checking for both the prefix and the command at the same time. Streamlining the process of adding commands.

Post a Comment for "Is There A Way To Make My Prefix Non Case Sensitive (discord.js)"