Bot Startup & Client Configuration
Relevant source files
The following files were used as context for generating this wiki page:
- bot.js
- src/client/createClient.js
- src/config/env.js (Referenced via test implementation)
- src/commands/registerSlashCommands.js
- AGENTS.md
- package.json
The TrackerBot startup process is a structured sequence that ensures environment integrity, initializes the Discord client with specific gateway intents, and registers event handlers and slash commands before authenticating with Discord. The system is built using ESM (ECMAScript Modules) and requires Node.js 18 or higher.
Sources: AGENTS.md:1-10, package.json:1-15, bot.js:1-12
Initialization Lifecycle
The bot follows a linear execution path starting from bot.js. This entry point orchestrates the validation of environmental requirements and the instantiation of the core client.
The diagram shows the sequential startup flow where environment validation must pass before any other service starts.
Sources: bot.js:10-28, AGENTS.md:15-20
Environment Validation
Before the Discord client is created, the bot calls ensureEnvVars(). This function validates that all critical configuration keys are present in the environment (sourced via dotenv). If required variables like DISCORD_TOKEN or WELCOME_CHANNEL_ID are missing, the process is designed to throw an error and prevent startup, following the "never crash the process" rule for runtime but enforcing strict requirements at boot.
Sources: bot.js:10, test/env.test.js:34-45, AGENTS.md:30-35
Client Configuration
The Discord client is configured in src/client/createClient.js. The configuration focuses on minimizing resource usage by selecting only the necessary Gateway Intents and Partials.
Gateway Intents and Partials
The bot utilizes discord.js v14 and requires specific intents to monitor server activity, member joins, and message content for honeypot functionality.
| Intent / Partial | Purpose |
|---|---|
Guilds | Required for basic interaction with Discord servers. |
GuildMembers | Needed for tracking joins/leaves and auto-assigning roles. |
GuildMessages | Enables monitoring of messages for honeypot triggers. |
MessageContent | Privileged intent required to read the text content of messages. |
Partials.Channel | Allows the bot to handle events from channels that may not be fully cached. |
Sources: src/client/createClient.js:3-13
// src/client/createClient.js:3-13
export function createClient() {
return new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
],
partials: [Partials.Channel]
});
}
Service Registration
Once the client is instantiated, the bot registers various modular handlers. This registration happens before the client.login() call to ensure the bot is ready to process events as soon as the connection is established.
Handler Modules
- Interaction Handler: Processes slash commands and button interactions.
- Member Handlers: Manages
GuildMemberAddandGuildMemberRemoveevents for welcome automation and role assignment. - Honeypot System: Sets up a hidden channel monitor to auto-ban spam bots.
Sources: bot.js:24-26, src/handlers/memberHandlers.js:13-20, src/handlers/honeypotHandler.js:28-30
Slash Command Deployment
The registration of slash commands occurs within the ClientReady event. This is an asynchronous process using the Discord REST API to sync the bot's local command definitions with Discord's global or guild-level command list.
The sequence diagram illustrates that command registration is deferred until the bot successfully authenticates and becomes "Ready."
Sources: bot.js:14-22, src/commands/registerSlashCommands.js:33-39
Execution Environment
The project is structured as a dual-service architecture. While bot.js handles Discord interactions, webserver.js (an Express server) typically runs concurrently to handle GitHub issue intake. Startup is managed via npm scripts defined in package.json.
| Command | Script | Description |
|---|---|---|
npm run start:bot | node bot.js | Launches only the Discord bot service. |
npm start | concurrently ... | Launches both the Discord bot and the Webserver. |
Sources: package.json:7-10, AGENTS.md:5-10
The startup sequence of TrackerBot is designed for resilience, ensuring that environmental configuration is verified before the Discord client consumes resources or attempts to register handlers. By centralizing client creation and intent management in src/client/, the architecture maintains a clean separation between configuration and event logic.