I've been looking up how to send an image with a bot command and only found how to send the same image from the repo of the project or a static URL, but I didn't see how to make a command that allows the user to choose an image and send it. I need this feature to make a bot command that sends an announcement in a specific channel, and including an image in that announcement should be optional.
I've tried this:
async execute(interaction) {
if (!interaction.member.roles.cache.some((role) => role.name === "admin")) {
await interaction.reply(`You don't have access !`);
return;
} else {
const { MessageAttachment, MessageEmbed } = require("discord.js");
//extracting the image path
const image_path = interaction.options.getString("image_path");
const file = new MessageAttachment(image_path);
const exampleEmbed = new MessageEmbed().setTitle("Announcement image").setImage(image_path);
//converting the msg to string
const message = interaction.options.getString("message");
await interaction.reply(`${message} message sent !`);
channel.send({ embeds: [exampleEmbed], files: [file] });
}
}