Derive Macro serenity_commands::CommandOption

source ·
#[derive(CommandOption)]
{
    // Attributes available to this derive:
    #[command]
}
Expand description

Derive the CommandOption trait.

This creates a sub-command or sub-command group which can be nested within other CommandOptions or Commands.

Documentation comments (///) will be used as the options’ descriptions, and are required whenever they are expected.

Examples

structs with named fields

A sub-command with the specified options. Note that none of the fields can be sub-commands or sub-command groups, and the macro will emit an error during compilation if this is the case.

Sets CommandOption::TYPE to SubCommand.

use serenity_commands::CommandOption;

#[derive(CommandOption)]
struct AddNumbers {
    /// The first number.
    first: f64,

    /// The second number, optional.
    second: Option<f64>,
}

Newtype structs

Delegates the implementation to the inner type, which must implement CommandOption.

Sets CommandOption::TYPE to the inner type’s CommandOption::TYPE.

use serenity_commands::CommandOption;

#[derive(CommandOption)]
struct FloatWrapper(f64);

Unit structs

A sub-command with no options.

Sets CommandOption::TYPE to SubCommand.

use serenity_commands::CommandOption;

#[derive(CommandOption)]
struct Ping;

enums

Sets CommandOption::TYPE to SubCommandGroup.

A sub-command group. Note that the macro will emit an error during compilation if any of the variants are not sub-commands or sub-command groups.

The behaviour for each variant type is analagous to that of the corresponding struct type:

  • A variant with named fields is a sub-command with the specified options.
  • A newtype variant is a sub-command/sub-command group which delegates the implementation to the inner type, which must implement CommandOption.
  • A unit variant is a sub-command with no options.
use serenity_commands::CommandOption;

#[derive(CommandOption)]
enum MyCommands {
    /// Ping the bot.
    Ping,

    /// Echo a message.
    Echo {
        /// The message to echo.
        message: String,
    },

    /// Perform math operations.
    Math(MathSubCommand),
}