【问题标题】:What does this if-statement exactly do?这个 if 语句究竟做了什么?
【发布时间】:2021-08-22 16:10:17
【问题描述】:

我正在使用discord.js V12 开发Discord Bot,但我的代码中有一些我不明白的行。

我的命令处理程序:

const fs = require('fs');

module.exports = (client, Discord) => {
    const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
    
    for(const file of command_files) {
        const command = require(`../commands/${file}`);
        
     //This is the if statement I dont understand   
      if(command.name)
        {
        client.commands.set(command.name, command); // and also this line thx.
        } else
        {
            continue;
        }
   }
};

谁能给我解释一下?谢谢。

【问题讨论】:

  • @Toasty 感谢它的帮助,感谢您让我的问题变得更好

标签: node.js discord.js


【解决方案1】:

第一个if statement

if(command.name) 

检查commands 的属性name 是否为空

  • 如果为空则**不执行括号内的代码,直接跳转到else block
  • 如果不是,它将执行括号内的代码

第二行

client.commands.set(command.name, command);

调用.set() 函数将带有这些参数的新命令添加到commands 集合中:

  • command.name 显然是要添加的command 的名称
  • command 对象本身具有所有属性和可执行功能

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 2017-06-11
    • 1970-01-01
    • 2012-07-23
    • 2016-09-10
    • 2023-03-15
    • 2012-10-17
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多