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):
| Part | Rule |
|---|---|
| names (command, subcommands, options, translations) | 1-32 lower-case letters, numbers, -, _, ʼ |
| descriptions (and translations) | 1-100 characters, not blank |
| options per level | at most 25, unique names, required ones first |
| the command’s own options | all subcommands / groups, or none |
| a group’s options | subcommands only |
| a subcommand’s options | no subcommands or groups |
| choices | at most 25; names 1-100 characters; string values at most 100 |
min_length / max_length | 0-6000 / 1-6000, max_length ≥ min_length |
| integers / numbers | within ±(2^53 − 1) / ±2^53; max_value ≥ min_value |
| choices and autocomplete | not both |
| the whole command | at 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
impl SlashCommandBuilder
Sourcepub fn new(
name: impl Into<CompactString>,
description: impl Into<CompactString>,
) -> Self
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(…).
Sourcepub fn name_localization(
self,
locale: Locale,
name: impl Into<CompactString>,
) -> Self
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.
Sourcepub fn name_localizations<N: Into<CompactString>>(
self,
localizations: impl IntoIterator<Item = (Locale, N)>,
) -> Self
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).
Sourcepub fn description_localization(
self,
locale: Locale,
description: impl Into<CompactString>,
) -> Self
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.
Sourcepub fn description_localizations<D: Into<CompactString>>(
self,
localizations: impl IntoIterator<Item = (Locale, D)>,
) -> Self
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).
Sourcepub fn default_member_permissions(self, permissions: Permissions) -> Self
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.
Sourcepub fn contexts(
self,
contexts: impl IntoIterator<Item = InteractionContextType>,
) -> Self
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.
Sourcepub fn integration_types(
self,
integration_types: impl IntoIterator<Item = IntegrationType>,
) -> Self
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.
Sourcepub fn nsfw(self, nsfw: bool) -> Self
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.
Sourcepub fn option(self, option: impl Into<CommandOption>) -> Self
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).
Sourcepub fn subcommand(self, subcommand: SlashCommandSubcommandBuilder) -> Self
pub fn subcommand(self, subcommand: SlashCommandSubcommandBuilder) -> Self
Adds a subcommand (/command subcommand).
discord.js counterpart: addSubcommand.
Sourcepub fn subcommand_group(self, group: SlashCommandSubcommandGroupBuilder) -> Self
pub fn subcommand_group(self, group: SlashCommandSubcommandGroupBuilder) -> Self
Adds a group of subcommands (/command group subcommand).
discord.js counterpart: addSubcommandGroup.
Sourcepub fn build(self) -> Result<CreateApplicationCommand, ValidationError>
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).
Sourcepub fn build_unchecked(self) -> CreateApplicationCommand
pub fn build_unchecked(self) -> CreateApplicationCommand
Returns the command without any check (D-180).
discord.js counterpart: SlashCommandBuilder#toJSON with validation off (disableValidators).