Expand description

Slash command parsing and creation.

Slash commands

This crate provides parsing slash command data as typed structs. It also provides a convenient way to register commands from these structs. Derive macros are provided to automatically implement related traits.

Read the documentation of these traits for usage examples.

Example

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

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

Localization

Localization of names and descriptions of slash commands is supported using the name_localizations and desc_localizations attributes on applicable structs.

The attribute takes a function that returns any type that implements IntoIterator<Item = (ToString, ToString)>, where the first tuple element is a valid locale and the second tuple element is the localized value.

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

#[derive(CommandModel, CreateCommand)]
#[command(name = "hello", desc = "Say hello", desc_localizations = "hello_desc")]
struct HelloCommand;

pub fn hello_desc() -> [(&'static str, &'static str); 2] {
    [("fr", "Dis bonjour"), ("de", "Sag Hallo")]
}

See the documentation of the traits to see where these attributes can be used.

Supported types

The CommandOption and CreateOption traits are implemented for the following types:

Command option typeProvided implementations
STRINGString, Cow
INTEGERi64
NUMBERf64
BOOLEANbool
USERResolvedUser, User, Id<UserMarker>
CHANNELInteractionChannel, Id<ChannelMarker>
ROLERole, Id<RoleMarker>
MENTIONABLEId<GenericMarker>
ATTACHMENTAttachment, Id<AttachmentMarker>

Structs

Enums

Traits

Derive Macros