Right now I'm working on making a bot that will post a random image from a local folder on my VSC. However, posting an embed message with the image results in an error:
DiscordAPIError: Invalid Form Body embeds[0].image.url: Could not interpret "{'attachment': ['1.jpg', '2mkjR-3__400x400.jpg', '8921036_sa.jpg', '91Vk1mS1x3L.png'], 'name': None}" as string.
This can be reproduced with the sample code:
const Discord = require('discord.js');
const { Intents } = Discord;
const fs = require('fs');
const config = require('config');
const authToken = config.get('authToken');
const myIntents = new Intents([
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]);
const client = new Discord.Client({ intents: myIntents });
client.on("ready", (client) => {
console.log(`Logged in as ${client.user.tag}.`);
});
client.on("messageCreate", (message) => {
if ('.pic' === message.content) {
let files = fs.readdirSync('./assets/images/');
let chosenFile = files[Math.floor(Math.random() * files.length)];
const image = new Discord.MessageAttachment(files);
const embed = new Discord.MessageEmbed()
.setTitle('yeet')
.setImage(image)
.setFooter('By K4STOR','');
message.channel.send({embeds: [embed]});
}
});
client.login(authToken);
In addition to the above script, you'll need to:
- create an 'assets/images' directory and
- add at least one image;
- create a configuration file (e.g. 'config/local.json') and
- add an appropriate 'authToken' entry
How can the above code be fixed to send the image?