pub trait CommandModel: Sized {
    fn from_interaction(data: CommandInputData<'_>) -> Result<Self, ParseError>;
}
Expand description

Parse command data into a concrete type.

This trait is used to parse received command data into a concrete command model. A derive macro is provided to implement this trait automatically.

Command models

This trait can be implemented on structs representing a slash command model. All type fields must implement the CommandOption trait. A unit struct can be used if the command has no options. See the module documentation for a full list of supported types.

use twilight_interactions::command::{CommandModel, ResolvedUser};

#[derive(CommandModel)]
struct HelloCommand {
    message: String,
    user: Option<ResolvedUser>
}

Validating options

The CommandModel trait only focus on parsing received interaction data and does not directly support additional validation. However, it will ensure that received data matches with the provided model. If you specify a max_value for a field, this requirement will be checked when parsing command data.

Not supporting additional validation is a design choice. This allow to clearly split between validations that are ensured by Discord, and those you perform on top of that. If an error occurs during parsing, it is always a bug, not a user mistake.

If you need to perform additional validation, consider creating another type that can be initialized from the command model.

Autocomplete interactions

Autocomplete interactions are no longer supported since 0.10, as the previous implementation was incorrect. See #9 for more information.

Subcommands and subcommands groups

This trait also support parsing subcommands and subcommands group when implemented on enums with all variants containing types that implement CommandModel. Each variant must have an attribute with the subcommand name.

Subcommand groups works in the same way as regular subcommands, except the variant type is another enum implementing CommandModel.

use twilight_interactions::command::CommandModel;

#[derive(CommandModel)]
enum HelloCommand {
    #[command(name = "user")]
    User(HelloUser),
    #[command(name = "config")]
    Config(HelloConfig)
}

Macro attributes

The macro provide a #[command] attribute to configure generated code.

AttributeTypeLocationDescription
namestrVariant (subcommand)Subcommand name (required).
renamestrFieldUse a different name for the field when parsing.
channel_typesstrFieldRestricts the channel choice to specific types.1
max_value, min_valuei64 or f64FieldMaximum and/or minimum value permitted.

Example

use twilight_interactions::command::CommandModel;

#[derive(CommandModel)]
struct HelloCommand {
    #[command(rename = "text")]
    message: String,
    #[command(max_value = 60)]
    delay: i64
}

  1. List of ChannelType names in snake_case separated by spaces like guild_text private

Required Methods

Construct this type from a CommandInputData.

Implementations on Foreign Types

Implementors