Crate serenity_commands

Source
Expand description

A library for creating/parsing serenity slash commands.

§Examples

use serenity::all::{
    async_trait, Client, Context, CreateInteractionResponse, CreateInteractionResponseMessage,
    EventHandler, GatewayIntents, GuildId, Interaction,
};
use serenity_commands::{Command, Commands, SubCommand};

#[derive(Debug, Commands)]
enum AllCommands {
    /// Ping the bot.
    Ping,

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

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

impl AllCommands {
    fn run(self) -> String {
        match self {
            Self::Ping => "Pong!".to_string(),
            Self::Echo { message } => message,
            Self::Math(math) => math.run().to_string(),
        }
    }
}

#[derive(Debug, Command)]
enum MathCommand {
    /// Add two numbers.
    Add(BinaryOperation),

    /// Subtract two numbers.
    Subtract(BinaryOperation),

    /// Multiply two numbers.
    Multiply(BinaryOperation),

    /// Divide two numbers.
    Divide(BinaryOperation),

    /// Negate a number.
    Negate {
        /// The number to negate.
        a: f64,
    },
}

impl MathCommand {
    fn run(self) -> f64 {
        match self {
            Self::Add(BinaryOperation { a, b }) => a + b,
            Self::Subtract(BinaryOperation { a, b }) => a - b,
            Self::Multiply(BinaryOperation { a, b }) => a * b,
            Self::Divide(BinaryOperation { a, b }) => a / b,
            Self::Negate { a } => -a,
        }
    }
}

#[derive(Debug, SubCommand)]
struct BinaryOperation {
    /// The first number.
    a: f64,

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

struct Handler {
    guild_id: GuildId,
}

#[async_trait]
impl EventHandler for Handler {
    async fn ready(&self, ctx: Context, _: serenity::all::Ready) {
        self.guild_id
            .set_commands(&ctx, AllCommands::create_commands())
            .await
            .unwrap();
    }

    async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
        if let Interaction::Command(command) = interaction {
            let command_data = AllCommands::from_command_data(&command.data).unwrap();
            command
                .create_response(
                    ctx,
                    CreateInteractionResponse::Message(
                        CreateInteractionResponseMessage::new().content(command_data.run()),
                    ),
                )
                .await
                .unwrap();
        }
    }
}

Enums§

Error
An error which can occur when extracting data from a command interaction.
PartialOption
A field which may be partially parsed.

Traits§

AutocompleteCommand
A top-level command for use with AutocompleteCommands.
AutocompleteCommands
A utility for extracting data from an autocomplete interaction.
AutocompleteSubCommandOrGroup
A sub-command group which can be nested inside of an AutocompleteCommand and contains AutocompleteSubCommandOrGroups.
BasicOption
A basic option which can be nested inside of Commands or SubCommands.
Command
A top-level command for use with Commands.
Commands
A utility for creating commands and extracting their data from application commands.
SubCommand
A sub-command which can be nested inside of a Command or SubCommandGroup.
SubCommandGroup
A sub-command group which can be nested inside of a Command and contains SubCommands.
SupportsAutocomplete
A trait for identifying types that support autocomplete interactions.

Type Aliases§

Autocomplete
A helper type alias for extracting the autocomplete type from a type that supports autocomplete.
Result
A type alias for std::result::Results which use Error as the error type.

Derive Macros§

BasicOption
Derives BasicOption.
Command
Derives Command.
Commands
Derives Commands.
SubCommand
Derives SubCommand.
SubCommandGroup
Derives SubCommandGroup.