如何将前缀值设置为大写和小写? (Discord.js)

时间:2020-11-08 04:31:10

标签: node.js discord.js

这里是我的问题

const prefix = T!
const realPrefix = prefix.toLowerCase() || prefix;

命令仍然只用“ t!”执行。如您在此screenshot中所见 但我希望该命令在大写字母(T!)和小写字母(t!)中均适用 screen shot

2 个答案:

答案 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);

唯一的假值是false0nullNaNundefined和空字符串。这意味着,在您的代码段中,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');