简体   繁体   English

有没有办法让我的前缀不区分大小写(Discord.js)

[英]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!我正在制作一个 discord 机器人,我的前缀是 xok,我目前使用的代码使它成为所以 xok 总是必须写为“xok”,我认为如果我能够改变它,这将是一个巨大的生活质量改善,但我真的看不出我如何使用当前代码,感谢任何帮助!

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

The code in my prefix "config.json" file vv我的前缀“config.json”文件vv中的代码

{ "token": "*my Token", "prefix": "xok" } {“令牌”:“*我的令牌”,“前缀”:“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.这样,无论输入前缀的大小写如何,它都将始终输入 function 的 rest。

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.简化添加命令的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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