Hey folks! I’m trying to set up a Discord bot that can send images when users type a specific command. Right now I’ve got a basic bot running but I’m stuck on the image-sending part.
Here’s a simplified version of what I’m working with:
const DiscordBot = require('discord-bot-lib');
const config = require('./config.json');
const bot = new DiscordBot(config.token);
bot.on('messageReceived', (msg) => {
if (msg.content.startsWith('!')) {
const command = msg.content.slice(1).split(' ')[0];
if (command === 'coolpic') {
// How do I send an image here?
// I've tried stuff like:
// msg.channel.sendImage('./images/cool.png');
// bot.sendFile(msg.channel, './images/cool.png');
// But nothing seems to work
}
}
});
bot.connect();
I want the bot to send a specific image when someone types ‘!coolpic’. I’ve looked at the docs but I’m still confused. Any help would be awesome! Oh and if it matters, I’m using discord-bot-lib version 2.1.0.
This approach uses Discord.js v13+. Make sure your bot has the necessary permissions in the server to send attachments. Also, double-check that the file path is correct relative to your script’s location. Hope this helps!
I’ve been through this exact issue before, and I can tell you it’s a bit tricky at first. What worked for me was using the Discord.js library instead of discord-bot-lib. It’s more widely used and has better documentation.
Here’s what I did:
First, install Discord.js with npm install discord.js. Then, modify your code to use Discord.js. You’ll need to create a MessageAttachment object and send it as part of your message.
This approach has been reliable for me. Just make sure your file path is correct and that your bot has the necessary permissions in the server. Let me know if you run into any issues!