pub trait CommandOption: Sized {
    // Required method
    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 are 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 provides an #[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§

source

fn from_option( value: CommandOptionValue, data: CommandOptionData, resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

Convert a CommandOptionValue into this value.

Implementations on Foreign Types§

source§

impl CommandOption for Attachment

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for i64

source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for User

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for Id<AttachmentMarker>

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for f64

source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for Id<UserMarker>

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for CommandOptionValue

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for bool

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for String

source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for Id<GenericMarker>

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for Id<ChannelMarker>

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for Role

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl<'a> CommandOption for Cow<'a, str>

source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for InteractionChannel

source§

fn from_option( value: CommandOptionValue, data: CommandOptionData, resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

source§

impl CommandOption for Id<RoleMarker>

source§

fn from_option( value: CommandOptionValue, _data: CommandOptionData, _resolved: Option<&CommandInteractionDataResolved> ) -> Result<Self, ParseOptionErrorType>

Implementors§