pub trait CreateCommand: Sized {
    const NAME: &'static str;

    // Required method
    fn create_command() -> ApplicationCommandData;
}
Expand description

Create a slash command from a type.

This trait is used to create commands from command models. A derive macro is provided to automatically implement the traits.

Types and fields documentation

The trait can be derived on structs whose fields implement CreateOption (see the module documentation for a list of supported types) or enums whose variants implement CreateCommand.

Unlike the CommandModel trait, all fields or variants of the type it’s implemented on must have a description. The description corresponds either to the first line of the documentation comment or the value of the desc attribute. The type must also be named with the name attribute.

Example

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

#[derive(CreateCommand)]
#[command(
    name = "hello",
    desc = "Say hello",
    default_permissions = "hello_permissions"
)]
struct HelloCommand {
    /// The message to send.
    message: String,
    /// The user to send the message to.
    user: Option<ResolvedUser>,
}

fn hello_permissions() -> Permissions {
    Permissions::SEND_MESSAGES
}

Macro attributes

The macro provides a #[command] attribute to provide additional information.

AttributeTypeLocationDescription
namestrTypeName of the command (required).
descstrType / Field / VariantDescription of the command (required).
default_permissionsfn1TypeDefault permissions required by members to run the command.
dm_permissionboolTypeWhether the command can be run in DMs.
nsfwboolTypeWhether the command is age-restricted.
renamestrFieldUse a different option name than the field name.
name_localizationsfn2Type / Field / VariantLocalized name of the command (optional).
desc_localizationsfn2Type / Field / VariantLocalized description of the command (optional).
autocompleteboolFieldEnable autocomplete on this field.
channel_typesstrFieldRestricts the channel choice to specific types.3
max_value, min_valuei64 or f64FieldSet the maximum and/or minimum value permitted.
max_length, min_lengthu16FieldMaximum and/or minimum string length permitted.

  1. Path to a function that returns Permissions

  2. Path to a function that returns a type that implements IntoIterator<Item = (ToString, ToString)>. See the module documentation to learn more. 

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

Required Associated Constants§

source

const NAME: &'static str

Name of the command.

Required Methods§

Implementations on Foreign Types§

source§

impl<T: CreateCommand> CreateCommand for Box<T>

Implementors§