-
-
Notifications
You must be signed in to change notification settings - Fork 150
Document embed::ImageSource::attachment usage #2460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from all commits
b8c6131
55b7460
17ddd3c
e30a7ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,36 @@ let embed = EmbedBuilder::new() | |
| # } | ||
| ``` | ||
|
|
||
| When using `ImageSource::attachment`, the image must also be sent as a message attachment. Here's a more complete example demonstrating how to send a message with an attachment and an embed that references it: | ||
|
|
||
| ```rust | ||
| # #[allow(unused_variables)] | ||
| # async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| use twilight_http::Client; | ||
| use twilight_model::id::Id; | ||
| use twilight_model::id::marker::ChannelMarker; | ||
| use twilight_util::builder::embed::{EmbedBuilder, ImageSource}; | ||
| use twilight_model::channel::message::MessageFlags; | ||
|
|
||
| let client = Client::new("my token".to_owned()); | ||
| let channel_id = Id::<ChannelMarker>::new(123); // Replace with your channel ID | ||
|
|
||
| let embed = EmbedBuilder::new() | ||
| .description("Here's a cool image of Twilight Sparkle as an attachment!") | ||
| .image(ImageSource::attachment("bestpony.png")?) | ||
| .build(); | ||
|
|
||
| let message = client | ||
| .create_message(channel_id) | ||
| .attachments(&[("bestpony.png".as_bytes(), "bestpony.png".to_owned(), 0)])? | ||
| .embeds(&[embed.build()])? | ||
|
Comment on lines
+105
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| .exec() | ||
| .await?; | ||
|
|
||
| # Ok(()) | ||
| # } | ||
| ``` | ||
|
|
||
| ### Link | ||
|
|
||
| The `link` feature enables the parsing and formatting of URLs to resources, such | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -237,6 +237,28 @@ impl EmbedBuilder { | |||||
| /// .build(); | ||||||
| /// # Ok(()) } | ||||||
| /// ``` | ||||||
| /// | ||||||
| /// Set the image source to a file attachment: | ||||||
| /// | ||||||
| /// ``` | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| /// # fn main() -> Result<(), Box<dyn std::error::Error>> { | ||||||
| /// use twilight_model::http::attachment::Attachment; | ||||||
| /// use twilight_util::builder::embed::{EmbedBuilder, ImageSource}; | ||||||
| /// | ||||||
| /// let filename = "hello.txt"; | ||||||
| /// let bytes = vec![104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]; // "hello world" in unicode code points | ||||||
| /// | ||||||
| /// let attachment = Attachment::from_bytes(filename.to_string(), bytes, 1); | ||||||
| /// | ||||||
| /// let source = ImageSource::attachment(filename)?; | ||||||
| /// let embed = EmbedBuilder::new() | ||||||
| /// .image(source) | ||||||
| /// .validate()? | ||||||
| /// .build(); | ||||||
| /// | ||||||
| /// // Then, send both the embed and the attachment with your message | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think some statements are missing below here. |
||||||
| /// # Ok(()) } | ||||||
| /// ``` | ||||||
| #[allow(clippy::missing_const_for_fn)] | ||||||
| pub fn image(mut self, image_source: ImageSource) -> Self { | ||||||
| self.0.image = Some(EmbedImage { | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is a bit wrong, you can no longer have the 3-tuple to make a attachment, instead you should use the
Attachmentstruct.So it should use
Attachment::from_bytes(filename: String, file: Vec<[u8]>, id: u64)instead. The first field in the tuple (orfileinAttachment::from_bytes) should be the actual image data.