【问题标题】:How to use local file as thumbnail in DiscordJS embedded message?如何在 DiscordJS 嵌入消息中使用本地文件作为缩略图?
【发布时间】:2018-03-25 20:40:55
【问题描述】:

是否可以使用本地文件作为 DiscordJs 嵌入消息的缩略图?

"thumbnail": {
  "url": "../img/025.png"
},

这似乎不起作用。

(node:34721) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1):  
DiscordAPIError: Invalid Form Body 
embed.thumbnail.url: Not a well formed URL.

不过,普通 URL 也可以。

【问题讨论】:

  • 发送文件的正确 url。 https://example.com/img/025.png
  • 这不是我的选择。一些图像是同时生成的,它们没有存储在任何服务器上。但我已经得到了答案:)(如下)。

标签: javascript image discord


【解决方案1】:

我已经找到了答案。 链接到文件的正确 url 对我来说不是一个选项,因为生成了一些图像。

您可以将图像附加到邮件中,并将此附件用作缩略图。 最基本的例子:

const embed = {
  "title": "A Title" ,
  "color": 0xF96221,
  "thumbnail": {
    "url": "attachment://image.png"
  },
  "fields": [
    {
      "name": "Field 1:",
      "value": "One",
      "inline": false
    },
    {
      "name": "Field 2:",
      "value": "Two",
      "inline": true,
    },
    {
      "name":"Field 3:",
      "value":"Three",
      "inline": true
    },
  ],
  "footer": {
    "text":"Footer text"
  }
};

将图像附加到消息中:

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

【讨论】:

  • 我正在尝试对 discord webhook 做同样的事情,但这个解决方案对我不起作用。
【解决方案2】:

其他方法,希望对你有帮助

const attachment = new Discord.MessageAttachment('fileRoute', 'nameOfYourPicture');
const embed = new Discord.MessageEmbed()
  .setTitle('Title')
  .setColor('#8fda81')
  .addField('Message Send', messageToSend)
  .attachFiles(attachment)
  .setThumbnail('attachment://nameOfYourPicture');
message.channel.send(embed);

【讨论】:

  • 完美运行。谢谢。
猜你喜欢
  • 2021-02-01
  • 2021-02-26
  • 2021-06-17
  • 2019-12-28
  • 2021-11-03
  • 2014-02-09
  • 2020-10-19
  • 2018-05-31
  • 2011-08-11
相关资源
最近更新 更多