Expand description
Slash command parsing and creation.
§Slash commands
This crate provides parsing slash command data as typed structs. It also provides a convenient way to register commands from these structs. Derive macros are provided to automatically implement related traits.
- Command parsing with the
CommandModeltrait. - Command creation with the
CreateCommandtrait. - Support for subcommands and subcommand groups.
- Command option choices with the
CommandOptionandCreateOptiontraits.
Read the documentation of the CommandModel and CreateCommand traits
for more information and the complete list of supported attributes.
§Example
use twilight_interactions::command::{CommandModel, CreateCommand, ResolvedUser};
#[derive(CommandModel, CreateCommand)]
#[command(name = "hello", desc = "Say hello")]
struct HelloCommand {
/// The message to send.
message: String,
/// The user to send the message to.
user: Option<ResolvedUser>,
}For a more complete example, see the examples/xkcd-bot directory in the
library repository.
§Localization
Command names and descriptions can be localized using the
name_localizations and desc_localizations attributes on the command
structs and fields.
-
For command names, you should provide the
nameattribute with the default command name, andname_localizationswith the name of a function that returns aNameLocalizationsstruct. -
For description, you should only provide the
name_localizationsattribute with the name of a function that returns aDescLocalizationsstruct.These structs take a list of tuples, where the first tuple element is a valid Discord locale and the second tuple element is the localized value.
use twilight_interactions::command::{
CommandModel, CreateCommand, ResolvedUser, NameLocalizations, DescLocalizations
};
#[derive(CommandModel, CreateCommand)]
#[command(
name = "hello",
name_localizations = "hello_name",
desc_localizations = "hello_desc"
)]
struct HelloCommand;
pub fn hello_name() -> NameLocalizations {
NameLocalizations::new([("fr", "bonjour"), ("de", "hallo")])
}
pub fn hello_desc() -> DescLocalizations {
DescLocalizations::new("Say hello", [("fr", "Dis bonjour"), ("de", "Sag Hallo")])
}§Supported types
The CommandOption and CreateOption traits are implemented for the
following types:
| Command option type | Provided implementations |
|---|---|
STRING | String, Cow |
INTEGER | i64 |
NUMBER | f64 |
BOOLEAN | bool |
USER | ResolvedUser, User, Id<UserMarker> |
CHANNEL | InteractionChannel, Id<ChannelMarker> |
ROLE | Role, Id<RoleMarker> |
MENTIONABLE | ResolvedMentionable, Id<GenericMarker> |
ATTACHMENT | Attachment, Id<AttachmentMarker> |
Option choices are supported for the STRING, INTEGER and NUMBER option
types. See the CommandOption and CreateOption traits documentation
for more information.
Structs§
- Application
Command Data - Data sent to Discord to create a command.
- Command
Input Data - Data sent by Discord when receiving a command.
- Desc
Localizations - Localization data for command descriptions.
- Name
Localizations - Localization data for command names.
- Resolved
User - A resolved Discord user.
Enums§
- Autocomplete
Value - An autocomplete command field.
- Resolved
Mentionable - A resolved mentionable.
Traits§
- Command
Model - Parse command data into a concrete type.
- Command
Option - Parse command option into a concrete type.
- Create
Command - Create a slash command from a type.
- Create
Option - Create a command option from a type.
Derive Macros§
- Command
Model derive - Derive macro for the
CommandModeltrait. - Command
Option derive - Derive macro for the
CommandOptiontrait. - Create
Command derive - Derive macro for the
CreateCommandtrait. - Create
Option derive - Derive macro for the
CreateOptiontrait.