Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions book/src/chapter_1_crates/section_7_first_party/section_4_util.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)])?
Copy link
Member

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 Attachment struct.

So it should use Attachment::from_bytes(filename: String, file: Vec<[u8]>, id: u64) instead. The first field in the tuple (or file in Attachment::from_bytes) should be the actual image data.

.embeds(&[embed.build()])?
Comment on lines +105 to +106
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attachments and embeds no longer return a result it it instead moved to the end.

.exec()
.await?;

# Ok(())
# }
```

### Link

The `link` feature enables the parsing and formatting of URLs to resources, such
Expand Down
22 changes: 22 additions & 0 deletions twilight-util/src/builder/embed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,28 @@ impl EmbedBuilder {
/// .build();
/// # Ok(()) }
/// ```
///
/// Set the image source to a file attachment:
///
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// ```
/// ```no_run

/// # 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
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Expand Down
Loading