# jkeevk/aimlul-twitch-bot architecture

> Architecture diagram of the GitHub repository jkeevk/aimlul-twitch-bot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jkeevk/aimlul-twitch-bot
- Repository: https://github.com/jkeevk/aimlul-twitch-bot

## Overview

#### 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: [`src/main.py`](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/main.py) (Bot startup), [`src/commands/games/base_game.py`](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/games/base_game.py) (Game framework), [`src/bot/twitch_bot.py`](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/bot/twitch_bot.py) (Chat bot runtime), [`src/commands/command_handler.py`](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/command_handler.py) (Command dispatcher)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_bot["Bot runtime"]
  node_main["Bot startup<br/>async application<br/>[main.py]"]
  node_bot_runtime["Chat bot runtime<br/>Twitch IRC client<br/>[twitch_bot.py]"]
  node_command_config["Command registration<br/>bot configuration<br/>[commands_config.py]"]
  node_twitch_api["Twitch REST client<br/>external API adapter<br/>[twitch_api.py]"]
end

subgraph group_engagement["Commands &amp; games"]
  node_command_handler["Command dispatcher<br/>chat input boundary<br/>[command_handler.py]"]
  node_permissions["Permissions &amp; cooldowns<br/>authorization policy<br/>[permissions.py]"]
  node_games["Game framework<br/>game domain<br/>[base_game.py]"]
  node_game_models["Game state models<br/>domain models<br/>[game_models.py]"]
  node_cache_manager["Game coordination cache<br/>temporary state<br/>[cache_manager.py]"]
end

subgraph group_eventsub["EventSub pipeline"]
  node_eventsub_manager["EventSub manager<br/>subscription processor<br/>[manager.py]"]
  node_event_handlers["Event router<br/>EventSub handlers<br/>[handlers.py]"]
  node_reward_handlers["Redemption handlers<br/>Channel Points adapter<br/>[reward_handlers.py]"]
  node_event_context["Redemption context<br/>execution context factory<br/>[ctx_factory.py]"]
end

subgraph group_data["State &amp; credentials"]
  node_database[("PostgreSQL persistence<br/>async ORM access<br/>[database.py]")]
  node_redis[("Redis client<br/>cache connectivity<br/>[redis_client.py]")]
  node_config["Configuration &amp; tokens<br/>credential lifecycle<br/>[config_loader.py]"]
end

subgraph group_admin["Administration plane"]
  node_admin_app["Admin application<br/>FastAPI service<br/>[main.py]"]
  node_admin_api["Admin API<br/>HTTP/WebSocket routes<br/>[api.py]"]
  node_admin_security["Admin security<br/>JWT/password security<br/>[security.py]"]
  node_remote_ops["Remote Docker operations<br/>SSH and log streaming<br/>[ssh_client.py]"]
  node_nginx["Admin proxy<br/>Nginx boundary<br/>[nginx.conf]"]
end

node_compose{{"Compose deployment<br/>orchestration<br/>[docker-compose.yml]"}}
node_twitch(("Twitch platform<br/>external service"))

node_compose -->|"runs"| node_main
node_compose -->|"runs"| node_admin_app
node_main -->|"starts"| node_bot_runtime
node_main -->|"starts"| node_eventsub_manager
node_main -->|"initializes"| node_database
node_main -->|"connects"| node_redis
node_main -->|"loads"| node_config
node_bot_runtime -->|"chat / IRC"| node_twitch
node_bot_runtime -->|"registers"| node_command_config
node_bot_runtime -->|"chat messages"| node_command_handler
node_command_handler -->|"authorizes"| node_permissions
node_command_handler -->|"dispatches"| node_games
node_games -->|"uses"| node_game_models
node_games -->|"coordinates"| node_cache_manager
node_cache_manager -->|"stores transient state"| node_redis
node_games -->|"persists statistics"| node_database
node_eventsub_manager -->|"subscriptions and events"| node_twitch
node_eventsub_manager -->|"forwards events"| node_event_handlers
node_event_handlers -->|"routes redemptions"| node_reward_handlers
node_reward_handlers -->|"builds context"| node_event_context
node_event_context -->|"invokes actions"| node_games
node_twitch_api -->|"REST calls"| node_twitch
node_config -->|"supplies tokens"| node_twitch_api
node_nginx -->|"proxies"| node_admin_app
node_admin_app -->|"serves"| node_admin_api
node_admin_api -->|"authenticates"| node_admin_security
node_admin_api -->|"operates hosts"| node_remote_ops
node_remote_ops -.->|"controls containers"| node_compose

