ローカルの画像を埋め込みで使用するサンプル
#TODO 良いタイトル
v14
AttachmentBuilderを使用する。
.setName(name)と.setThumbnail(attachment://name)のnameは同じにしておく。(これは識別用?)
.setFile(attachment)のattachment内はファイルパス、Buffer、FileStreamなどが使用可能。
code:js
const { EmbedBuilder, AttachmentBuilder } = require("discord.js");
const embed = new EmbedBuilder().setThumbnail("attachment://thumbnail.jpg");
const attachment = new AttachmentBuilder()
.setName("thumbnail.jpg")
.setFile("./picture.jpg")
channel.send({
files: attachment,
embeds: embed
})
v13
MessageEmbedのattachFiles は廃止された。
attachment://<ファイル名>.<拡張子> の形式で添付したファイルにアクセスできる。
code:js
const buffer = //...
const embed = new Discord.MessageEmbed().setThumbnail("attachment://thumbnail.jpg");
const attachment = new Discord.MessageAttachment(buffer, "thumbnail.jpg");
channel.send({ files: attachment, embeds: embed });
v12
MessageEmbedのattachFiles と、attachment:// URI Schemeを使う
attachment://<ファイル名>.<拡張子> の形式で attachFiles によって添付したファイルにアクセスできる。
Sets the file to upload alongside the embed. This file can be accessed via attachment://fileName.extension when setting an embed image or author/footer icons. Multiple files can be attached.
MessageEmbed#attachFiles
正直ここに書いてあるのは不親切だと思うtig.icon
code:js
const buffer = //...
const embed = new Discord.MessageEmbed()
.attachFiles(new Discord.MessageAttachment(buffer, "thumbnail.jpg"))
.setThumbnail("attachment://thumbnail.jpg");
ここではサムネイルを例としているが Image や Auther、Footerなどでも使用できる
ちなみに、内部では普通の添付ファイルと同じように扱われているので 埋め込み 内で添付したファイルを使用しない場合通常の添付ファイルと同様に扱われる。
関連
埋め込みを送信する
埋め込みに複数の画像を表示させる
クラスを使って埋め込みを作る
MessageEmbedを使って埋め込みを送信するサンプル
MessageEmbed
MessageAttachment
https://discord.com/channels/391390986770710528/703920554629136434/834062337601962015