jdddddddddd
jdddddddddd

Reputation: 13

send an image using discord.js

I have been following a few different guides to program a simple discord bot. Everything works except I cannot get it to send an image. I have looked at these previous questions 1 2, but their solutions are not working for me. This is my code:

const {Client, Intents} = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = '?';

client.once('ready', () => {
  console.log("Dog is online!");
});

client.on('messageCreate', message => {
  if(!message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if(command === 'ping') {
    message.channel.send('pong!');
  }
  else if (command === 'bark') {
    message.channel.send('bark bark bark grrrr...')
  }
  else if(command === 'nick') {
    message.channel.send('grrr...');
  }
  else if (command === 'pic') {
    message.channel.send("little dog", {files: ["https://i.imgur.com/xxXXXxx.jpeg"] });
  }

});

//must be last line
client.login('');

my client login is there in my editor, just not sharing it here. the "pic" command is what is not working. It displays the "little dog" text, but does not send the image. The only reason I'm using imgur is because I'm not sure if you can send local images; if someone knows a solution using local files I'll take it.

Upvotes: 1

Views: 14412

Answers (2)

m ihsan
m ihsan

Reputation: 137

You can use

files: [{ attachment: "YourImage.jpg" }];

You can also rename the image with

files: [{ attachment: <images>.toBuffer(), name: 'newName.png' }];

Example:

message.channel.send({ files: [{ attachment: 'YourImage.png' }] });

the <images> is your image variable

const images = blablabla

Upvotes: 8

RasmonT
RasmonT

Reputation: 403

According to the V13 discord.js

You can do following, this is also working for me.

const { MessageAttachment } = require('discord.js')

const attachment = new MessageAttachment('URL'); //ex. https://i.imgur.com/random.jpg

message.channel.send({ content: "I sent you a photo!", files: [attachment] })

This example will send a photo with the text I sent you a photo!

Upvotes: 3

Related Questions