Mafia game bot on XMTP with inline action buttons in group chats with time-based phases.
This project is a Mafia (social deduction) game built as an XMTP agent that runs entirely within XMTP chat groups. Players can create game lobbies, join via inline action buttons, complete tasks, attempt kills, and vote to eliminate suspects - all through XMTP messaging. The game features time-based phases that automatically advance, role-based gameplay with random Mafia/Town assignments, and interactive buttons for voting and killing. Tasks are distributed in group chats with player address mentions, while kill commands are handled privately via DMs. The game supports up to 6 players per game with configurable parameters for rounds, phase durations, and game mechanics. All game state is managed server-side with automatic round progression, ensuring a smooth multiplayer experience entirely within the XMTP ecosystem.
Built using XMTP Agent SDK with TypeScript for type safety. The architecture follows a modular pattern: agent setup handles codec registration (ActionsCodec, IntentCodec for XIP-67 inline actions), middleware processes all messages with intent detection fallbacks, and handlers manage game logic. Custom codecs enable interactive buttons for join/vote/kill actions. Game state is managed in-memory via GameManager class with better-sqlite3 for XMTP client persistence. Notable hacks: dual intent detection (contentType check + object structure fallback) due to SDK inconsistencies, direct client access via (agent as any).client for sending ActionsContent with ContentTypeActions, and a custom timer system tracking phase deadlines in game state. The kill command bypasses mention requirements in DMs via middleware early-return, while group commands require @mafia mentions. Player addresses are resolved from conversation members or preferences API, enabling address-based targeting. All phases are time-based with automatic advancement, using setTimeout with cleanup on game end.

