【发布时间】:2020-07-11 01:38:36
【问题描述】:
我正在使用 JavaScript 和 discord.js 制作一个 Discord 机器人。在那里,我想向频道发送 RichEmbed/MessageEmbed(我不知道它是如何调用的)。但是,它没有发送嵌入,而是在 discord.js 中引发了错误。
TypeError: fields.flat is not a function
at Function.normalizeFields (D:\discord-bot\node_modules\discord.js\src\structures\MessageEmbed.js:436:8)
at MessageEmbed.addFields (D:\discord-bot\node_modules\discord.js\src\structures\MessageEmbed.js:252:42)
at commands.forEach.command (D:\discord-bot\src\js\core\commands\commandManager.js:55:19)
at Array.forEach (<anonymous>)
at helloWorldEmbed (D:\discord-bot\src\js\core\commands\commandManager.js:54:18)
at Object.call (D:\discord-bot\src\js\core\commands\commandManager.js:29:13)
at Client.client.on (D:\discord-bot\src\js\core\bot.js:16:49)
at Client.emit (events.js:182:13)
at MessageCreateAction.handle (D:\discord-bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (D:\discord-bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
我已经在寻找答案,但似乎只有我一个人对此有问题。
这是我使用的代码:
const embed = new MessageEmbed()
.setTitle('Hello World')
.setDescription('This is a test.')
.setColor('#3498db')
quotes.forEach(quote => {
embed.addField(quote.name, quote.description, true)
})
message.channel.send('Hello world.', embed)
【问题讨论】:
-
您的问题不在于您提供的代码。您是否正在使用现有的嵌入并尝试在任何地方删除字段?我怀疑您将字段设置为 null 或未定义,而不是空数组。
-
@Tarazed 发布该消息后,我不再对嵌入做任何事情
-
好的,错误消息的意思是 embed.fields.flat 不是一个函数,这意味着 embed.fields 不是它应该是的数组(数组有一个内置的平面函数)。某处正在改变它的类型。它不在此代码中,构造函数在字段中使用空数组创建嵌入,我看不到您更改的任何地方。
-
@Tarazed 好的,我进入 MessageEmbed.js 文件并记录了 fields 参数以及它是否是一个 instanceof 数组。确实是一个数组
-
好的,我检查了我的 node.js 版本,它似乎已经过时了,可能就是这个问题
标签: javascript discord discord.js