serenity
serenity copied to clipboard
Embed Footer, Thumbnail & Image can be local files, not only HTTP(S)
In the documentation it states that the Footer, Thumbnail & Image only support HTTP(S).
https://docs.rs/serenity/latest/serenity/builder/struct.CreateEmbed.html#method.image https://docs.rs/serenity/latest/serenity/builder/struct.CreateEmbed.html#method.thumbnail https://docs.rs/serenity/latest/serenity/builder/struct.CreateEmbedFooter.html#method.icon_url
This is a bit misleading as you can use local files when attached to the message. I tried fixing the docs, but have not a setup ready at the moment to test if the docs are working and if it works in pure Serenity. (In poise it works, but uses the CreateReply
function.)
Would love if someone could check this and add something to the docs about how to use local files.
use serenity::builder::{CreateEmbed, CreateAttachment, CreateEmbedFooter};
// First we create an attachment
let attachment = CreateAttachment::path("images/loss.jpg").await.expect("Could not find image");
// Then we create an embed where we add the image/footer/thumbnail
let embed = CreateEmbed::default()
.image("attachment://loss.jpg")
.thumbnail("attachment://loss.jpg")
.footer(
CreateEmbedFooter::new("Loss footer")
.icon_url("attachment://loss.jpg"),
);
// I think this should work in Serenity:
let builder = CreateMessage::new().embed(embed).add_file(attachment);
// In Poise we do the following and that works for sure:
ctx.send(CreateReply::default().embed(embed).attachment(attachment))
.await?;
You can create an attachment an attach it as such
let img_path: &Path = Path::new("./img.png");
let attachment_bytes: Vec<u8> = std::fs::read(img_path).expect("Failed to read card file");
let attachment: CreateAttachment = CreateAttachment::bytes(
attachment_bytes,
"img.png"
);
// attachment can be routed thru an embed aswell
CreateMessage::default()
.add_file(attachment)
.content("@everyone")