How to send attachments and embeds in the same message?

17,265

Solution 1

To understand how to accomplish your task, you first need to know how the TextBasedChannel.send() method works. Let's take a look at TextChannel.send() from the docs.

.send([content], [options])

content (StringResolvable): Text for the message.
options (MessageOptions or Attachment or RichEmbed): Options for the message, can also be just a RichEmbed or Attachment

Now, let's see how your usage applies.


message.channel.send(`text`, attachment);

In this case, `text` is serving as the content parameter of the method, and attachment is passed as the options parameter.

msg.channel.send(embed);

Here, the content parameter is omitted, and embed is passed as the options parameter.


While keeping your same style of code, you can utilize an object to hold both the embed and attachment for the options parameter.

// const { Attachment, RichEmbed } = require('discord.js');

const attachment = new Attachment('https://puu.sh/DTwNj/a3f2281a71.gif', 'test.gif');

const embed = new RichEmbed()
  .setTitle('**Test**')
  .setImage('attachment://test.gif') // Remove this line to show the attachment separately.

message.channel.send({ embed, files: [attachment] })
  .catch(console.error);

Solution 2

The TextChannel.send function can take different options.

So you can just do this:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', (msg) => {
  msg.channel.send({
    embed:  new Discord.RichEmbed()
      .setTitle('A slick little embed')
      .setColor(0xFF0000)
      .setDescription('Hello, this is a slick embed!'),
    files: [{
      attachment: './README.md',
      name: 'readme'
    }]
  })
    .then(console.log)
    .catch(console.error);
});

However, if you want to add an image in your embed, just follow the example in the guide or this example given in the api doc:

// Send an embed with a local image inside
channel.send('This is an embed', {
  embed: {
    thumbnail: {
         url: 'attachment://file.jpg'
      }
   },
   files: [{
      attachment: 'entire/path/to/file.jpg',
      name: 'file.jpg'
   }]
})
  .then(console.log)
  .catch(console.error);
Share:
17,265

Related videos on Youtube

Dhiraj Singh Karki
Author by

Dhiraj Singh Karki

Updated on June 04, 2022

Comments

  • Dhiraj Singh Karki
    Dhiraj Singh Karki almost 2 years

    How to send attachments and embeds in the same message?

    To send attachments:

    if (message.content === ';file') {
      const attachment = new Attachment('https://i.imgur.com/FpPLFbT.png');
      message.channel.send(`text`, attachment);
    }
    

    To send embeds:

    if (msg.content === ';name') {
      const embed = new Discord.RichEmbed()
        .setTitle(`About`)
        .setDescription(`My name is <@${msg.author.id}>`)
        .setColor('RANDOM');
      msg.channel.send(embed);
    }
    
  • Dhiraj Singh Karki
    Dhiraj Singh Karki almost 5 years
    Thankyou so much the information... :) It's working now :))) Appreciate your hard work...
  • Dhiraj Singh Karki
    Dhiraj Singh Karki almost 5 years
    this is also working.. thank you for your efforts :)
  • JackRed
    JackRed almost 5 years
    @DhirajSinghKarki don't hesitate to accept slothiful answer as it was more detailed than mine. This way people looking for an answer will know that the solution is working