我如何区分论点?

How do I differentiate arguments?

所以我想让我的代码检测不同的部分。问题是它只认为有 1 个参数。我尝试了一些 if 语句,并在控制台记录了它,结果显示为 1。当前代码为:

module.exports = {
    name: 'report',
    description: "report a naughty person",
    async execute(message, args, Discord, client){ 
    const reason = args.splice(1)
    console.log (args)

if (args.length = 1){ 

    let embed = new Discord.MessageEmbed()
        .setColor('#1ed700')
        .setTitle('Report \n')
        .setDescription(`Person who reported ${message.author} \n`
        + `Channel reported in: ${message.channel}\n`
        + `Person reported: ${args[0]} \n` //The 1st argument
        + `Reason reported: ${reason.join(' ')}`) // The 2nd argument
         

    let messageEmbed = await message.channel.send(embed);
    message.channel.send(`<@&${process.env.DUMMY_ROLE}>`)
    
} else
    message.channel.send('Code is not written properly')
    

}};

我怎样才能改变它,使它以 2 个不同的论点出现(第一个是提及,第二个是原因)。我怎样才能检查是否有这两个参数?

首先,您使用 赋值运算符 来检查 args.length 是否为 1。 你想要一个比较所以你想要的是以下内容:

if (args.length === 1) {
// Do something
}

尝试将您的参数设置为以下代码:

const args = message.content.split(' ').slice(1); // Only use the last port slice() if you have a prefix like "-" or "+"

我添加了 2 个检查来向您展示如何检查用户是否被提及,另一个检查用户是否给出了原因。 要检查是否有提及,您可以简单地使用 message.mentions.users.first();。您唯一忘记添加原因的是 join(' ') 否则用户可以只写一个词作为原因(我猜这不是您想要的)。

module.exports = {
    name: 'report',
    description: "report a naughty person",
    async execute(message, args, Discord, client){ 
    const reason = args.splice(1).join(' ')
    const user = message.mentions.users.first();

   if (!user) return console.log('No user mentioned');
   if (!reason) return console.log('No reason given') // Checks if there is a reason given
   
   if (user && reason) {
  // Do something
console.log('User mentioned and reason given')
}
}};

你可以试试这个:

module.exports = {
    name: 'report',
    description: "report a naughty person",
    async execute(message, args, Discord, client){ 
    const reportedPerson = message.mentions.users.first();
    args.shift();
    const reason = args.join(' ');
    console.log(args);

    if(reportedPerson && reason){
      // Do something
    }
    else {
      console.log("No reported person or reason given!");
    }
    
};