Command Basics
Faculty Bot uses the Poise framework for command handling. Commands support both slash commands and prefix commands.Creating a Simple Command
Define the Command Function
Create a command function in the appropriate module (user.rs, administration.rs, or moderation.rs):
src/commands/user.rs
Command Attributes
Basic Attributes
Permissions
Localization
Custom Checks
Command Parameters
Simple Parameters
Parameter Attributes
Localized Parameters
Channel Type Filtering
Subcommands
Create command groups with subcommands:- Slash:
/verify init email:[email protected] - Prefix:
>verify init [email protected]
Response Patterns
Simple Text Response
Embed Response
With Components (Buttons)
With File Attachments
Accessing Bot State
Database Access
Configuration Access
Shared State (DashMap)
Error Handling
Returning User-Friendly Errors
Using Translations
Database Error Conversion
Real-World Example: Leaderboard Command
Here’s a complete example from the codebase:src/commands/user.rs
Testing Commands
In Development
- Start the bot:
cargo run - In Discord:
- Slash command:
/your_command - Prefix command:
>your_command
- Slash command:
Register Slash Commands
After adding new slash commands:Best Practices
- Always handle errors: Use
.map_err()to convert toErrorenum - Use defer for slow commands:
ctx.defer_or_broadcast().await?; - Validate input: Check user input before database operations
- Use translations: Support multiple languages with rosetta-i18n
- Add descriptions: Provide clear descriptions for users
- Permission checks: Use
required_permissionsor custom checks - Guild-only when needed: Use
guild_onlyfor server-specific commands - Ephemeral for sensitive data: Use
ephemeralfor private responses
Next Steps
- Learn about Event Handlers
- Understand Internationalization
- Review existing commands in
src/commands/for more examples