Module command

Module command 

Source
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 the CommandModel and CreateCommand traits for more information and the complete list of supported attributes.

§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>,
}

For a more complete example, see the examples/xkcd-bot directory in the library repository.

§Localization

Command names and descriptions can be localized using the name_localizations and desc_localizations attributes on the command structs and fields.

  • For command names, you should provide the name attribute with the default command name, and name_localizations with the name of a function that returns a NameLocalizations struct.

  • For description, you should only provide the name_localizations attribute with the name of a function that returns a DescLocalizations struct.

    These structs take a list of tuples, where the first tuple element is a valid Discord locale and the second tuple element is the localized value.

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

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

pub fn hello_name() -> NameLocalizations {
    NameLocalizations::new([("fr", "bonjour"), ("de", "hallo")])
}

pub fn hello_desc() -> DescLocalizations {
    DescLocalizations::new("Say hello", [("fr", "Dis bonjour"), ("de", "Sag Hallo")])
}

§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>
MENTIONABLEResolvedMentionable, Id<GenericMarker>
ATTACHMENTAttachment, Id<AttachmentMarker>

Option choices are supported for the STRING, INTEGER and NUMBER option types. See the CommandOption and CreateOption traits documentation for more information.

Structs§

ApplicationCommandData
Data sent to Discord to create a command.
CommandInputData
Data sent by Discord when receiving a command.
DescLocalizations
Localization data for command descriptions.
NameLocalizations
Localization data for command names.
ResolvedUser
A resolved Discord user.

Enums§

AutocompleteValue
An autocomplete command field.
ResolvedMentionable
A resolved mentionable.

Traits§

CommandModel
Parse command data into a concrete type.
CommandOption
Parse command option into a concrete type.
CreateCommand
Create a slash command from a type.
CreateOption
Create a command option from a type.

Derive Macros§

CommandModelderive
Derive macro for the CommandModel trait.
CommandOptionderive
Derive macro for the CommandOption trait.
CreateCommandderive
Derive macro for the CreateCommand trait.
CreateOptionderive
Derive macro for the CreateOption trait.