Skip to main content

SlashCommandBuilder

Struct SlashCommandBuilder 

Source
pub struct SlashCommandBuilder { /* private fields */ }
Expand description

Builds a slash command (CHAT_INPUT): a name users type after /, a description, and options, subcommands or subcommand groups.

Register the result with Rest::create_global_application_command (or the guild or bulk variants). As for every builder, setters only store and build checks everything (D-179); build_unchecked skips the checks (D-180).

build checks what Discord checks, as measured on the live API (D-194–D-196):

PartRule
names (command, subcommands, options, translations)1-32 lower-case letters, numbers, -, _, ʼ
descriptions (and translations)1-100 characters, not blank
options per levelat most 25, unique names, required ones first
the command’s own optionsall subcommands / groups, or none
a group’s optionssubcommands only
a subcommand’s optionsno subcommands or groups
choicesat most 25; names 1-100 characters; string values at most 100
min_length / max_length0-6000 / 1-6000, max_length ≥ min_length
integers / numberswithin ±(2^53 − 1) / ±2^53; max_value ≥ min_value
choices and autocompletenot both
the whole commandat most 8000 characters

Characters are Unicode code points; descriptions and choice texts are measured after trimming. The 8000 total counts every name, description and string choice value, taking the longest translation of each (D-195).

discord.js counterpart: SlashCommandBuilder. Unlike discord.js, options are added with one option method and mixing subcommands with options is reported by build instead of the type system (D-192, D-193).

§Examples

use djs_builders::{
    SlashCommandBuilder, SlashCommandIntegerOption, SlashCommandStringOption,
    SlashCommandUserOption,
};
use djs_model::{interaction::InteractionContextType, permissions::Permissions, util::Locale};

let ban = SlashCommandBuilder::new("ban", "Bans a member")
    .name_localization(Locale::Polish, "zbanuj")
    .description_localization(Locale::Polish, "Banuje członka")
    .default_member_permissions(Permissions::BAN_MEMBERS)
    .contexts([InteractionContextType::Guild])
    .option(SlashCommandUserOption::new("user", "Who to ban").required(true))
    .option(SlashCommandStringOption::new("reason", "Why").max_length(512))
    .option(
        SlashCommandIntegerOption::new("days", "Days of messages to delete")
            .choice("None", 0)
            .choice("A week", 7),
    )
    .build()?;
assert_eq!(ban.options.len(), 3);

A problem is reported by build:

use djs_builders::{SlashCommandBuilder, SlashCommandStringOption, ValidationError};

let error = SlashCommandBuilder::new("Ping", "Checks the bot").build().unwrap_err();
assert_eq!(error.to_string(), "name must be lower case: use 'p' instead of 'P'");

let error = SlashCommandBuilder::new("echo", "Repeats a text")
    .option(SlashCommandStringOption::new("prefix", "Put before"))
    .option(SlashCommandStringOption::new("text", "What to repeat").required(true))
    .build()
    .unwrap_err();
assert_eq!(error.field(), "options[1].required");

Implementations§

Source§

impl SlashCommandBuilder

Source

pub fn new( name: impl Into<CompactString>, description: impl Into<CompactString>, ) -> Self

Creates a slash command named name (1-32 lower-case letters, numbers, -, _ or ʼ) and described by description (1-100 characters).

discord.js counterpart: new SlashCommandBuilder().setName(…).setDescription(…).

Source

pub fn name_localization( self, locale: Locale, name: impl Into<CompactString>, ) -> Self

Sets the name shown to users whose Discord language is locale. It follows the same rules as the name itself.

discord.js counterpart: setNameLocalization.

Source

pub fn name_localizations<N: Into<CompactString>>( self, localizations: impl IntoIterator<Item = (Locale, N)>, ) -> Self

Sets several name translations at once, keeping those set before (a locale given again gets the new name).

discord.js counterpart: setNameLocalizations (which replaces them all).

Source

pub fn description_localization( self, locale: Locale, description: impl Into<CompactString>, ) -> Self

Sets the description shown to users whose Discord language is locale (1-100 characters, like the description).

discord.js counterpart: setDescriptionLocalization.

Source

pub fn description_localizations<D: Into<CompactString>>( self, localizations: impl IntoIterator<Item = (Locale, D)>, ) -> Self

Sets several description translations at once, keeping those set before.

discord.js counterpart: setDescriptionLocalizations (which replaces them all).

Source

pub fn default_member_permissions(self, permissions: Permissions) -> Self

Sets the permissions a member needs to use the command by default; server administrators can change who may use it. Permissions::empty() limits it to administrators. Without this, everyone can use it.

discord.js counterpart: setDefaultMemberPermissions.

Source

pub fn contexts( self, contexts: impl IntoIterator<Item = InteractionContextType>, ) -> Self

Sets where the command can be used: in servers, in DMs with the bot, and in other DMs and group DMs. Without this, Discord allows all of them.

discord.js counterpart: setContexts.

Source

pub fn integration_types( self, integration_types: impl IntoIterator<Item = IntegrationType>, ) -> Self

Sets which installations offer the command: the app installed to a server, to a user, or both. Without this, Discord uses the installation types the app supports.

discord.js counterpart: setIntegrationTypes.

Source

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

Marks the command as age-restricted, so it can only be used in age-restricted channels.

discord.js counterpart: setNSFW.

Source

pub fn option(self, option: impl Into<CommandOption>) -> Self

Adds an option (a parameter users fill in), after those already added: any SlashCommand…Option of this crate, or a [CommandOption] of djs-model.

A command has at most 25 options, with unique names and the required ones first. It either has options or subcommands, not both.

discord.js counterpart: addStringOption, addIntegerOption, … (one per type).

Source

pub fn subcommand(self, subcommand: SlashCommandSubcommandBuilder) -> Self

Adds a subcommand (/command subcommand).

discord.js counterpart: addSubcommand.

Source

pub fn subcommand_group(self, group: SlashCommandSubcommandGroupBuilder) -> Self

Adds a group of subcommands (/command group subcommand).

discord.js counterpart: addSubcommandGroup.

Source

pub fn build(self) -> Result<CreateApplicationCommand, ValidationError>

Checks the command against Discord’s rules (see the type docs) and returns it.

discord.js counterpart: SlashCommandBuilder#toJSON.

§Errors

The first ValidationError found, checking the name, its translations, the description, its translations, then each option in order (with what it contains), and finally the 8000-character total (TooLong with the field command).

Source

pub fn build_unchecked(self) -> CreateApplicationCommand

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

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

Trait Implementations§

Source§

impl Clone for SlashCommandBuilder

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 SlashCommandBuilder

Source§

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

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

impl PartialEq for SlashCommandBuilder

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 SlashCommandBuilder

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.