[][src]Attribute Macro command_attr::command

#[command]

The heart of the attribute-based framework.

This is a function attribute macro; if you attempt to use this on other Rust constructs, it won't work.

Options

To alter how the framework will interpret the command, you can provide options as attributes following this #[command] macro.

Each option has its own kind of data to stock and manipulate with. They're given to the option either with the #[option(...)] or #[option = ...] syntaxes. If an option doesn't require for any data to be supplied, then it's simply #[option].

If the input to the option is malformed, the macro will give you can error, describing the correct method for passing data, and what it should be.

The list of available options, is, as follows:

  • #[checks(idents)] Preconditions that must be met. Executed before the command's execution. idents is a list of identifiers, seperated by a comma, referencing functions of the declaration: fn(&mut Context, &Message, &mut Args, &CommandOptions) -> bool

  • #[aliases(names)] A list of other names that can be used to execute this command. In serenity::framework::standard::CommandOptions, these are put in the names field, right after the command's name.

  • #[description(desc)]/#[description = desc] A summary of the command.

  • #[usage(usg)]/`#[usage = usg] Usage schema of the command.

  • #[example(ex)]/`#[example = ex] Example of the command's usage.

  • #[min_args(min)], #[max_args(max)], #[num_args(min_and_max)] The minimum and/or maximum amount of arguments that the command should/can receive.

num_args is a helper attribute, serving as a shorthand for calling min_args and max_args with the same amount of arguments.

  • #[allowed_roles(roles)] A list of strings (role names), seperated by a comma, stating that only members of certain roles can execute this command.

  • #[help_available]/#[help_available(bool)] Whether this command should be displayed in the help message.

  • #[only_in(context)] Which context the command can only be executed in.

context can be of "guilds" or "dms" (direct messages).

  • #[owners_only]/#[owners_only(bool)] Whether this command is exclusive to owners.

  • #[owner_privilege]/`#[owner_privilege] Whether this command has a privilege for owners (i.e certain options are ignored for them).

  • #[sub(commands)] A list of command names, separated by a comma, stating the subcommands of this command. These are executed in the form: this-command sub-command

Notes

The name of the command is parsed from the applied function, or can be passed inside the #[command] attribute, a lá #[command(foobar)].

This macro attribute generates static instances of Command and CommandOptions, conserving the provided options.

The names of the instances are all uppercased names of the command name. For example, with a name of "foo":

This example is not tested
pub static FOO_COMMAND_OPTIONS: CommandOptions = CommandOptions { ... };
pub static FOO_COMMAND: Command = Command { options: FOO_COMMAND_OPTIONS, ... };