使用discord.js将数据作为json文件发送

问题描述 投票:0回答:1

我正在尝试使用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)
javascript node.js discord discord.js buffer
1个回答
0
投票

您需要使用缓冲区将数据传输到构建器。您可以使用

Buffer.from(<target>)
来完成此操作。您可以在此处了解有关缓冲区的更多信息或在此处访问Node.js 文档

使用您的来源的示例:

const jsonFile = new AttachmentBuilder(Buffer.from(JSON.stringify(data)), {
    name: embedName + ".json",
  });
© www.soinside.com 2019 - 2024. All rights reserved.