Get size of discord.js message request

Viewed 200

So I'm adding a check if an attachment can be sent with the message, and I want to know before sending if the attachment will fit into the message.
The naive answer would be of course to just check if the attachment size is over 8, 50, or 100MB depending the guild's boost status, but this is not actually the answer.

The size limit is for the whole request, not the attachment.
To try this out, create a file with e.g
truncate -s 8388607 test
and try to send it to Discord, either with your normal client, or with code e.g:

console.log(fs.statSync('test').size);
msg.channel.send({files: [{attachment: fs.readFileSync('test'), name: 'test'}]}).catch(console.error);

The file size is even one byte below 8MB but you still can't send it.

So the question is, how can I see what size my message request will be?
Can I maybe prepare the request, check its size, and then send it (if within size limits)?

0 Answers
Related