Module twilight_interactions::command
source · [−]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.
- Command parsing with the
CommandModel
trait. - Command creation with the
CreateCommand
trait. - Support for subcommands and subcommand groups.
- Command option choices with the
CommandOption
andCreateOption
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 type | Provided implementations |
---|---|
STRING | String , Cow |
INTEGER | i64 |
NUMBER | Number , f64 |
BOOLEAN | bool |
USER | ResolvedUser , User , UserId |
CHANNEL | InteractionChannel , ChannelId |
ROLE | Role , RoleId |
MENTIONABLE | GenericId |
ATTACHMENT | Attachment , 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
Derive macro for the the CommandModel
trait.
derive
Derive macro for the the CommandOption
trait.
derive
Derive macro for the the CreateCommand
trait.
derive
Derive macro for the the CreateOption
trait.