CommandModel

Trait CommandModel 

Source
pub trait CommandModel: Sized {
    // Required method
    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 focuses 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 allows splitting validations that are ensured by Discord and those you perform on top of them. 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 supported with the #[command(autocomplete = true)] attribute. Only autocomplete command models are able to use the AutocompleteValue type in command fields.

Since autocomplete interactions are partial interactions, models must meet the following requirements:

  • Every field should be an Option<T> or AutocompleteValue<T>, since there is no guarantee that a specific field has been filled before the interaction is submitted.
  • If a field has autocomplete enabled, its type must be AutocompleteValue or the parsing will fail, since focused fields are sent as String.
  • Autocomplete models are partial, which means that unknown fields will not make the parsing fail.
  • It is not possible to derive CreateCommand on autocomplete models.

Autocomplete models are not meant to be used alone: you should use a regular model to handle interactions submit, and another for autocomplete interactions.

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

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

§Subcommands and subcommands groups

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

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

When using subcommands, you should parse and create the command using the top-level command. See the xkcd-bot example for example usage.

use twilight_interactions::command::CommandModel;

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

§Macro attributes

The macro provides 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.
max_length, min_lengthu16FieldMaximum and/or minimum string length 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§

Source

fn from_interaction(data: CommandInputData<'_>) -> Result<Self, ParseError>

Construct this type from CommandInputData.

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.

Implementations on Foreign Types§

Source§

impl CommandModel for Vec<CommandDataOption>

Source§

impl<T: CommandModel> CommandModel for Box<T>

Implementors§