Dhiraj Singh Karki
Dhiraj Singh Karki

Reputation: 41

How to send attachments and embeds in the same message?

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);
}

Upvotes: 2

Views: 20973

Answers (2)

slothiful
slothiful

Reputation: 5623

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);

Upvotes: 4

JackRed
JackRed

Reputation: 1204

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);

Upvotes: 1

Related Questions