pub trait CommandOption: Sized {
    fn from_option(
        value: CommandOptionValue,
        data: CommandOptionData,
        resolved: Option<&CommandInteractionDataResolved>
    ) -> Result<Self, ParseOptionErrorType>; }
Expand description

Parse command option into a concrete type.

This trait is used by the implementation of CommandModel generated by the derive macro. See the module documentation for a list of implemented types.

Option choices

This trait can be derived on enums to represent command options with predefined choices. The #[option] attribute must be present on each variant.

The corresponding slash command types is automatically inferred from the value attribute. In the example below, the inferred type would be INTEGER.

A value method is also generated for each variant to obtain the value of the variant. This method is not described in the trait as it is only implemented for option choices.

Example

use twilight_interactions::command::CommandOption;

#[derive(CommandOption)]
enum TimeUnit {
    #[option(name = "Minute", value = 60)]
    Minute,
    #[option(name = "Hour", value = 3600)]
    Hour,
    #[option(name = "Day", value = 86400)]
    Day
}

assert_eq!(TimeUnit::Minute.value(), 60);

Macro attributes

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

AttributeTypeLocationDescription
namestrVariantSet the name of the command option choice.
valuestr, i64 or f64VariantValue of the command option choice.

Required Methods

Convert a CommandOptionValue into this value.

Implementations on Foreign Types

Implementors