How clashkinginc/clashkingbot works
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: __init__.py Discord API · auto_refresh.py Background Jobs · commands.py Player Commands · client.py DB Clients
Components 23
Runtime
Process entry, bootstrapping, and Discord integration
- Main · Process entrypoint that starts the bot
main.py - Bot Boot · Bot assembly, startup wiring, and login flow
classes/bot.py - Discord API · Event hooks, autocomplete, converters, and slash option handling
discord/__init__.py - Startup Utils · Shared startup and login helpers
utility/startup.py
Domains
Command surfaces grouped by major feature areas
- Clan Commands · Clan management commands and helpers
commands/clan/commands.py - Player Commands · Player lookup, profiles, and related actions
commands/player/commands.py - War Commands · War operations and war-focused commands
commands/war/commands.py - Roster Commands · Roster and roster-click interactions
commands/rosters/commands.py - Board Commands · Board creation and board management commands
commands/boards/commands.py - Reminder Commands · Reminder creation, delivery, and management
commands/reminders/commands.py - Ticketing · Support ticket command flow and interactions
commands/ticketing/commands.py
Core Models
Shared business objects and service abstractions
- Config Models · Bot configuration, enums, and shared state objects
classes/config.py - Player Models · Player state, bans, legends, stats, and strikes
classes/player/base.py - Roster Models · Roster and membership abstractions
classes/roster.py - Errors · Custom exceptions and centralized error handling
exceptions/handler.py
Data Layer
Persistence and external data access
- DB Clients · Mongo-style client boundary for clan, player, family, and settings data
classes/DatabaseClient/client.py - Clash Data · Clash of Clans data normalization and game-specific helpers
utility/clash/other.py
Automation
Background jobs and log pipelines
- Background Jobs · Scheduled refresh, embed sharing, board refresh, and voice loops
background/features/auto_refresh.py - Log Pipelines · Join/leave, war, donations, legends, reminders, and other log sinks
background/logs/war.py - Cache Layer · Derived-state cache and ephemeral data distribution
background/tasks/background_cache.py
Presentation
Embeds, components, images, and localization
- Discord Output · Embeds, UI components, and interaction helpers
utility/discord/embeds.py - Image Gen · Generated graphics for wars, capital, upgrades, and leaderboards
utility/imagegen/WarEndResult.py - Locales & Assets · Translated strings, fonts, backgrounds, flags, and static assets
locales/en-US/player.ftl
Connections 28
- 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