【问题标题】:How do I use a local image on a discord.js rich embed?如何在 discord.js 富嵌入中使用本地图像?
【发布时间】:2018-12-14 10:42:33
【问题描述】:

我有这个代码:

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.js


    【解决方案1】:

    discord.js v13 及更高版本中,MessageEmbed#attachFiles 一直是deprecated。从现在开始,您应该直接将文件添加到响应中。

    MessageEmbed#attachFiles 已被移除;文件现在应该是 直接附加到消息而不是嵌入。

    // 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'] });
    

    【讨论】:

      【解决方案2】:

      另一种方法是:

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

      【讨论】:

      • 但是这样你并没有使用 Rich Embeds
      • Rich Embeds 现在是 v12 中的 MessageEmbeds,这个方法也是一个 MessageEmbed,只是做同样事情的不同方法。您也可以从文档中添加这种格式的字段和其他所有内容
      【解决方案3】:

      为 2020 年遇到同样问题的其他人更新了 Luke 的代码到 Discord.js v12

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

      【讨论】:

      • 使用编辑按钮编辑答案。
      【解决方案4】:

      这对我有用。

      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)
      

      【讨论】:

        【解决方案5】:

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

        您只能将图片上传到服务器/图片托管网站并获取 URL。

        【讨论】:

          猜你喜欢
          • 2021-09-06
          • 2011-04-18
          • 2021-10-28
          • 2020-09-03
          • 2020-12-30
          • 2018-07-08
          • 2022-08-19
          • 1970-01-01
          • 2020-08-10
          相关资源
          最近更新 更多