“MessageEmbed 字段值不能为空” discord.js

时间:2021-06-20 04:49:59

标签: javascript node.js npm discord.js

我目前正在使用 node.js 制作一个 discord 机器人,但我在执行 kick 和 ban 命令时发现了一个问题。我试图确保不和谐嵌入,但我一直遇到这个问题。 有人可以帮我吗?

这是一个嵌入代码:

const kickembed = new Discord.MessageEmbed()
.setTitle('Member Kicked')
.setThumbnail(member.user.displayAvatarURL())
.addField('User Kicked', member)
.addField('Kicked by', message.author)
.addField('Reason', reason)
.setFooter('Time Kicked', client.user.displayAvatarURL())
.setTimestamp()

message.channel.send(kickembed);

这是一个错误日志:

(node:2400) UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty.

    at Function.normalizeField (D:\DATA\Discord Bot\Tutorial #2\node_modules\discord.js\src\structures\MessageEmbed.js:432:23)        
    at D:\DATA\Discord Bot\Tutorial #2\node_modules\discord.js\src\structures\MessageEmbed.js:452:14
    at Array.map (<anonymous>)
    at Function.normalizeFields (D:\DATA\Discord Bot\Tutorial #2\node_modules\discord.js\src\structures\MessageEmbed.js:451:8)        
    at MessageEmbed.addFields (D:\DATA\Discord Bot\Tutorial #2\node_modules\discord.js\src\structures\MessageEmbed.js:266:42)
    at MessageEmbed.addField (D:\DATA\Discord Bot\Tutorial #2\node_modules\discord.js\src\structures\MessageEmbed.js:257:17)
    at Object.run (D:\DATA\Discord Bot\Tutorial #2\commands\kick.js:35:6)
    at Client.<anonymous> (D:\DATA\Discord Bot\Tutorial #2\index.js:51:40)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
ated 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:2400) [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.```

1 个答案:

答案 0 :(得分:0)

Discord.js 不允许您使用空字段值,从您的代码看来,错误是由 message.author 引起的,因为它返回的是 user 对象而不是字符串,而是您应该使用 message.author.username (它将返回运行命令的人的用户名)。建议使用三元运算符以防止将来出现此错误。

const member = message.mentions.members.first();
const kickembed = new Discord.MessageEmbed()
.setTitle('Member Kicked')
.setThumbnail(member.user.displayAvatarURL())
.addField('User Kicked', member.user.username ? member.user.username : 'unknown')
.addField('Kicked by', message.author.username ? message.author.username : 'cannot access username')
.addField('Reason', reason ? reason : 'No reason provided')
.setFooter('Time Kicked', client.user.displayAvatarURL())
.setTimestamp()

message.channel.send(kickembed);

由于您没有在问题中定义 user 变量,因此我在答案中添加了一个。