# clashkinginc/clashkingbot architecture

> Architecture diagram of the GitHub repository clashkinginc/clashkingbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/clashkinginc/clashkingbot
- Repository: https://github.com/clashkinginc/clashkingbot

## Overview

1. **Top-level purpose**
   ClashKing is a long-running Discord bot for Clash of Clans clan operations. The runtime is centered on Discord slash commands, event handlers, and scheduled background jobs that all operate on the same clan/player data model.

2. **Process entry and bootstrapping**
   `main.py` is the app entrypoint, with startup/runtime wiring spread through `classes/bot.py`, `utility/startup.py`, and `utility/login.py`. These modules likely assemble the Discord client, configure services, load cogs/commands, connect to persistence layers, and start the event loop.

3. **Discord integration boundary**
   The `discord/` package is the bot-facing adapter layer around the Discord framework. It contains event hooks, autocomplete, converters, slash-command options, and interaction handling. This is the main ingress/egress boundary for user actions and bot responses.

4. **Command surface area**
   The `commands/` tree is organized by feature domain rather than by technical layer: bans, boards, clan, family, giveaways, leaderboards, legends, link, reminders, rosters, settings, setup, stats, strikes, ticketing, war, and more. Each domain usually has a `commands.py` plus a `utils.py`, indicating that command orchestration is separated from reusable domain logic.

5. **Core domain models and business logic**
   The `classes/` directory contains the bot’s domain objects and service abstractions: players, rosters, reminders, tickets, config, enums, and custom helpers. This is where the bot’s main state and business rules live, independent of individual command handlers.

6. **Database and persistence layer**
   `classes/DatabaseClient/` is the repository’s main data-access boundary. It is split into client types such as `client.py`, `clanclient.py`, `playerclient.py`, and `familyclient.py`, plus typed models for player, legends, and settings. This suggests a structured persistence layer for MongoDB-backed reads/writes and domain-specific queries.

7. **External game data and API integration**
   The bot relies on Clash of Clans data through `coc.py` and related helpers in `utility/clash/` and `utility/war.py`. These modules appear to normalize game-specific concepts such as capital, war, and other clan/player state before it reaches commands or background jobs.

8. **Background processing and automation**
   `background/` contains the non-interactive execution paths: feature loops like auto-refresh, embed sharing, board refreshing, and voice stat loops, plus logging pipelines for joins/leaves, donations, wars, legends, events, reminders, and more. This is the bot’s async automation layer, responsible for keeping Discord channels, logs, and board posts up to date without direct user interaction.

9. **Caching and derived state**
   `background/tasks/background_cache.py` and `utility/cdn.py` indicate a cache/distribution layer for computed artifacts. The presence of Redis in the README suggests ephemeral state and rate-sensitive data are cached separately from durable MongoDB records.

10. **Rendering and rich Discord output**
   `utility/discord/embeds.py`, `utility/discord/components.py`, `utility/components.py`, and the `utility/imagegen/` subtree generate the visual layer of the bot. This includes Discord embeds, interactive components/buttons, and image-based outputs for wars, clan capital, player upgrades, and leaderboard-style results.

11. **Localization and content assets**
   The `locales/` tree contains Fluent translation files for many languages, showing that text rendering is internationalized rather than hardcoded. `assets/` provides fonts, country flags, backgrounds, and static generation helpers, which are used by the image/visual rendering pipeline.

12. **Operational concerns and error handling**
   `exceptions/handler.py` centralizes custom error handling, while `utility/constants.py`, `utility/general.py`, `utility/time.py`, and `utility/search.py` provide shared cross-cutting utilities. This suggests a layered design where commands and background jobs call common helpers instead of duplicating validation, formatting, and lookup logic.

13. **Testing and deployment**
   `testing/migrations.py` implies explicit migration or data-shape testing support. The repository is containerized via `Dockerfile`, and CI is managed with GitHub Actions in `.github/workflows/main.yml`, making the bot deployable as a single service process with external dependencies on Discord, MongoDB, Redis, and Clash of Clans data services.

