Skip to main content
Slash commands are special message types that:
  • 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 (typically src/commands.ts):
src/commands.ts

Handling Commands

You can use bot.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 the args property of the event.

Working with Mentions

Users can mention other users or the bot in slash commands. You can get the mentions from the mentions 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 the hasAdminPermission 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.
You can create commands that require USDC payments. Payments are processed through the x402 protocol v2, supporting multi-chain payments and reusable sessions.
Note: This bot flow uses a facilitator to verify and settle payments after a user signs an authorization inside Towns. It does not use the standard x402 HTTP middleware headers (PAYMENT-SIGNATURE / PAYMENT-RESPONSE) because Towns bots are not HTTP endpoints.

Defining Paid Commands

Add a paid property to your command definition with a price in USDC:
src/commands.ts

How it works

When a user invokes a paid command:
  1. Bot sends a signature request for USDC transfer authorization
  2. User signs the request in their wallet
  3. Payment is verified and settled via the x402 facilitator
  4. Your command handler executes after successful payment

Configuring the Facilitator

Configure which x402 facilitator the bot will call for verify/settle:
src/index.ts
If you’re using Coinbase/CDP’s hosted facilitator, set:

Sessions (Pay Once, Use Many)

Enable session-based access to reduce friction for users who call your command frequently. With sessions, users pay once and can use the command multiple times within a time window.
src/commands.ts
When a user with an active session invokes the command:
  • The command executes immediately without payment
  • Session usage is tracked and displayed to the user
  • After session expires or uses are exhausted, payment is required again

Multi-Chain Support

Accept payments on multiple blockchain networks. The x402 v2 protocol uses CAIP-2 network identifiers.
src/commands.ts
Supported Networks:
Note: Solana CAIP-2 identifiers are part of the broader x402 v2 ecosystem, but the Towns bot paid-command flow currently supports EVM networks only.

Custom Payment Recipient

By default, payments go to your bot’s app address. You can specify a different recipient:
src/commands.ts

Updating Commands

Commands are automatically synced with Towns when your bot starts. To update your bot’s slash commands:
  1. Modify your command definitions in src/commands.ts
  2. Restart your bot
The new commands will be automatically registered and appear in Towns’ autocomplete.

Next Steps