- Start with
/(e.g.,/help,/poll,/weather) - Show up in autocomplete when users type
/in Towns - Display descriptions to help users understand what they do
- Are handled separately from regular messages (don’t trigger
onMessage)
Example Commands
Creating commands
Commands are defined in a TypeScript file (typicallysrc/commands.ts):
src/commands.ts
Handling Commands
You can usebot.onSlashCommand() to handle each slash command:
src/index.ts
Working with Arguments
Slash commands can receive arguments after the command name. You can get them from theargs property of the event.
Working with Mentions
Users can mention other users or the bot in slash commands. You can get the mentions from thementions property of the event.
Checking Permissions
You may want to check if the user has the necessary permissions to use a command. You can use thehasAdminPermission method to check if the user is an admin.
This is a shortcut for checking if the user has the ModifyBanning permission.
If you want to check for a specific permission, you can use the checkPermission method.
Paid Commands
You can create commands that require USDC payments in any network. Payments are processed through the x402 protocol.Defining Paid Commands
Add apaid property to your command definition with a price in USDC:
src/commands.ts
How it works
When a user invokes a paid command:- Bot sends a signature request for USDC transfer authorization
- User signs the request in their wallet
- Payment is verified and settled via the x402 facilitator
- Your command handler executes after successful payment
src/commands.ts
Updating Commands
Commands are automatically synced with Towns when your bot starts. To update your bot’s slash commands:- Modify your command definitions in
src/commands.ts - Restart your bot
Next Steps
- Learn about handling other events
- Explore message formatting and attachments
- See permission-based features