admin管理员组

文章数量:1323187

How to send attachments and embeds in the same message?

To send attachments:

if (message.content === ';file') {
  const attachment = new Attachment('.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);
}

How to send attachments and embeds in the same message?

To send attachments:

if (message.content === ';file') {
  const attachment = new Attachment('https://i.imgur./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);
}
Share Improve this question edited Jul 16, 2019 at 16:52 Federico Grandi 6,7965 gold badges33 silver badges51 bronze badges asked Jul 16, 2019 at 4:56 Dhiraj Singh KarkiDhiraj Singh Karki 411 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

To understand how to acplish 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);

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

本文标签: javascriptHow to send attachments and embeds in the same messageStack Overflow