I'm trying to send some javascript object (as JSON data in a json file) with a command using discord.js. Yet It's not as easy as sending an image. I tried using AttachmentBuilder
(with/without JSON.stringify
), with and without the additional data being the name:
const jsonFile = new AttachmentBuilder(JSON.stringify(data), {
name: embedName + ".json",
});
message.reply({ content: "Data:", files : [jsonFile]})
But I get this weird error no matter how i put my data:
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)
You would need to use a buffer to stream the data to the builder. You can do so by using Buffer.from(<target>)
. You can learn more about buffers here or access the Node.js docs here.
Example using your source:
const jsonFile = new AttachmentBuilder(Buffer.from(JSON.stringify(data)), {
name: embedName + ".json",
});