我正在尝试使用discord.js 通过命令发送一些 javascript 对象(作为 json 文件中的 JSON 数据)。但这并不像发送图像那么容易。我尝试使用
AttachmentBuilder
(有/没有 JSON.stringify
),有或没有附加数据的名称:
const jsonFile = new AttachmentBuilder(JSON.stringify(data), {
name: embedName + ".json",
});
message.reply({ content: "Data:", files : [jsonFile]})
但是无论我如何放置数据,我都会遇到这个奇怪的错误:
TypeError: Cannot read properties of undefined (reading 'Symbol(Symbol.asyncIterator)')
at DataResolver.resolveFile (C:\Users\___\node_modules\discord.js\src\util\DataResolver.js:117:24)
at MessagePayload.resolveFile (C:\Users\___\node_modules\discord.js\src\structures\MessagePayload.js:260:54)
at C:\Users\___\node_modules\discord.js\src\structures\MessagePayload.js:225:85
at Array.map (<anonymous>)
at MessagePayload.resolveFiles (C:\Users\___\node_modules\discord.js\src\structures\MessagePayload.js:225:56)
at StringSelectMenuInteraction.reply (C:\Users\___\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:109:70)
at module.exports.run (C:\Users\___\interactions\StringSelectMenu\select-embed.js:38:20)
at async Object.run (C:\Users\___\events\interactionCreate.js:24:11)
at async SkeeBot.<anonymous> (C:\Users\___\classes\SkeeBot.js:96:11)
您需要使用缓冲区将数据传输到构建器。您可以使用
Buffer.from(<target>)
来完成此操作。您可以在此处了解有关缓冲区的更多信息或在此处访问Node.js 文档。
使用您的来源的示例:
const jsonFile = new AttachmentBuilder(Buffer.from(JSON.stringify(data)), {
name: embedName + ".json",
});