const prefix = T!
const realPrefix = prefix.toLowerCase() || prefix;
命令仍然只用“ t!”执行。如您在此screenshot中所见 但我希望该命令在大写字母(T!)和小写字母(t!)中均适用 screen shot
答案 0 :(得分:0)
我不知道您的代码的样子,但这对我有用! Here is the Example
client.on("message", message => {
const prefix = "t!";
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(message.content.toLowerCase().startsWith(prefix))
{
if(command === "ping") {
message.channel.send("Pong.")
}
}
});
client.login('BOT TOKEN);
答案 1 :(得分:0)
逻辑或运算符(||
)将返回第一个真实操作数的值。在示例x || y
中,如果x
为真,它将返回x
。如果x
不正确,它将返回y
。
if (true || true) console.log(1);
if (true || false) console.log(2);
if (false || false) console.log(3);
唯一的假值是false
,0
,null
,NaN
,undefined
和空字符串。这意味着,在您的代码段中,prefix.toLowerCase()
始终是真实的(除非prefix
是一个空字符串),这意味着y
操作数将永远不会被考虑。
到目前为止,最简单的解决方案是将邮件转换为小写而不是前缀。
if (!message.content.toLowerCase().startsWith('t!')) return;
const content = 'T!Hello World';
if (content.toLowerCase().startsWith('t!'))
console.log('Hurrah!');
第二种方法是使用正则表达式。
if (!/^[tT]!/.test(message.content)) return;
// ^ - this must be the start of the message
// [tT] - this character can either be 't' or 'T'
// ! - this is a character literal (just !)
const content = 'T!Hello World';
if (/^[tT]!/.test(content)) console.log('Hurrah');