Skip to main content

ButtonBuilder

Struct ButtonBuilder 

Source
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

Source

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(…).

Source

pub fn secondary(custom_id: impl Into<CompactString>) -> Self

Creates a button in the neutral style (grey).

discord.js counterpart: setStyle(ButtonStyle.Secondary).setCustomId(…).

Source

pub fn success(custom_id: impl Into<CompactString>) -> Self

Creates a button in the confirming style (green).

discord.js counterpart: setStyle(ButtonStyle.Success).setCustomId(…).

Source

pub fn danger(custom_id: impl Into<CompactString>) -> Self

Creates a button in the destructive style (red).

discord.js counterpart: setStyle(ButtonStyle.Danger).setCustomId(…).

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(…).

Source

pub fn premium(sku_id: SkuId) -> PremiumButtonBuilder

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(…).

Source

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.

Source

pub fn emoji(self, emoji: impl Into<PartialEmoji>) -> Self

Sets the emoji shown before the label (or alone).

discord.js counterpart: setEmoji.

Source

pub fn disabled(self, disabled: bool) -> Self

Sets whether the button is greyed out and cannot be clicked.

discord.js counterpart: setDisabled.

Source

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.

Source

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.

Source

pub fn build_unchecked(self) -> Component

Returns the button without any check (D-180).

discord.js counterpart: ButtonBuilder#toJSON with validation off (disableValidators).

Trait Implementations§

Source§

impl Clone for ButtonBuilder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ButtonBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ButtonBuilder

Source§

impl From<ButtonBuilder> for Component

Source§

fn from(button: ButtonBuilder) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for ButtonBuilder

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ButtonBuilder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.