click node_compose "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/docker-compose.yml"
click node_main "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/main.py"
click node_bot_runtime "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/bot/twitch_bot.py"
click node_command_config "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/bot/commands_config.py"
click node_twitch_api "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/api/twitch_api.py"
click node_command_handler "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/command_handler.py"
click node_permissions "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/permissions.py"
click node_games "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/games/base_game.py"
click node_game_models "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/models/game_models.py"
click node_cache_manager "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/managers/cache_manager.py"
click node_eventsub_manager "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/eventsub/manager.py"
click node_event_handlers "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/eventsub/handlers.py"
click node_reward_handlers "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/eventsub/reward_handlers.py"
click node_event_context "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/eventsub/ctx_factory.py"
click node_database "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/db/database.py"
click node_redis "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/core/redis_client.py"
click node_config "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/core/config_loader.py"
click node_admin_app "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/app/main.py"
click node_admin_api "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/app/routes/api.py"
click node_admin_security "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/app/security.py"
click node_remote_ops "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/app/services/ssh_client.py"
click node_nginx "https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/nginx/nginx.conf"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_bot_runtime,node_command_config,node_twitch_api toneBlue
class node_command_handler,node_permissions,node_games,node_game_models,node_cache_manager toneAmber
class node_eventsub_manager,node_event_handlers,node_reward_handlers,node_event_context toneMint
class node_database,node_redis,node_config toneRose
class node_admin_app,node_admin_api,node_admin_security,node_remote_ops,node_nginx toneIndigo
class node_compose,node_twitch toneNeutral
```

## Components

### Bot runtime

- [Bot startup](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/main.py): async application · Initializes runtime dependencies and long-running processes. · `src/main.py`
- [Chat bot runtime](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/bot/twitch_bot.py): Twitch IRC client · Encapsulates chat behavior and lifecycle coordination. · `src/bot/twitch_bot.py`
- [Command registration](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/bot/commands_config.py): bot configuration · `src/bot/commands_config.py`
- [Twitch REST client](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/api/twitch_api.py): external API adapter · `src/api/twitch_api.py`

### Commands & games

- [Command dispatcher](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/command_handler.py): chat input boundary · Applies command handling before state-mutating actions. · `src/commands/command_handler.py`
- [Permissions & cooldowns](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/permissions.py): authorization policy · `src/commands/permissions.py`
- [Game framework](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/games/base_game.py): game domain · Shared base for twenty-one, beer challenge, and barrel events. · `src/commands/games/base_game.py`
- [Game state models](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/models/game_models.py): domain models · `src/commands/models/game_models.py`
- [Game coordination cache](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/commands/managers/cache_manager.py): temporary state · Holds cooldown and coordination state. · `src/commands/managers/cache_manager.py`

### EventSub pipeline

- [EventSub manager](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/eventsub/manager.py): subscription processor · `src/eventsub/manager.py`
- [Event router](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/eventsub/handlers.py): EventSub handlers · `src/eventsub/handlers.py`
- [Redemption handlers](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/eventsub/reward_handlers.py): Channel Points adapter · Maps redemptions to game actions and completion semantics. · `src/eventsub/reward_handlers.py`
- [Redemption context](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/eventsub/ctx_factory.py): execution context factory · Creates command/game context without chat input. · `src/eventsub/ctx_factory.py`

### State & credentials

- [PostgreSQL persistence](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/db/database.py): async ORM access · Central database setup and persisted entities. · `src/db/database.py`
- [Redis client](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/core/redis_client.py): cache connectivity · `src/core/redis_client.py`
- [Configuration & tokens](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/src/core/config_loader.py): credential lifecycle · Loads configuration and manages Twitch tokens. · `src/core/config_loader.py`

### Administration plane

- [Admin application](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/app/main.py): FastAPI service · Independent operational control service. · `admin/app/main.py`
- [Admin API](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/app/routes/api.py): HTTP/WebSocket routes · `admin/app/routes/api.py`
- [Admin security](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/app/security.py): JWT/password security · `admin/app/security.py`
- [Remote Docker operations](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/app/services/ssh_client.py): SSH and log streaming · Controls Docker hosts and streams container logs. · `admin/app/services/ssh_client.py`
- [Admin proxy](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/admin/nginx/nginx.conf): Nginx boundary · `admin/nginx/nginx.conf`

### Other components

- [Compose deployment](https://github.com/jkeevk/aimlul-twitch-bot/blob/main/docker-compose.yml): orchestration · Runs bot, admin, PostgreSQL, and Redis services. · `docker-compose.yml`
- **Twitch platform**: external service · Chat, REST, and Channel Points boundary.

## Connections

- 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
