Add documentation for creating new commands

This commit is contained in:
2025-08-10 23:15:05 -03:00
parent 0f0027da27
commit b96474d0fb
+51 -1
View File
@@ -20,9 +20,11 @@
//!
//! // Create a new bot instance with the state and a list of prefixes.
//! let bot: Bot<State> = 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<State> = 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<State> = Bot::new(state, ["p."]);
//! let bot: Bot<State> = Bot::new(state, "p.");
//! let bot: Bot<State> = Bot::new(state, "p.".to_string());
//! let bot: Bot<State> = 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<State> 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;