pub struct ButtonBuilder { /* private fields */ }Expand description
Builds a button for an action row or a section accessory.
Each style has its own constructor (D-197): primary,
secondary, success and danger
take the custom_id your app receives when the button is clicked; link
takes a URL and sends no interaction. Premium buttons have their own builder,
PremiumButtonBuilder (D-202).
build checks what Discord checks (D-200): a custom_id of 1-100
characters (counted as given, not trimmed), a label of at most 80 characters that is not
blank — or an emoji instead —, and for links a URL of at most 512 characters starting with
http://, https:// or discord://.
discord.js counterpart: ButtonBuilder.
§Examples
use djs_builders::{ActionRowBuilder, ButtonBuilder};
use djs_model::emoji::PartialEmoji;
let row = ActionRowBuilder::new()
.component(ButtonBuilder::success("confirm").label("Confirm"))
.component(ButtonBuilder::danger("cancel").label("Cancel"))
.component(ButtonBuilder::secondary("like").emoji(PartialEmoji::Unicode { name: "👍".into() }))
.component(ButtonBuilder::link("https://example.com/help").label("Help"))
.build()?;
assert_eq!(row.children().len(), 4);Implementations§
Source§impl ButtonBuilder
impl ButtonBuilder
Sourcepub fn primary(custom_id: impl Into<CompactString>) -> Self
pub fn primary(custom_id: impl Into<CompactString>) -> Self
Creates a button in the most prominent style (blurple), sending custom_id
(1-100 characters) to your app when clicked.
discord.js counterpart: setStyle(ButtonStyle.Primary).setCustomId(…).
Sourcepub fn secondary(custom_id: impl Into<CompactString>) -> Self
pub fn secondary(custom_id: impl Into<CompactString>) -> Self
Creates a button in the neutral style (grey).
discord.js counterpart: setStyle(ButtonStyle.Secondary).setCustomId(…).
Sourcepub fn success(custom_id: impl Into<CompactString>) -> Self
pub fn success(custom_id: impl Into<CompactString>) -> Self
Creates a button in the confirming style (green).
discord.js counterpart: setStyle(ButtonStyle.Success).setCustomId(…).
Sourcepub fn danger(custom_id: impl Into<CompactString>) -> Self
pub fn danger(custom_id: impl Into<CompactString>) -> Self
Creates a button in the destructive style (red).
discord.js counterpart: setStyle(ButtonStyle.Danger).setCustomId(…).
Sourcepub fn link(url: impl Into<CompactString>) -> Self
pub fn link(url: impl Into<CompactString>) -> Self
Creates a button that opens url (http://, https:// or discord://, at most 512
characters). It sends no interaction to your app.
discord.js counterpart: setStyle(ButtonStyle.Link).setURL(…).
Creates a button to buy the SKU sku_id, which shows the SKU’s name and price and
cannot have a label or emoji (D-202).
discord.js counterpart: setStyle(ButtonStyle.Premium).setSKUId(…).
Sourcepub fn label(self, label: impl Into<CompactString>) -> Self
pub fn label(self, label: impl Into<CompactString>) -> Self
Sets the text on the button (at most 80 characters, not blank). A button needs a label or an emoji.
discord.js counterpart: setLabel.
Sourcepub fn emoji(self, emoji: impl Into<PartialEmoji>) -> Self
pub fn emoji(self, emoji: impl Into<PartialEmoji>) -> Self
Sets the emoji shown before the label (or alone).
discord.js counterpart: setEmoji.
Sourcepub fn disabled(self, disabled: bool) -> Self
pub fn disabled(self, disabled: bool) -> Self
Sets whether the button is greyed out and cannot be clicked.
discord.js counterpart: setDisabled.
Sourcepub fn id(self, id: i32) -> Self
pub fn id(self, id: i32) -> Self
Sets the component’s numeric identifier (Discord generates one otherwise); it must be unique in the message.
discord.js counterpart: setId.
Sourcepub fn build(self) -> Result<Component, ValidationError>
pub fn build(self) -> Result<Component, ValidationError>
Checks the button and returns it as a [Component].
discord.js counterpart: ButtonBuilder#toJSON.
§Errors
ValidationError::Missing for an empty custom_id or a button with neither a label
nor an emoji, ValidationError::TooLong for a custom_id, label or URL above its
limit, ValidationError::InvalidUrl for a link with another scheme.
Sourcepub fn build_unchecked(self) -> Component
pub fn build_unchecked(self) -> Component
Returns the button without any check (D-180).
discord.js counterpart: ButtonBuilder#toJSON with validation off (disableValidators).