!ping or ?help). While Discord encourages slash commands, prefix commands remain useful for certain use cases.
Basic Prefix Command
Create a command file that exports themessage handler:
!, users can run !ping to trigger this command.
Setting a Custom Prefix
UsesetPrefixResolver to configure your bot’s prefix:
src/app.ts
?ping instead of !ping.
Multiple Prefixes
Support multiple prefixes by returning an array:src/app.ts
!ping, ?ping, or >ping.
Guild-Specific Prefixes
Implement per-server prefixes with a database:Command Arguments
Access command arguments usingctx.args():
src/app/commands/say.ts
!say hello world, args will be ['hello', 'world'].
Parsing Arguments
Manually parse complex arguments:src/app/commands/kick.ts
Hybrid Commands
Support both slash and prefix commands in one file:src/app/commands/userinfo.ts
Permissions and Validation
Validate permissions and input:src/app/commands/ban.ts
Help Command Example
Create a help command that lists all prefix commands:src/app/commands/help.ts
Migration Tips
If you’re migrating from prefix to slash commands:Best Practices
- Validate all input: Never trust user input from message content
- Provide usage examples: Help users understand command syntax
- Use middleware: Add permission checks and validation via middleware instead of repeating code
- Consider slash commands: Slash commands provide better UX and type safety
- Cache prefix lookups: Optimize database queries with caching
Related Resources
- Slash Commands - Modern alternative to prefix commands
- Middlewares - Add validation and permission checks
- Cache Plugin - Optimize prefix resolver performance
- Commands - Core command concepts