#slash-command #serenity #creating-parsing

macro serenity-commands-macros

A library for creating/parsing Serenity slash commands

10 releases (4 breaking)

0.6.0 Sep 13, 2024
0.5.1 Aug 31, 2024
0.4.5 Aug 31, 2024
0.4.3 Dec 9, 2023
0.1.0 Dec 4, 2023

#1123 in Procedural macros

Download history 15/week @ 2024-07-13 8/week @ 2024-07-20 40/week @ 2024-07-27 30/week @ 2024-08-03 23/week @ 2024-08-10 11/week @ 2024-08-17 18/week @ 2024-08-24 394/week @ 2024-08-31 62/week @ 2024-09-07 110/week @ 2024-09-14 20/week @ 2024-09-21 66/week @ 2024-09-28 15/week @ 2024-10-05 12/week @ 2024-10-12 21/week @ 2024-10-19 4/week @ 2024-10-26

59 downloads per month
Used in serenity-commands

MIT license

46KB
1K SLoC

Serenity Commands

A library for creating/parsing Serenity slash commands.

Usage

See the examples directory for more 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();
        }
    }
}

Dependencies

~0.6–1.1MB
~25K SLoC