From b96474d0fbeeb0e3db1b543152e13369e2c96e9f Mon Sep 17 00:00:00 2001 From: Rafael Bradley Date: Sun, 10 Aug 2025 23:15:05 -0300 Subject: [PATCH] Add documentation for creating new commands --- src/framework/mod.rs | 52 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/framework/mod.rs b/src/framework/mod.rs index 5fb1f19..8d4d578 100644 --- a/src/framework/mod.rs +++ b/src/framework/mod.rs @@ -20,9 +20,11 @@ //! //! // Create a new bot instance with the state and a list of prefixes. //! let bot: Bot = Bot::new(state, ["p."]); +//! +//! bot.run(&token).await.unwrap(); //! ``` //! -//! ## Prefixes +//! ### Prefixes //! //! The framework supports multiple prefixes per command and allows you to set them dynamically //! based on server and user IDs. @@ -40,6 +42,54 @@ //! //! let bot: Bot = Bot::new(state, get_prefixes); //! ``` +//! +//! Otherwise, you can just pass a static list of prefixes as shown in the previous example. +//! +//! ``` +//! // All of these are valid ways to create a bot with a prefix. +//! let bot: Bot = Bot::new(state, ["p."]); +//! let bot: Bot = Bot::new(state, "p."); +//! let bot: Bot = Bot::new(state, "p.".to_string()); +//! let bot: Bot = Bot::new(state, ["p.".to_string()]); +//! ``` +//! +//! Setting no prefix will make the bot raise errors when receiving messages. +//! +//! ## Adding Commands +//! +//! Creating a command is quite simple. You just need to implement the `Command` trait and register +//! it with the bot. +//! +//! ``` +//! use crate::framework::commands::{Command, CommandError, context::CommandContext}; +//! +//! pub struct MyCommand; +//! +//! #[async_trait::async_trait] +//! impl Command for MyCommand { +//! fn name(&self) -> &'static str { +//! "my_command" +//! } +//! +//! async fn execute(&self, ctx: CommandContext<'_, T>, _args: &str) -> Result<(), CommandError> { +//! // Your command logic here. +//! ctx.send().content("Hello, world!").await.map_err(|_| CommandError::MessageSendingFailed)?; +//! +//! Ok(()) +//! } +//! } +//! ``` +//! +//! Then you just need to register the command with the bot: +//! +//! ``` +//! let bot = Bot::new(state, ["p."]).command(MyCommand); +//! +//! bot.run(&token).await.unwrap(); +//! ``` +//! +//! Log into your Discord account and invite the bot to your server. Then you can test the command by sending +//! `p.my_command` in a channel the bot has access to. pub mod bot; pub mod commands;