【问题标题】:How to get value of a module.exports description in another file [Discord.js]如何在另一个文件中获取 module.exports 描述的值 [Discord.js]
【发布时间】:2021-02-19 21:51:12
【问题描述】:

我正在尝试制作一个不和谐的机器人,它有一个帮助命令,它发送一个嵌入的字段,每个字段对应一个命令。我想自动将这些字段的值设置为正确的命令文件描述。这是命令文件的示例:

module.exports = {
    name: 'kick',
    description: "Kicks the mentioned user. Administrator permissions required",
    execute(message, args) {
      // command code
} 

我正在尝试将字段值设置为模块导出描述。如果这是可能的,我将如何实现这一目标?

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    您可以使用Client#commands#get 获取命令的对象,然后只需使用.description 获取该命令的描述。

    在本例中,我将使用Client 作为客户端对象,但如果您的对象是 Bot 或其他任何对象,请相应地调整代码。

    示例模块:

    module.exports = {
       name: 'testCommand',
       description: 'Hello World!'
    }
    

    如何获取描述:

    const cmd = Client.commands.get('testCommand');
    
    console.log(cmd.description);
    // 'Hello World!'
    

    如果您在想要获取描述的命令中,只需使用this 来引用当前命令

    module.exports = {
       name: 'testCommand',
       description: 'Hello World!',
    
       // this.description = 'Hello World!'
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 2016-05-16
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-16
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多