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

Posted

技术标签:

【中文标题】“MessageEmbed 字段值不能为空” discord.js【英文标题】:"MessageEmbed field values may not be empty" discord.js 【发布时间】:2021-09-04 05:30:48 【问题描述】:

我目前正在使用 node.js 制作一个不和谐的机器人,但我在执行 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.```

【问题讨论】:

您的自定义字段中的某个人似乎收到了一个空值。确保您的所有 addField 调用都传递了一个有效值(应该是一个不为空的字符串)。这是 normalizeField github.com/discordjs/discord.js/blob/… 调用的函数 下次提出问题时,如果您显示 messagemember 的定义位置,对尝试解决您的问题的人会更有用。 【参考方案1】:

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

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 变量,因此我在答案中添加了一个。

【讨论】:

以上是关于“MessageEmbed 字段值不能为空” discord.js的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你写DI_1_DI框架有什么?

di.xml

data_2

php Customizzare la pagina di login di Wordpress

html gli operatori di assegnazione di JavaScript.html

DI 原理解析 并实现一个简易版 DI 容器