serenity-rs / serenity

A Rust library for the Discord API.
https://discord.gg/serenity-rs
ISC License
4.67k stars 573 forks source link

Embed Footer, Thumbnail & Image can be local files, not only HTTP(S) #2776

Open keiveulbugs opened 6 months ago

keiveulbugs commented 6 months ago

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?;
floris-xlx commented 5 months ago

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")
ivinjabraham commented 2 weeks ago

This seems correct, not sure why the docs say it only supports HTTPS for those methods. CreateEmbed::attachment even specifies CreateEmbed::image works with local attachments by using the attachment:// syntax. So do the Discord docs.