简体   繁体   中英

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 improvement if i were able to change it, but i cant really see how i could using the current code, any help is appreciated!

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

The code in my prefix "config.json" file vv

{ "token": "*my Token", "prefix": "xok" }

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM