MessageEmbed字段值不能为空

时间:2020-08-21 11:34:27

标签: javascript node.js discord.js

每次尝试执行以下操作:!help命令时,如果该命令没有任何别名,则会给我控制台带来错误,我尝试了以下方法,如果该特定命令没有别名,它应该返回'None',这是代码:

            let command = helpArgs[0]

            if(helpArgs[0]){
            if(bot.commands.has(command)) {
                command = bot.commands.get(command)
                const embed = new Discord.MessageEmbed()
                .setTitle(`${command.config.name}`)
                .addField('Name', `${command.config.name}`)
                .addField('Description', `${command.config.description}`)
                .addField('Usage', `${command.config.usage}`)
                .addField('Aliases', `${command.config.aliases !== undefined ? command.config.aliases : 'None'}`)
message.channel.send(embed)

这是错误:

7:14 GMT+0300 (Eastern European Summer Time)
(node:11744) UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty.
    at Function.normalizeField (C:\Users\\OneDrive\Documents\GitHub\Fergus\node_modules\discord.js\src\structures\MessageEmbed.js:425:23)
    at C:\Users\\OneDrive\Documents\GitHub\Fergus\node_modules\discord.js\src\structures\MessageEmbed.js:445:14
    at Array.map (<anonymous>)
    at Function.normalizeFields (C:\Users\\OneDrive\Documents\GitHub\Fergus\node_modules\discord.js\src\structures\MessageEmbed.js:444:8)
    at MessageEmbed.addFields (C:\Users\\OneDrive\Documents\GitHub\Fergus\node_modules\discord.js\src\structures\MessageEmbed.js:259:42) 
    at MessageEmbed.addField (C:\Users\\OneDrive\Documents\GitHub\Fergus\node_modules\discord.js\src\structures\MessageEmbed.js:250:17)  
    at Object.module.exports.run (C:\Users\\OneDrive\Documents\GitHub\Fergus\commands\Information\help.js:115:18)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11744) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:11744) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

错误发生在.addField('Aliases',${command.config.aliases !== undefined ? command.config.aliases : 'None'})处,我检查了每个字段。

2 个答案:

答案 0 :(得分:0)

command.config.aliases确实是空的,即使它不是未定义的也是如此。您应该修改检查。我不知道aliases字段包含什么,所以我无法说出支票应该是什么。

答案 1 :(得分:0)

我认为错误可能是,您在此字段中使用的方法会导致错误,因此,该字段为空。要在字符串中列出别名,您可以尝试以下方法。

let aliases = command.config.aliases.toString();
if (aliases == "") {
    aliases = "None";
}


.addField('Aliases', `${aliases}`)

使用此方法,可以从命令配置的别名数组创建一个String。然后查看是否没有别名。如果是,则字符串为“ None”。之后,您需要使用变量aliases替换嵌入中的方法。