1

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.

1

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

5 Comments

I tried your first suggestion and it's still just sending the text with no image, the second suggestion gives me an error saying "images.tobuffer is not a function"
replace the "images" with your images
I have a const: const images = "My_dog.jpg" the line I'm using now is: message.channel.send("little dog", {files: [{attachment: images.toBuffer()}] }); this is giving me the error TypeError: images.toBuffer is not a function
I figured it out, I ended up just treating imgur links as a string and sending it like any other message. didn't use file: or `attachment.
but this will show the URL above right? at least that's what i get. also for pm4 files it won't autoplay
3

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.