A chess bot that enables users to play chess matches with wagers, allows spectators to place bets, and automatically distributes winnings.
- ๐ฎ Play Chess: Start chess games with wagered ETH
- ๐ฐ Wager System: Players tip to join games and wager ETH
- ๐ Spectator Betting: Spectators can bet on game outcomes
- ๐ผ๏ธ Visual Boards: Automatically generates chess board images
- โก Real-time Updates: Game state updates after each move
- ๐ Auto Payouts: Automatic distribution of winnings (TODO: implement payment distribution)
This bot allows users in Towns Protocol channels to:
- Start Games: Use
/startchess [amount]to create a new game with a wager - Join Games: Tip the game message to accept the challenge and match the wager
- Make Moves: Use
/move [move]to play chess (e.g.,/move e2e4) - Resign: Use
/resignto forfeit your game - Spectate & Bet: Spectators can tip to bet on players
- Node.js 18+ or Bun runtime
- Towns Developer Portal access at https://app.towns.com/developer
- Bot credentials (
APP_PRIVATE_DATAandJWT_SECRET)
- Install dependencies:
bun install
# or
npm install- Copy
.env.sampleto.envand fill in your credentials:
cp .env.sample .env- Add environment variables:
APP_PRIVATE_DATA=your_bot_private_data_here
JWT_SECRET=your_jwt_secret_here
MIN_WAGER=0.01 # Optional: minimum wager amount in ETH- Run the bot:
bun run dev # Development with hot reload
# or
bun run start # ProductionStart a new chess game with a wager amount.
Example:
/startchess 0.01
This creates a game with a 0.01 ETH wager. The game creator must tip the game message to pay their wager, then another player can tip to accept the challenge.
Make a chess move using standard algebraic notation.
Examples:
/move e2e4 # Pawn from e2 to e4
/move Nf3 # Knight to f3
/move O-O # Kingside castling
/move O-O-O # Queenside castling
Resign from your current game. The opponent automatically wins.
Show all available commands and how to play.
- Start a Game: Use
/startchess 0.01to create a game with a 0.01 ETH wager - Pay Your Wager: Tip the game message with the wager amount
- Wait for Opponent: Another player tips the message to accept
- Play Chess: Make moves with
/move [notation] - Win or Lose: Game ends when checkmate, stalemate, or resignation occurs
Spectators can tip the game message to bet on either player:
- Tips are automatically assigned to players (alternating)
- Winners receive proportional payouts (TODO: implement payout distribution)
- Game Creation: Player uses
/startchesscommand - Wager Payment: Player 1 tips the game message to pay wager
- Challenge Acceptance: Player 2 tips to match wager and join
- Gameplay: Players alternate moves with
/movecommand - Game End: Checkmate, draw, or resignation
- Payouts: Winnings distributed automatically (TODO: implement)
- GameManager (
src/chess/GameManager.ts): Manages game state and moves - BoardRenderer (
src/chess/BoardRenderer.ts): Generates chess board images - Types (
src/chess/types.ts): TypeScript type definitions - Main Handler (
src/index.ts): Bot event handlers and webhook logic
chess.js: Chess logic and move validationcanvas: Chess board image generation@towns-protocol/bot: Towns Protocol bot frameworkviem: Ethereum utilities for handling ETH amounts
- Push code to GitHub
- Create new Web Service on Render
- Connect GitHub repository
- Set environment variables:
APP_PRIVATE_DATAJWT_SECRETMIN_WAGER(optional)
- Set build command:
bun install - Set start command:
bun run start - Deploy!
After deployment, configure your webhook URL in the Towns Developer Portal:
- Go to https://app.towns.com/developer
- Find your bot and click "Edit"
- Set webhook URL:
https://your-bot.onrender.com/webhook - Select message forwarding: ALL_MESSAGES or MENTIONS_REPLIES_REACTIONS
- Save changes
chess-bot/
โโโ src/
โ โโโ index.ts # Main bot handler
โ โโโ commands.ts # Slash command definitions
โ โโโ chess/
โ โโโ types.ts # TypeScript types
โ โโโ GameManager.ts # Game state management
โ โโโ BoardRenderer.ts # Board image generation
โโโ .env # Environment variables
โโโ package.json # Dependencies
โโโ README.md # This file
To extend the bot:
- Payment Distribution: Implement payout logic in
src/index.ts(marked as TODO) - Database: Add persistent storage for games (SQLite, PostgreSQL, etc.)
- Ratings: Implement ELO rating system
- Time Controls: Add timer for blitz/rapid games
- Tournaments: Multi-player tournament brackets
- Check Render logs for errors
- Verify
APP_PRIVATE_DATAandJWT_SECRETare correct - Ensure webhook URL ends with
/webhook - Check bot permissions in Developer Portal
- Verify message forwarding is set correctly
- Ensure player tips the exact wager amount (within 1% tolerance)
- Check that game status is correct (
waiting_for_wagerโwaiting_for_opponent) - Verify bot has permission to receive tips
- Use standard chess notation (e.g.,
e2e4,Nf3,O-O) - Moves must be legal according to chess rules
- Check that it's your turn before making a move
- Towns Academy: https://www.towns.com/academy/vibebot
- Developer Hub: https://www.towns.com/developer-hub
- Towns Documentation: https://docs.towns.com
- Chess.js: https://github.com/jhlywa/chess.js
- Base Network: https://base.org
MIT License - Feel free to use and modify for your own Towns bots!
Built following Towns Protocol bot development framework and official guidelines.