Module twilight_interactions::command
source · 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 these traits for usage examples.
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>,
}Localization
Localization of names and descriptions of slash commands is supported
using the name_localizations and desc_localizations attributes on
applicable structs.
The attribute takes a function that returns any type that implements
IntoIterator<Item = (ToString, ToString)>, where the first tuple element
is a valid locale
and the second tuple element is the localized value.
use twilight_interactions::command::{CommandModel, CreateCommand, ResolvedUser};
#[derive(CommandModel, CreateCommand)]
#[command(name = "hello", desc = "Say hello", desc_localizations = "hello_desc")]
struct HelloCommand;
pub fn hello_desc() -> [(&'static str, &'static str); 2] {
[("fr", "Dis bonjour"), ("de", "Sag Hallo")]
}See the documentation of the traits to see where these attributes can be used.
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> |
Structs
- Data sent to Discord to create a command.
- Data sent by Discord when receiving a command.
- A resolved Discord user.
Enums
- An autocomplete command field.
- A resolved mentionable.
Traits
- Parse command data into a concrete type.
- Parse command option into a concrete type.
- Create a slash command from a type.
- Create a command option from a type.
Derive Macros
- CommandModel
deriveDerive macro for theCommandModeltrait. - CommandOption
deriveDerive macro for theCommandOptiontrait. - CreateCommand
deriveDerive macro for theCreateCommandtrait. - CreateOption
deriveDerive macro for theCreateOptiontrait.