pub struct EmbedBuilder { /* private fields */ }Expand description
Builds an embed — a rich content block for a message, an interaction reply or a webhook — and checks it against Discord’s limits.
Every setter takes and returns the builder (D-178), stores the value as given and never
fails; build checks everything at the end (D-179) and returns the first
problem as a ValidationError (D-182). build_unchecked skips
the checks (D-180).
build checks what Discord checks, as measured on the live API (D-183, D-188, D-191):
| Part | Rule |
|---|---|
title | at most 256 characters |
description | at most 4096 characters |
fields | at most 25; name at most 256, value at most 1024 characters |
footer text | at most 2048 characters |
author name | at most 256 characters |
| all of the texts above | at most 6000 characters together |
color | at most 0xFFFFFF |
url, author URL | http:// or https:// |
image, thumbnail, icons | http://, https:// or attachment:// |
| the whole embed | a non-blank title, description, footer or author name, a field, an image or a thumbnail |
Characters are Unicode code points after trimming leading and trailing whitespace, the way Discord counts them; schemes are compared without regard to case. Empty or blank texts are allowed — Discord accepts them, as long as the embed has some content.
The 6000-character total also applies to all embeds of a message together (at most
10 embeds); check a whole list with validate_embeds.
discord.js counterpart: EmbedBuilder. Unlike discord.js, the setters do not throw, an
emoji counts as one character (not two UTF-16 units), and empty field names and values
are allowed only because Discord allows them.
§Examples
use djs_builders::{EmbedBuilder, EmbedFooterBuilder};
let embed = EmbedBuilder::new()
.title("Server status")
.url("https://example.com/status")
.description("Everything is running.")
.color(0x57_F287)
.author("Status bot")
.field("Uptime", "14 days")
.inline_field("Ping", "42 ms")
.inline_field("Players", "17")
.footer(EmbedFooterBuilder::new("Updated").icon_url("https://example.com/icon.png"))
.thumbnail("attachment://logo.png")
.build()?;
assert_eq!(embed.title.as_deref(), Some("Server status"));
assert_eq!(embed.fields.len(), 3);
assert!(embed.fields[1].inline);A problem is reported by build, not by the setter:
use djs_builders::{EmbedBuilder, ValidationError};
let error = EmbedBuilder::new()
.title("Results")
.url("ftp://example.com/results")
.build()
.unwrap_err();
assert_eq!(error.field(), "url");
assert!(matches!(error, ValidationError::InvalidUrl { .. }));Send it with a message:
use djs_builders::EmbedBuilder;
use djs_model::rest::CreateMessage;
let message = CreateMessage::default().embed(EmbedBuilder::new().description("Hello!").build()?);
assert_eq!(
serde_json::to_string(&message).unwrap(),
r#"{"embeds":[{"description":"Hello!"}]}"#
);Implementations§
Source§impl EmbedBuilder
impl EmbedBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a builder for an empty embed.
An embed needs some content before build accepts it: a title, a
description, a field, an author, a footer, an image or a thumbnail.
discord.js counterpart: new EmbedBuilder().
Sourcepub fn title(self, title: impl Into<CompactString>) -> Self
pub fn title(self, title: impl Into<CompactString>) -> Self
Sets the title (at most 256 characters), shown in bold at the top.
discord.js counterpart: EmbedBuilder#setTitle.
Sourcepub fn url(self, url: impl Into<CompactString>) -> Self
pub fn url(self, url: impl Into<CompactString>) -> Self
Sets the URL the title links to (http:// or https://).
Embeds of one message that share a URL are shown as one by Discord, which is how several images can appear in a single embed.
discord.js counterpart: EmbedBuilder#setURL.
Sourcepub fn description(self, description: impl Into<CompactString>) -> Self
pub fn description(self, description: impl Into<CompactString>) -> Self
Sets the description (at most 4096 characters), the main text of the embed. It can use markdown.
discord.js counterpart: EmbedBuilder#setDescription.
Sourcepub fn color(self, color: u32) -> Self
pub fn color(self, color: u32) -> Self
Sets the color of the left border as an RGB integer (0xRRGGBB, at most 0xFFFFFF).
discord.js counterpart: EmbedBuilder#setColor with a number.
§Examples
use djs_builders::EmbedBuilder;
let embed = EmbedBuilder::new().title("x").color(0x58_65F2).build().unwrap();
assert_eq!(embed.color, Some(0x58_65F2));Sourcepub fn color_rgb(self, red: u8, green: u8, blue: u8) -> Self
pub fn color_rgb(self, red: u8, green: u8, blue: u8) -> Self
Sets the color of the left border from its red, green and blue parts.
discord.js counterpart: EmbedBuilder#setColor with an [r, g, b] tuple.
§Examples
use djs_builders::EmbedBuilder;
let embed = EmbedBuilder::new().title("x").color_rgb(88, 101, 242).build().unwrap();
assert_eq!(embed.color, Some(0x58_65F2));Sets the author shown above the title: a name (at most 256 characters), or an
EmbedAuthorBuilder with a link and an icon too.
discord.js counterpart: EmbedBuilder#setAuthor.
§Examples
use djs_builders::{EmbedAuthorBuilder, EmbedBuilder};
let plain = EmbedBuilder::new().author("Status bot").build().unwrap();
assert_eq!(plain.author.unwrap().name, "Status bot");
let linked = EmbedBuilder::new()
.author(
EmbedAuthorBuilder::new("Status bot")
.url("https://example.com")
.icon_url("https://example.com/avatar.png"),
)
.build()
.unwrap();
assert_eq!(linked.author.unwrap().url.as_deref(), Some("https://example.com"));Sets the footer: a text (at most 2048 characters), or an EmbedFooterBuilder with
an icon too. A timestamp is shown next to it.
discord.js counterpart: EmbedBuilder#setFooter.
§Examples
use djs_builders::{EmbedBuilder, EmbedFooterBuilder};
let embed = EmbedBuilder::new()
.footer(EmbedFooterBuilder::new("Page 1 of 3").icon_url("attachment://icon.png"))
.build()
.unwrap();
assert_eq!(embed.footer.unwrap().text, "Page 1 of 3");Sourcepub fn field(
self,
name: impl Into<CompactString>,
value: impl Into<CompactString>,
) -> Self
pub fn field( self, name: impl Into<CompactString>, value: impl Into<CompactString>, ) -> Self
Adds a field on its own line: a name (at most 256 characters) above a value (at
most 1024). An embed has at most 25 fields.
discord.js counterpart: EmbedBuilder#addFields with { name, value }.
Sourcepub fn inline_field(
self,
name: impl Into<CompactString>,
value: impl Into<CompactString>,
) -> Self
pub fn inline_field( self, name: impl Into<CompactString>, value: impl Into<CompactString>, ) -> Self
Adds an inline field, shown next to the inline fields around it (up to three per row on desktop).
discord.js counterpart: EmbedBuilder#addFields with { name, value, inline: true }.
Sourcepub fn fields(self, fields: impl IntoIterator<Item = EmbedField>) -> Self
pub fn fields(self, fields: impl IntoIterator<Item = EmbedField>) -> Self
Adds several fields at once, after those already added.
discord.js counterpart: EmbedBuilder#addFields.
§Examples
use djs_builders::EmbedBuilder;
use djs_model::message::EmbedField;
let scores = [("Ada", 31), ("Grace", 27)];
let embed = EmbedBuilder::new()
.title("Scores")
.fields(scores.iter().map(|(name, score)| EmbedField::new(*name, score.to_string(), true)))
.build()
.unwrap();
assert_eq!(embed.fields[1].value, "27");Sourcepub fn image(self, url: impl Into<CompactString>) -> Self
pub fn image(self, url: impl Into<CompactString>) -> Self
Sets the large image below the fields: a link (http://, https://) or a file
uploaded with the message (attachment://name.png).
discord.js counterpart: EmbedBuilder#setImage.
Sourcepub fn thumbnail(self, url: impl Into<CompactString>) -> Self
pub fn thumbnail(self, url: impl Into<CompactString>) -> Self
Sets the small image in the top-right corner, with the same URLs as
image.
discord.js counterpart: EmbedBuilder#setThumbnail.
Sourcepub fn timestamp(self, timestamp: Timestamp) -> Self
pub fn timestamp(self, timestamp: Timestamp) -> Self
Sets the time shown in the footer, in each viewer’s time zone.
A timestamp alone is not content: an embed with only a timestamp is rejected.
discord.js counterpart: EmbedBuilder#setTimestamp with a date.
Sourcepub fn timestamp_now(self) -> Self
pub fn timestamp_now(self) -> Self
Sets the time shown in the footer to now (the system clock).
discord.js counterpart: EmbedBuilder#setTimestamp() without an argument.
§Panics
If the system clock is set before 1970 (see [Timestamp::now]).
Sourcepub fn build(self) -> Result<Embed, ValidationError>
pub fn build(self) -> Result<Embed, ValidationError>
Checks the embed against Discord’s limits (see the type docs) and returns it.
discord.js counterpart: EmbedBuilder#toJSON (which does not validate; the setters
do).
§Errors
The first ValidationError found, checking the parts in this order: title,
description, URL, color, fields, footer, image, thumbnail, author, the 6000-character
total (TooLong with the field embed), and finally
whether the embed has any content (Empty).
Sourcepub fn build_unchecked(self) -> Embed
pub fn build_unchecked(self) -> Embed
Returns the embed without any check (D-180) — for example when Discord has raised a
limit before this crate has. Discord rejects an invalid embed with
50035 Invalid Form Body.
discord.js counterpart: EmbedBuilder#toJSON with disableValidators().
Trait Implementations§
Source§impl Clone for EmbedBuilder
impl Clone for EmbedBuilder
Source§impl Debug for EmbedBuilder
impl Debug for EmbedBuilder
Source§impl Default for EmbedBuilder
impl Default for EmbedBuilder
impl Eq for EmbedBuilder
Source§impl From<Embed> for EmbedBuilder
Starts from an existing embed — for example one received in a message, to send it again
with changes. Every part is kept; a setter replaces its part (and
field adds after the existing fields).
impl From<Embed> for EmbedBuilder
Starts from an existing embed — for example one received in a message, to send it again
with changes. Every part is kept; a setter replaces its part (and
field adds after the existing fields).
discord.js counterpart: EmbedBuilder.from(embed) / new EmbedBuilder(data).
§Examples
use djs_builders::EmbedBuilder;
use djs_model::message::Embed;
let received: Embed = serde_json::from_str(r#"{"title":"Poll","description":"Open"}"#).unwrap();
let closed = EmbedBuilder::from(received).description("Closed").build().unwrap();
assert_eq!(closed.title.as_deref(), Some("Poll"));
assert_eq!(closed.description.as_deref(), Some("Closed"));