Command

Trait Command 

Source
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 name attribute

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:

AttributeExplanationExamplesApplicable Discord types
choiceLimits 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
minLimits the user’s input to be at least this value.#[min = 0.0]INTEGER, NUMBER
maxLimits the user’s input to be at most this value.#[max = 10.0]INTEGER, NUMBER
channel_typesLimits 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§

Source

fn parse(command: &ApplicationCommandInteraction) -> Result<Self, ParseError>

Try to parse the interaction as this type of command

Source

fn register( command: &mut CreateApplicationCommand, ) -> &mut CreateApplicationCommand

Register this command so that it can be used

Source

fn name() -> String

The name of the command

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.

Implementors§