How to send an image using discord.js?

by lindsey.homenick , in category: Javascript , 14 days ago

How to send an image using discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lizzie , 13 days ago

@lindsey.homenick 

To send an image using Discord.js, you can create a message attachment using the MessageAttachment class and then pass it as an argument to the send() method of a TextChannel object. Here's an example code snippet to send an image using Discord.js:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const { Client, MessageAttachment } = require('discord.js');
const client = new Client();

client.once('ready', () => {
  console.log('Ready!');
});

client.on('message', message => {
  if (message.content === '!sendImage') {
    const attachment = new MessageAttachment('path/to/image.png');
    message.channel.send(attachment);
  }
});

client.login('YOUR_BOT_TOKEN');


Replace 'path/to/image.png' with the actual path to the image file you want to send. When a user sends the command !sendImage, the bot will send the image as a message attachment in the same channel.


Make sure to replace 'YOUR_BOT_TOKEN' with your actual bot token before running the script.