Trait twilight_interactions::command::CommandModel
source · [−]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.
Attribute | Type | Location | Description |
---|---|---|---|
name | str | Variant (subcommand) | Subcommand name (required). |
rename | str | Field | Use a different name for the field when parsing. |
channel_types | str | Field | Restricts the channel choice to specific types.1 |
max_value , min_value | i64 or f64 | Field | Maximum 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
}
List of
ChannelType
names in snake_case separated by spaces likeguild_text private
. ↩
Required Methods
fn from_interaction(data: CommandInputData<'_>) -> Result<Self, ParseError>
fn from_interaction(data: CommandInputData<'_>) -> Result<Self, ParseError>
Construct this type from a CommandInputData
.