A production-grade Telegram Mafia party game bot with clean OOP architecture, smart AI players, full role system, SQLite stats, and polished inline UI.
| Feature | Details |
|---|---|
| Roles | Civilian, Mafia, Doctor, Detective, Sniper (1-shot), Maniac (solo win) |
| State Machine | LOBBY → NIGHT → DAY → VOTING → (loop) → ENDED |
| Smart AI | Weighted heuristics, not random — Mafia targets power roles, Doctor protects most-voted, Sniper holds shot for confirmed Mafia |
| Anti-spam voting | One vote per player, changeable; tie = no elimination |
| SQLite stats | Game history, win rates, role breakdown, leaderboard |
| Multi-chat | One GameManager per chat, fully isolated |
| Auto cleanup | Ended games purged every 5 minutes |
| Error handling | Graceful fallback everywhere, zero crash on invalid input |
| Modern UI | Inline keyboards for all interactions |
- Message @BotFather on Telegram
/newbot→ follow prompts- Copy the token
pip install -r requirements.txtBOT_TOKEN=your_token_here python bot.pyOr create a .env file and use python-dotenv:
BOT_TOKEN=123456:ABC...
mafia_bot/
├── bot.py # Entry point, Telegram handlers, routing
├── game_manager.py # State machine: GameManager + GameRegistry
├── role_engine.py # Pure logic: night resolution, win conditions
├── ai_engine.py # Smart AI decision-making (weighted heuristics)
├── models.py # Data classes: Player, Role, Phase, NightResult
├── keyboards.py # All InlineKeyboard builders
├── database.py # Async SQLite (game history, player stats)
└── requirements.txt
bot.py (Telegram I/O)
│
├── GameRegistry (manages all active games)
│ └── GameManager (state machine per chat)
│ ├── RoleEngine (pure night/win logic)
│ └── AIEngine (smart AI decisions)
│
├── Database (async SQLite)
└── keyboards.py (UI builders)
- Add bot to a group chat
/newgame— creates lobby- Players tap Join Game or type
/join - Host can Add AI Player to fill slots
- Host taps Start Game (or
/startgame) when 4+ players ready - Each player receives their secret role via DM
- Night: Special roles use DM inline buttons to act
- Day: Discuss in group chat
- Vote: Tap vote buttons to eliminate a suspect
- Repeat until win condition!
| Role | Team | Night Action |
|---|---|---|
| 👤 Civilian | Town | None |
| 🔫 Mafia | Mafia | Kill 1 player (majority vote) |
| 💊 Doctor | Town | Protect 1 player from death |
| 🔍 Detective | Town | Learn 1 player's true role |
| 🎯 Sniper | Town | ONE unblockable kill (use wisely!) |
| 🔪 Maniac | Solo | Kill 1 player — win by surviving alone |
- 🏙️ Town wins: All Mafia and Maniac are eliminated
- 🔫 Mafia wins: Mafia count ≥ remaining Town + Maniac
- 🔪 Maniac wins: Last player standing
| Command | Description |
|---|---|
/newgame |
Create a lobby (group chat) |
/join |
Join the lobby |
/leave |
Leave the lobby |
/startgame |
Start the game (host only) |
/endgame |
Force-end (host/admin) |
/players |
Show player list and status |
/myrole |
DM reminder of your role |
/stats |
View statistics & leaderboard |
/help |
Full help text |
Edit constants in game_manager.py:
NIGHT_ACTION_TIMEOUT = 45 # seconds for night phase
DAY_SPEECH_TIMEOUT = 60 # seconds for discussion
VOTING_TIMEOUT = 40 # seconds for votingSQLite file: mafia_bot.db (auto-created on first run)
Tables:
games— full game records with player snapshotplayer_stats— per-user win rates, role history
# /etc/systemd/system/mafia_bot.service
[Unit]
Description=Ultra Pro Mafia Bot
After=network.target
[Service]
WorkingDirectory=/opt/mafia_bot
Environment=BOT_TOKEN=your_token_here
ExecStart=/usr/bin/python3 bot.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.targetsystemctl enable --now mafia_bot
journalctl -u mafia_bot -f