我的 discord.js 帮助命令不能正常工作

My discord.js help command doesn't exactly work

首先,我的帮助命令确实有效,但不是以我希望的方式工作。

我的第一个问题是命令是在单独的消息中发送的,当您有很多命令时这有点烦人。

我的第二个问题是,当在嵌入中发送消息时,它显示如下:

我尝试了多种方法来摆脱 'Undefined'。

我的代码:

const fs = require("fs");
const Discord = require("discord.js");

module.exports.run = async(bot, message, args, con) => {
   fs.readdir("./commands/", (err, files) => {
     if(err) console.error(err);

    let jsfiles = files.filter(f => f.split(".").pop() === "js");
    if(jsfiles.length <= 0) {
        console.log("No commands to load!");
        return;
    }

    var namelist = "";
    var desclist = "";
    var usage = "";

    let result = jsfiles((f, i) => {
        let props = require(`./${f}`);
        namelist = props.help.name;
        desclist = props.help.description;
        usage = props.help.usage;

        // send help text
        let helpembed = new Discord.RichEmbed()
        .setTitle("Commands")
        .setFooter("Please report any bugs to Vati#1662")
        .setColor("RANDOM")
        .addField(`**${namelist}** \n${desclist} \n${usage}`)  
        message.author.sendEmbed(helpembed);
    });

   })
  }
    module.exports.help = {
    name: "help",
    description: "shows all commands",
    usage: "help"
    }

当您使用 RichEmbed.addField() 时,它需要至少两个参数:字段的标题及其值。

.addField(`**${namelist}** \n${desclist} \n${usage}`) // this has only the title argument

尝试将三个 "sections" 放在三个不同的字段中。

.addField("Name:", namelist, true) // the 'true' means that they're inline fileds
.addField("Usage:", usage, true) // they will try to fit on the same line
.addField("Description:", desclist) // if there's no third argument, the default is 'false'

这些命令在不同的消息中发送,因为您是 运行 每个命令的完整代码,而不仅仅是为每个命令添加字段。如果您不想花时间在所有这些东西上,您可以使用 discord.js-commando 库:它是一个处理命令并处理错误、不完整命令和许多其他东西的框架。如果你想查看它,你可以找到文档 here.