Posting img with REST

I may be approaching this in an inefficient manner, so while I am asking specifically how to post an image using REST, I am open to other ideas. My use case involves occasionally posting as the bot, typically components like buttons that are permanent. I prefer not to use / commands for these, as I only need to update if I change the code. These utility functions are currently written as mini-apps, creating a client, logging in, and then posting on the ready event. But I don't think this is the best way to do so, as I am now logging in as a second client. So, I have been trying to use a simple REST post. I got it working; not really hard, I can do text, buttons, menus, etc., but I can't get files (i.e., images) to post. Here is the relevant code I am using.
await rest.post(
Routes.channelMessages(config.devMaps.channels['selfService'] ?? ''),
{
body: {
files: [
{
attachment: `${join(import.meta.dirname, '../assets/images/self-service.png')}`,
},
],
content: 'Test',
},
},
);
await rest.post(
Routes.channelMessages(config.devMaps.channels['selfService'] ?? ''),
{
body: {
files: [
{
attachment: `${join(import.meta.dirname, '../assets/images/self-service.png')}`,
},
],
content: 'Test',
},
},
);
3 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
duck
duck3mo ago
files aren't actually sent in the body alongside content this is something discord.js heavily simplifies for you that @discordjs/rest does not files are sent alongside the body in the RequestData as documented, they're expected to be RawFiles afaik while <RawFile>.data does accept strings, it does not resolve file paths for you you'll want to read the file to a buffer yourself
Pibbles
PibblesOP3mo ago
Ok, I will go look into that. Hey, thank you much, that was not to big a deal once you gave me the direction to go. Really appricate it.
Want results from more Discord servers?
Add your server