Main entry points: [`discord/__init__.py`](https://github.com/clashkinginc/clashkingbot/blob/master/discord/__init__.py) (Discord API), [`background/features/auto_refresh.py`](https://github.com/clashkinginc/clashkingbot/blob/master/background/features/auto_refresh.py) (Background Jobs), [`commands/player/commands.py`](https://github.com/clashkinginc/clashkingbot/blob/master/commands/player/commands.py) (Player Commands), [`classes/DatabaseClient/client.py`](https://github.com/clashkinginc/clashkingbot/blob/master/classes/DatabaseClient/client.py) (DB Clients)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main(("Main<br/>entrypoint<br/>[main.py]"))
  node_bot_boot["Bot Boot<br/>startup<br/>[bot.py]"]
  node_discord_adapter["Discord API<br/>adapter<br/>[__init__.py]"]
  node_setup_utils["Startup Utils<br/>bootstrap<br/>[startup.py]"]
end

subgraph group_domains["Domains"]
  node_clan_cmds["Clan Commands<br/>command domain<br/>[commands.py]"]
  node_player_cmds["Player Commands<br/>command domain<br/>[commands.py]"]
  node_war_cmds["War Commands<br/>command domain<br/>[commands.py]"]
  node_roster_cmds["Roster Commands<br/>command domain<br/>[commands.py]"]
  node_board_cmds["Board Commands<br/>command domain<br/>[commands.py]"]
  node_reminder_cmds["Reminder Commands<br/>command domain<br/>[commands.py]"]
  node_ticket_cmds["Ticketing<br/>command domain<br/>[commands.py]"]
end

subgraph group_core["Core Models"]
  node_config_models["Config Models<br/>domain model<br/>[config.py]"]
  node_player_models["Player Models<br/>domain model<br/>[base.py]"]
  node_roster_models["Roster Models<br/>domain model<br/>[roster.py]"]
  node_errors{{"Errors<br/>error handling<br/>[handler.py]"}}
end

subgraph group_data["Data Layer"]
  node_db_clients[("DB Clients<br/>persistence<br/>[client.py]")]
  node_clash_api["Clash Data<br/>external api<br/>[other.py]"]
end

subgraph group_automation["Automation"]
  node_background_jobs["Background Jobs<br/>automation<br/>[auto_refresh.py]"]
  node_log_pipelines["Log Pipelines<br/>automation<br/>[war.py]"]
  node_cache_layer[("Cache Layer")]
end

subgraph group_presentation["Presentation"]
  node_discord_output["Discord Output<br/>rendering<br/>[embeds.py]"]
  node_imagegen["Image Gen<br/>rendering<br/>[WarEndResult.py]"]
  node_i18n_assets["Locales & Assets<br/>content<br/>[player.ftl]"]
end

node_main -->|"starts"| node_bot_boot
node_bot_boot -->|"bootstraps"| node_setup_utils
node_bot_boot -->|"registers"| node_discord_adapter
node_discord_adapter -->|"routes"| node_clan_cmds
node_discord_adapter -->|"routes"| node_player_cmds
node_discord_adapter -->|"routes"| node_war_cmds
node_discord_adapter -->|"routes"| node_roster_cmds
node_discord_adapter -->|"routes"| node_board_cmds
node_discord_adapter -->|"routes"| node_reminder_cmds
node_discord_adapter -->|"routes"| node_ticket_cmds
node_clan_cmds -->|"reads/writes"| node_db_clients
node_player_cmds -->|"uses"| node_player_models
node_player_cmds -->|"reads/writes"| node_db_clients
node_war_cmds -->|"normalizes"| node_clash_api
node_war_cmds -->|"renders"| node_imagegen
node_roster_cmds -->|"uses"| node_roster_models
node_board_cmds -->|"feeds"| node_background_jobs
node_reminder_cmds -->|"schedules"| node_background_jobs
node_background_jobs -->|"triggers"| node_log_pipelines
node_background_jobs -->|"reads/writes"| node_cache_layer
node_log_pipelines -->|"reads/writes"| node_db_clients
node_log_pipelines -->|"publishes"| node_discord_output
node_discord_output -->|"composes"| node_imagegen
node_discord_output -->|"localizes"| node_i18n_assets
node_clan_cmds -->|"uses"| node_config_models
node_player_cmds -->|"handles"| node_errors
node_background_jobs -->|"handles"| node_errors
node_db_clients -->|"hydrates"| node_clash_api

click node_main "https://github.com/clashkinginc/clashkingbot/blob/master/main.py"
click node_bot_boot "https://github.com/clashkinginc/clashkingbot/blob/master/classes/bot.py"
click node_discord_adapter "https://github.com/clashkinginc/clashkingbot/blob/master/discord/__init__.py"
click node_setup_utils "https://github.com/clashkinginc/clashkingbot/blob/master/utility/startup.py"
click node_clan_cmds "https://github.com/clashkinginc/clashkingbot/blob/master/commands/clan/commands.py"
click node_player_cmds "https://github.com/clashkinginc/clashkingbot/blob/master/commands/player/commands.py"
click node_war_cmds "https://github.com/clashkinginc/clashkingbot/blob/master/commands/war/commands.py"
click node_roster_cmds "https://github.com/clashkinginc/clashkingbot/blob/master/commands/rosters/commands.py"
click node_board_cmds "https://github.com/clashkinginc/clashkingbot/blob/master/commands/boards/commands.py"
click node_reminder_cmds "https://github.com/clashkinginc/clashkingbot/blob/master/commands/reminders/commands.py"
click node_ticket_cmds "https://github.com/clashkinginc/clashkingbot/blob/master/commands/ticketing/commands.py"
click node_config_models "https://github.com/clashkinginc/clashkingbot/blob/master/classes/config.py"
click node_player_models "https://github.com/clashkinginc/clashkingbot/blob/master/classes/player/base.py"
click node_roster_models "https://github.com/clashkinginc/clashkingbot/blob/master/classes/roster.py"
click node_db_clients "https://github.com/clashkinginc/clashkingbot/blob/master/classes/DatabaseClient/client.py"
click node_clash_api "https://github.com/clashkinginc/clashkingbot/blob/master/utility/clash/other.py"
click node_background_jobs "https://github.com/clashkinginc/clashkingbot/blob/master/background/features/auto_refresh.py"
click node_log_pipelines "https://github.com/clashkinginc/clashkingbot/blob/master/background/logs/war.py"
click node_cache_layer "https://github.com/clashkinginc/clashkingbot/blob/master/background/tasks/background_cache.py"
click node_discord_output "https://github.com/clashkinginc/clashkingbot/blob/master/utility/discord/embeds.py"
click node_imagegen "https://github.com/clashkinginc/clashkingbot/blob/master/utility/imagegen/WarEndResult.py"
click node_i18n_assets "https://github.com/clashkinginc/clashkingbot/blob/master/locales/en-US/player.ftl"
click node_errors "https://github.com/clashkinginc/clashkingbot/blob/master/exceptions/handler.py"

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_boot,node_discord_adapter,node_setup_utils toneBlue
class node_clan_cmds,node_player_cmds,node_war_cmds,node_roster_cmds,node_board_cmds,node_reminder_cmds,node_ticket_cmds toneAmber
class node_config_models,node_player_models,node_roster_models,node_errors toneMint
class node_db_clients,node_clash_api toneRose
class node_background_jobs,node_log_pipelines,node_cache_layer toneIndigo
class node_discord_output,node_imagegen,node_i18n_assets toneTeal
```

## Components

### Runtime

Process entry, bootstrapping, and Discord integration

- [Main](https://github.com/clashkinginc/clashkingbot/blob/master/main.py): entrypoint · Process entrypoint that starts the bot · `main.py`
- [Bot Boot](https://github.com/clashkinginc/clashkingbot/blob/master/classes/bot.py): startup · Bot assembly, startup wiring, and login flow · `classes/bot.py`
- [Discord API](https://github.com/clashkinginc/clashkingbot/blob/master/discord/__init__.py): adapter · Event hooks, autocomplete, converters, and slash option handling · `discord/__init__.py`
- [Startup Utils](https://github.com/clashkinginc/clashkingbot/blob/master/utility/startup.py): bootstrap · Shared startup and login helpers · `utility/startup.py`

### Domains

Command surfaces grouped by major feature areas

- [Clan Commands](https://github.com/clashkinginc/clashkingbot/blob/master/commands/clan/commands.py): command domain · Clan management commands and helpers · `commands/clan/commands.py`
- [Player Commands](https://github.com/clashkinginc/clashkingbot/blob/master/commands/player/commands.py): command domain · Player lookup, profiles, and related actions · `commands/player/commands.py`
- [War Commands](https://github.com/clashkinginc/clashkingbot/blob/master/commands/war/commands.py): command domain · War operations and war-focused commands · `commands/war/commands.py`
- [Roster Commands](https://github.com/clashkinginc/clashkingbot/blob/master/commands/rosters/commands.py): command domain · Roster and roster-click interactions · `commands/rosters/commands.py`
- [Board Commands](https://github.com/clashkinginc/clashkingbot/blob/master/commands/boards/commands.py): command domain · Board creation and board management commands · `commands/boards/commands.py`
- [Reminder Commands](https://github.com/clashkinginc/clashkingbot/blob/master/commands/reminders/commands.py): command domain · Reminder creation, delivery, and management · `commands/reminders/commands.py`
- [Ticketing](https://github.com/clashkinginc/clashkingbot/blob/master/commands/ticketing/commands.py): command domain · Support ticket command flow and interactions · `commands/ticketing/commands.py`

### Core Models

Shared business objects and service abstractions

- [Config Models](https://github.com/clashkinginc/clashkingbot/blob/master/classes/config.py): domain model · Bot configuration, enums, and shared state objects · `classes/config.py`
- [Player Models](https://github.com/clashkinginc/clashkingbot/blob/master/classes/player/base.py): domain model · Player state, bans, legends, stats, and strikes · `classes/player/base.py`
- [Roster Models](https://github.com/clashkinginc/clashkingbot/blob/master/classes/roster.py): domain model · Roster and membership abstractions · `classes/roster.py`
- [Errors](https://github.com/clashkinginc/clashkingbot/blob/master/exceptions/handler.py): error handling · Custom exceptions and centralized error handling · `exceptions/handler.py`

### Data Layer

Persistence and external data access

- [DB Clients](https://github.com/clashkinginc/clashkingbot/blob/master/classes/DatabaseClient/client.py): persistence · Mongo-style client boundary for clan, player, family, and settings data · `classes/DatabaseClient/client.py`
- [Clash Data](https://github.com/clashkinginc/clashkingbot/blob/master/utility/clash/other.py): external api · Clash of Clans data normalization and game-specific helpers · `utility/clash/other.py`

### Automation

Background jobs and log pipelines

- [Background Jobs](https://github.com/clashkinginc/clashkingbot/blob/master/background/features/auto_refresh.py): automation · Scheduled refresh, embed sharing, board refresh, and voice loops · `background/features/auto_refresh.py`
- [Log Pipelines](https://github.com/clashkinginc/clashkingbot/blob/master/background/logs/war.py): automation · Join/leave, war, donations, legends, reminders, and other log sinks · `background/logs/war.py`
- [Cache Layer](https://github.com/clashkinginc/clashkingbot/blob/master/background/tasks/background_cache.py): cache · Derived-state cache and ephemeral data distribution · `background/tasks/background_cache.py`

### Presentation

Embeds, components, images, and localization

- [Discord Output](https://github.com/clashkinginc/clashkingbot/blob/master/utility/discord/embeds.py): rendering · Embeds, UI components, and interaction helpers · `utility/discord/embeds.py`
- [Image Gen](https://github.com/clashkinginc/clashkingbot/blob/master/utility/imagegen/WarEndResult.py): rendering · Generated graphics for wars, capital, upgrades, and leaderboards · `utility/imagegen/WarEndResult.py`
- [Locales & Assets](https://github.com/clashkinginc/clashkingbot/blob/master/locales/en-US/player.ftl): content · Translated strings, fonts, backgrounds, flags, and static assets · `locales/en-US/player.ftl`

## Connections

- Main → Bot Boot: starts · Main launches the bot runtime
- Bot Boot → Startup Utils: bootstraps · Startup helpers assemble and authenticate the bot
- Bot Boot → Discord API: registers · Bot wires Discord event and command surfaces
- Discord API → Clan Commands: routes · Discord interactions dispatch into clan commands
- Discord API → Player Commands: routes · Discord interactions dispatch into player commands
- Discord API → War Commands: routes · Discord interactions dispatch into war commands
- Discord API → Roster Commands: routes · Discord interactions dispatch into roster commands
- Discord API → Board Commands: routes · Discord interactions dispatch into board commands
- Discord API → Reminder Commands: routes · Discord interactions dispatch into reminder commands
- Discord API → Ticketing: routes · Discord interactions dispatch into ticketing flows
- Clan Commands → DB Clients: reads/writes · Clan operations persist and query shared data
- Player Commands → Player Models: uses · Player commands operate on player domain state
- Player Commands → DB Clients: reads/writes · Player commands query and update persistence
- War Commands → Clash Data: normalizes · War logic depends on Clash of Clans data helpers
- War Commands → Image Gen: renders · War commands produce visual summaries and results
- Roster Commands → Roster Models: uses · Roster commands manipulate roster state
- Board Commands → Background Jobs: feeds · Board commands connect with scheduled refresh flows
- Reminder Commands → Background Jobs: schedules · Reminders are delivered by async background processing
- Background Jobs → Log Pipelines: triggers · Refresh loops and log processors run as background workers
- Background Jobs → Cache Layer: reads/writes · Jobs rely on cached derived state
- Log Pipelines → DB Clients: reads/writes · Logging pipelines persist operational history
- Log Pipelines → Discord Output: publishes · Logs are formatted into Discord messages and embeds
- Discord Output → Image Gen: composes · Rendered Discord output may include generated images
- Discord Output → Locales & Assets: localizes · User-facing text is sourced from translation files
- Clan Commands → Config Models: uses · Commands consult shared configuration and enums
- Player Commands → Errors: handles · Command failures flow through centralized exceptions
- Background Jobs → Errors: handles · Automation paths share common error handling
- DB Clients → Clash Data: hydrates · Persistence layer stores normalized game data
