Trait twilight_interactions::command::CreateCommand
source · 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.
| Attribute | Type | Location | Description |
|---|---|---|---|
name | str | Type | Name of the command (required). |
desc | str | Type / Field / Variant | Description of the command (required). |
default_permissions | fn1 | Type | Default permissions required by members to run the command. |
dm_permission | bool | Type | Whether the command can be run in DMs. |
nsfw | bool | Type | Whether the command is age-restricted. |
rename | str | Field | Use a different option name than the field name. |
name_localizations | fn2 | Type / Field / Variant | Localized name of the command (optional). |
desc_localizations | fn2 | Type / Field / Variant | Localized description of the command (optional). |
autocomplete | bool | Field | Enable autocomplete on this field. |
channel_types | str | Field | Restricts the channel choice to specific types.3 |
max_value, min_value | i64 or f64 | Field | Set the maximum and/or minimum value permitted. |
max_length, min_length | u16 | Field | Maximum and/or minimum string length permitted. |
Path to a function that returns
Permissions. ↩Path to a function that returns a type that implements
IntoIterator<Item = (ToString, ToString)>. See the module documentation to learn more. ↩List of
ChannelTypenames in snake_case separated by spaces likeguild_text private. ↩
Required Associated Constants§
Required Methods§
sourcefn create_command() -> ApplicationCommandData
fn create_command() -> ApplicationCommandData
Create an ApplicationCommandData for this type.