Derive Macro serenity_commands::Command

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

Derive the Command trait.

This creates a top-level command for use with CommandData. The command may contain regular options, sub-commands, and sub-command groups.

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

Examples

structs with named fields

A 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.

use serenity_commands::Command;

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

    /// The second number.
    second: f64,
}

Newtype structs

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

use serenity_commands::Command;

#[derive(Command)]
struct CommandWrapper(InnerCommand);

Unit structs

A command with no options.

use serenity_commands::Command;

#[derive(Command)]
struct Ping;

enums

A command with sub-commands. 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::Command;

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

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

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