How jkeevk/aimlul-twitch-bot works
Purpose and deployment
AIMLUL is an asynchronous Twitch channel bot for chat commands, interactive games, persistent player statistics, and Channel Points redemptions. It is deployed as Docker services alongside PostgreSQL, Redis, and a separate FastAPI administration service. Primary bot startup is src/main.py; Compose orchestration is docker-compose.yml.
Bot lifecycle and Twitch boundary
The application initializes configuration, persistence, Redis, Twitch clients, and long-running bot/EventSub processes from src/main.py. Twitch chat/IRC behavior is encapsulated in src/bot/twitch_bot.py, with lifecycle coordination in src/bot/manager.py. Twitch REST calls are isolated in src/api/twitch_api.py, separating external API interaction from gameplay and command logic.
Chat command dispatch
Incoming chat messages flow through the command layer: src/commands/command_handler.py interprets commands, applies permissions/cooldowns, and delegates to game or trigger handlers. Bot command registration/configuration lives in src/bot/commands_config.py; authorization policy is centralized in src/commands/permissions.py. This is the main boundary between untrusted Twitch chat input and state-mutating application behavior.
Game and engagement domain
Game implementations are modularized behind a shared game base in src/commands/games/base_game.py. Core Channel Points/chat experiences include blackjack-like play in src/commands/games/twenty_one.py, beer challenge mechanics in src/commands/games/beer_challenge.py, and mass-timeout/barrel events in src/commands/games/beer_barrel.py. Supporting domain state models are in src/commands/models/game_models.py; temporary coordination and cooldown-like state use src/commands/managers/cache_manager.py.
EventSub redemption pipeline
EventSub is a distinct inbound Twitch integration: src/eventsub/manager.py maintains subscription/event processing, src/eventsub/handlers.py routes received events, and src/eventsub/reward_handlers.py translates Channel Points redemptions into game actions. src/eventsub/ctx_factory.py constructs the execution context needed to invoke command/game logic without a chat-message origin. This pipeline must retain Twitch redemption scopes and completion semantics.
Persistence, cache, and credentials
PostgreSQL access and async ORM setup are centralized in src/db/database.py, with persisted entities in src/db/models.py; schema evolution is managed through migrations/env.py and versioned migrations such as migrations/versions/20260116_add_tickets.py. Redis connectivity is isolated in src/core/redis_client.py. Configuration loading and token lifecycle are separated into src/core/config_loader.py, src/utils/token_manager.py, and src/utils/token_generator.py.
Administration plane
The independent FastAPI admin application starts at admin/app/main.py, exposes authenticated HTTP/WebSocket endpoints through admin/app/routes/api.py, and uses admin/app/security.py for JWT/password security. Operational control is intentionally remote: admin/app/services/ssh_client.py manages Docker hosts over SSH, while admin/app/services/websocket_manager.py streams container logs to authenticated clients. Nginx is the admin-facing proxy boundary at admin/nginx/nginx.conf; its image is defined in admin/Dockerfile.
Main entry points: main.py Bot startup · base_game.py Game framework · twitch_bot.py Chat bot runtime · command_handler.py Command dispatcher
Components 23
Bot runtime
- Bot startup · Initializes runtime dependencies and long-running processes.
src/main.py - Chat bot runtime · Encapsulates chat behavior and lifecycle coordination.
src/bot/twitch_bot.py - Command registration
src/bot/commands_config.py - Twitch REST client
src/api/twitch_api.py
Commands & games
- Command dispatcher · Applies command handling before state-mutating actions.
src/commands/command_handler.py - Permissions & cooldowns
src/commands/permissions.py - Game framework · Shared base for twenty-one, beer challenge, and barrel events.
src/commands/games/base_game.py - Game state models
src/commands/models/game_models.py - Game coordination cache · Holds cooldown and coordination state.
src/commands/managers/cache_manager.py
EventSub pipeline
- EventSub manager
src/eventsub/manager.py - Event router
src/eventsub/handlers.py - Redemption handlers · Maps redemptions to game actions and completion semantics.
src/eventsub/reward_handlers.py - Redemption context · Creates command/game context without chat input.
src/eventsub/ctx_factory.py
State & credentials
- PostgreSQL persistence · Central database setup and persisted entities.
src/db/database.py - Redis client
src/core/redis_client.py - Configuration & tokens · Loads configuration and manages Twitch tokens.
src/core/config_loader.py
Administration plane
- Admin application · Independent operational control service.
admin/app/main.py - Admin API
admin/app/routes/api.py - Admin security
admin/app/security.py - Remote Docker operations · Controls Docker hosts and streams container logs.
admin/app/services/ssh_client.py - Admin proxy
admin/nginx/nginx.conf
Other components
- Compose deployment · Runs bot, admin, PostgreSQL, and Redis services.
docker-compose.yml - Twitch platform · Chat, REST, and Channel Points boundary.
Connections 28
- Compose deployment → Bot startup: runs
- Compose deployment → Admin application: runs
- Bot startup → Chat bot runtime: starts
- Bot startup → EventSub manager: starts
- Bot startup → PostgreSQL persistence: initializes
- Bot startup → Redis client: connects
- Bot startup → Configuration & tokens: loads
- Chat bot runtime → Twitch platform: chat / IRC
- Chat bot runtime → Command registration: registers
- Chat bot runtime → Command dispatcher: chat messages
- Command dispatcher → Permissions & cooldowns: authorizes
- Command dispatcher → Game framework: dispatches
- Game framework → Game state models: uses
- Game framework → Game coordination cache: coordinates
- Game coordination cache → Redis client: stores transient state
- Game framework → PostgreSQL persistence: persists statistics
- EventSub manager → Twitch platform: subscriptions and events
- EventSub manager → Event router: forwards events
- Event router → Redemption handlers: routes redemptions
- Redemption handlers → Redemption context: builds context
- Redemption context → Game framework: invokes actions
- Twitch REST client → Twitch platform: REST calls
- Configuration & tokens → Twitch REST client: supplies tokens
- Admin proxy → Admin application: proxies
- Admin application → Admin API: serves
- Admin API → Admin security: authenticates
- Admin API → Remote Docker operations: operates hosts
- Remote Docker operations → Compose deployment: controls containers