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

0tdrvxhp  于 9个月前  发布在  Node.js
关注(0)|答案(1)|浏览(110)

我尝试使用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)

8gsdolmq

8gsdolmq1#

您需要使用缓冲区将数据流传输到构建器。您可以使用Buffer.from(<target>)来完成此操作。您可以了解有关buffers here的更多信息或访问Node.js docs here
使用您的源代码的示例:

const jsonFile = new AttachmentBuilder(Buffer.from(JSON.stringify(data)), {
    name: embedName + ".json",
  });

字符串

相关问题