首页 > 解决方案 > MessageEmbed 字段值不能为空

问题描述

每次我尝试这样做时: !help command ,如果该命令没有任何别名,它会在控制台中给我那个错误,我尝试了以下方法,如果特定命令没有别名,它应该返回'无',代码如下:

            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'}) ,我检查了每个字段。

标签: javascriptnode.jsdiscord.js

解决方案


看起来command.config.aliases确实是空的,即使它不是未定义的。您应该修改检查。我不知道该aliases字段包含什么,所以我不能说检查应该是什么。


推荐阅读