如何在 discord.js 丰富的嵌入中使用本地图像?

How do I use a local image on a discord.js rich embed?

我有这个代码:

var datos = ["dato1","dato2","dato3"]

console.log ("》" + message.author.username + " introdujo el comando:  " + message.content + "  en  " + message.guild.name);

let embed = new discord.RichEmbed()
    .setTitle("Datos sobre gatos  ")

    .setColor(12118406)
    .setDescription(datos[Math.floor(Math.random() * datos.length)])
    .setFooter("© 2018 República Gamer LLC", bot.user.avatarURL)
    .setImage("http://i.imgur.com/sYyH2IM.png")
message.channel.send({embed})

.catch ((err) => {
    console.error(err);

    let embed = new discord.RichEmbed()
        .setColor(15806281)
        .setTitle("❌ Ocurrió un error")
        .setDescription("Ocurrió un error durante la ejecución del comando")
    message.channel.send({embed})
})

如何使用本地图像路径代替 URL(在 .setImage() 行)

您好!不幸的是,Discord 的 API 只接受 URLs 而不是本地路径。

您只能将图片上传到 server/image 托管网站并获得 URL。

这对我有用。

const attachment = new Discord.Attachment('./card_images/sample.png', 'sample.png');
const embed = new RichEmbed()
        .setTitle('Wicked Sweet Title')
        .attachFile(attachment)
        .setImage('attachment://sample.png');
message.channel.send({embed}).catch(console.error)

已将 Luke 的代码更新为 Discord.js v12,供 2020 年遇到同样问题的任何其他人使用

const attachment = new Discord
                      .MessageAttachment('./card_images/sample.png', 'sample.png');
const embed = new Discord.MessageEmbed()
     .setTitle('Wicked Sweet Title')
     .attachFiles(attachment)
     .setImage('attachment://sample.png');

message.channel.send({embed});

另一种方法是:

const attachment = new Discord.MessageAttachment('./help.png', 'help.png');

message.channel.send({
  embed: {
    files: [
      attachment
    ],
    image: {
      url: 'attachment://help.png'
    }
  }
});

discord.js v13 and on, MessageEmbed#attachFiles has been deprecated。从现在开始,您应该直接将文件添加到响应中。

MessageEmbed#attachFiles has been removed; files should now be attached directly to the message instead of the embed.

// Before v13
const embed = new Discord.MessageEmbed().setTitle('Attachments').attachFiles(['./image1.png', './image2.jpg']);
channel.send(embed);
// v13
const embed = new Discord.MessageEmbed().setTitle('Attachment').setImage('attachment://image.png');
channel.send({ embeds: [embed], files: ['./image.png'] });