🏗️ System Architecture & Lifecycle
🏠 Documentation Hub > Architecture Overview
This guide provides an accurate, code-accurate technical walkthrough of Reverb's internal architecture, component hierarchies, initialization sequence, and data flow.
1. Architectural Topology
Reverb separates Discord gateway processing, audio node communication, database operations, and command dispatching into clean, modular tiers:
2. Boot & Initialization Lifecycle
When running npm start, pnpm start, or yarn start, Reverb executes through a defined lifecycle:
Lifecycle Steps in Detail:
- Sharding Initialization (
index.js): Creates aShardingManagertargeting./src/reverbClient.jswithtotalShards: "auto". Shards are automatically spawned and managed. - Configuration & Hooks (
_loadHooks): Loadsconfig.reverb.js, initializes in-memory DiscordCollectioninstances for prefix commands, aliases, and slash commands, and calculates brand hex/integer colors. - Database Connection (
_loadDatabase): InstantiatesDatabaseManager, detects the configured driver (sqlite,mongodb,postgresql,mysql, orpglite), connects to the database, and initializes table schemas. - Music Manager (
_loadMusicManager): InstantiatesreverbManager(extending Moonlink'sManager), registers audio cluster nodes fromconfig.reverb.js, and sets up LRCLIB lyrics client. - REST Rate-Limit Monitor (
_loadRestManager): Listens toclient.rest.on("rateLimited")to log rate-limit headers and prevent thread starvation. - Core Handlers (
_loadHandler): Dynamically loads the 4 core handlers:clientEvents,commands,slashCommands, anderrors. - Gateway Login (
client.deploy()): Authenticates with the Discord Gateway usingbot.token. Once connected, theclientReadyevent initializes Moonlink withclient.user.idand registers application slash commands.
3. Audio Pipeline & Player Hierarchy
Reverb's audio management extends Moonlink.js with specialized managers:
Key Audio Classes:
reverbManager(src/structures/reverbManager.js): Extends Moonlink.jsManager. Configures node failover (leastLoad), auto-destruction on idle, stuck track detection, LRCLIB integration, and dynamically registers all events fromsrc/events/music/.reverbPlayerManager(src/structures/reverbPlayerManager.js): Extends Moonlink.jsPlayerManager. Manages player lifecycles, player collections, bulk disconnections, and creates instances ofreverbPlayer.reverbPlayer(src/structures/reverbPlayer.js): Extends Moonlink.jsPlayer. Adds custom guild data caching, persistent track message tracking (setTrackMessage), channel resolution, and the round-robin fairplay queue algorithm (setFairPlay).
4. Multi-Driver Database Architecture
Reverb uses an abstracted Repository pattern managed by DatabaseManager (src/database/DatabaseManager.js):
- Agnostic Persistence: Commands interact with unified async methods (e.g.
client.database.getDJ(guildId)orclient.database.getWhitelist(guildId)). - Auto-Migration: Relational providers (SQLite, PostgreSQL, MySQL, PGLite) automatically execute
CREATE TABLE IF NOT EXISTSand self-healingALTER TABLEmigrations on boot.
5. Core Handler Subsystems
Loaded via _loadHandler() from src/handler/:
| Handler | Source File | Functionality |
|---|---|---|
clientEvents | src/handler/clientEvents.js | Recursively reads non-music event folders (client, commands, system) and binds handlers to client.on(event.name, ...). |
commands | src/handler/commands.js | Recursively reads src/commands/messages/, registers commands and aliases into client.commands and client.aliases. |
slashCommands | src/handler/slashCommands.js | Recursively reads src/commands/slash/, registers commands into client.slashCommands, and handles global/dev deployment via Discord REST. |
errors | src/handler/errors.js | Catches global uncaught exceptions, unhandled promise rejections, and Discord REST errors to prevent bot crashes. |
⏭️ Next Step
Explore how events are routed through Reverb in Event Dispatching & Music Events.
