pub trait Command: ApplicationCommandInteractionHandler + Sized {
// Required methods
fn parse(
command: &ApplicationCommandInteraction,
) -> Result<Self, ParseError>;
fn register(
command: &mut CreateApplicationCommand,
) -> &mut CreateApplicationCommand;
fn name() -> String;
}Expand description
This trait provides the methods needed to parse and register a slash command.
For most use cases, just derive it via the macros crate:
/// Greet a user
#[derive(Debug, Command)]
#[name = "greet"]
struct HelloCommand {
/// The user to greet
user: UserInput,
}To derive the trait, you must provide the following (see the example above):
- Docstrings for the struct and all fields (these will be used for the descriptions of the command and its options)
- The name of the command via the
nameattribute
All fields must implement the parsable::ParsableCommandOption trait - see the docs for the
trait for a list of types supported out of the box.
You may also provide additional attributes to specify more complex behaviours for the command options:
| Attribute | Explanation | Examples | Applicable Discord types |
|---|---|---|---|
| choice | Limits the user’s input to specific choices - use the attribute on the field multiple times, once for each choice. | #[choice("Action")] #[choice("First", 1)] | STRING, INTEGER, NUMBER |
| min | Limits the user’s input to be at least this value. | #[min = 0.0] | INTEGER, NUMBER |
| max | Limits the user’s input to be at most this value. | #[max = 10.0] | INTEGER, NUMBER |
| channel_types | Limits the user’s choice of channels to specific types of channels | #[channel_types(ChannelType::Text, ChannelType::News)] | CHANNEL |
For how to work with subcommands, see the documentation for the SubCommand trait
Required Methods§
Sourcefn parse(command: &ApplicationCommandInteraction) -> Result<Self, ParseError>
fn parse(command: &ApplicationCommandInteraction) -> Result<Self, ParseError>
Try to parse the interaction as this type of command
Sourcefn register(
command: &mut CreateApplicationCommand,
) -> &mut CreateApplicationCommand
fn register( command: &mut CreateApplicationCommand, ) -> &mut CreateApplicationCommand
Register this command so that it can be used
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.