Expand description

Slash command parsing and creation.

Slash commands

This crate provide parsing slash command data as typed structs. It also provide 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 description of slash command names is supported using the name_localizations and desc_localizations attributes on applicable structs.

The attribute takes a function that return any type that implement 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 traits documentation 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
NUMBERNumber, f64
BOOLEANbool
USERResolvedUser, User, UserId
CHANNELInteractionChannel, ChannelId
ROLERole, RoleId
MENTIONABLEGenericId
ATTACHMENTAttachment, AttachmentId

Structs

Data sent to discord to create a command.

Data sent by Discord when receiving a command.

A resolved Discord user.

Traits

Parse command data into a concrete type.

Parse command option into a concrete type.

Create a slash command from a type.

Create a command option from a type.

Derive Macros

Derive macro for the the CommandModel trait.

Derive macro for the the CommandOption trait.

Derive macro for the the CreateCommand trait.

Derive macro for the the CreateOption trait.