如何使用 discord.js 创建动态帮助嵌入,以便按类别组织命令

时间:2020-12-24 00:24:27

标签: discord.js

在制作 Discord 机器人时,将每个命令、描述和类别手动硬编码到帮助嵌入中显然效率低下(并且可能不准确),因此我决定使用以下代码使其自动:

    const commands = loadCommands()
    let embed = new Discord.MessageEmbed()
    embed.setTitle('Commands List')
    embed.setColor(`${embedColor}`)
    embed.setDescription('Prefix: `' + `${prefix}`+ '` \n\n')
    let list = ' '
    for (const command of commands) {
   
      const mainCommand =
        typeof command.commands === 'string'
          ? command.commands
          : command.commands[0]
      const args = command.expectedArgs ? ` ${command.expectedArgs}` : ''
      const { description } = command

      list += ' `'+`${mainCommand}`+'` '
    }
    
    embed.addFields(
        {name: 'general commands', value: list, inline: true
        })
    message.channel.send(embed)
  },
}

它接受我所有的命令并将它们放入嵌入中。

问题是我不知道如何让它按类别排序。我已经为所有命令列出了类别(例如,像“help”和“ping”这样的命令在实用程序中);我希望能够使嵌入的每个类别都有一个字段,值是类别中的所有命令。

在那一点上,我还想知道如何制作它,以便如果您执行像 <prefix>help [command] 这样的命令,机器人会检查 [command] 是否存在,如果存在,机器人会检查返回该命令的硬编码描述(如果 [command] 不存在,机器人将返回通用帮助嵌入)。

预先感谢您提供的所有帮助。

0 个答案:

没有答案