- 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.
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