This file summarizes the runtime architecture and data model of the bot as implemented in the repository. It is derived directly from the code (index.js, utils, events, models).
- index.js
- Loads commands and events dynamically from
commands/andevents/. - Initializes the
Databasesingleton (utils/database.js) which wrapsmodels/schemas.js. - Starts
TaskScheduler(utils/scheduler.js) for periodic jobs. - Rotates bot presence and handles graceful shutdown and global error handlers.
- Loads commands and events dynamically from
- Command loader
deploy-commands.jsscanscommands/, usesSlashCommandBuilderJSON viacommand.data.toJSON()and registers commands via Discord REST API.
- Events
events/interactionCreate.js— central handler for slash commands, buttons, modals, select menus; enforces per-command cooldowns and handles many interactive flows (tickets, AI modals, truth-or-dare, feedback).events/messageCreate.js— XP tracking, message-based AI auto-responses (when configured), and related side-effects.- Other events:
ready.js,guildMemberAdd.js,modalCreate.js,voiceStateUpdate.js,helpInteraction.js.
- Database layer
utils/database.jsexposes model methods and utility functions for server config, user profile, tickets, AI rate limits, XP transactions, leaderboards, birthdays, shop items, anti-raid, and guild economy.- Models defined in
models/schemas.js(see Data Models below).
- Background scheduler
utils/scheduler.jsusesnode-cronto run:- daily resets (midnight UTC)
- weekly resets (Monday midnight UTC)
- hourly leaderboard updates
- role checks every 30 minutes
- birthday checks daily at 8:00 UTC
- ServerConfig — per-guild configuration:
aiEnabled,aiContext,aiChannels,aiMode,ticketCategoryId,ticketLogChannelId, XP and role automation settings, birthday config, etc. - UserProfile — per-user-per-guild profile:
userId,guildId,wallet,bank,totalXp,level,messageCount,voiceMinutes,points,currentRoles,dailyStreak, timestamps. - Ticket — support ticket records with messages array, status, priority, moderatorId, closedAt.
- AIRateLimit — per-user rate limiting for AI requests (requestCount, lastRequest, resetAt).
- XPTransaction — XP audit log entries.
- Leaderboard — cached top users per guild.
- Birthday — birthday records.
- GuildEconomy, ShopItem, TruthOrDareConfig, AntiRaid — specialized configuration schemas.
(See models/schemas.js for full field lists and indexes.)
- Startup (
node index.js)index.jsloads commands and events and attempts to connect to MongoDB usingMONGODB_URI.- Scheduler is created and registered.
- Bot logs in with
DISCORD_TOKEN.
- Interaction flow
- Slash commands:
events/interactionCreate.jsreceives chat input commands, enforces cooldowns, and calls the command'sexecute(interaction, client). - Buttons/selects/modals:
interactionCreatedispatches UI interactions to helper handlers (tickets, help navigation, AI modals, truth-or-dare buttons).
- Slash commands:
- Message flow
messageCreate.jshandles XP awarding (rate-limited per user/time) and message-based AI auto-responses when the serverServerConfigallows it.
- Scheduler tasks
- Cron jobs perform resets, leaderboard updates, and role assignment checks.
- Each command file exports at least
data(SlashCommandBuilder) andexecutefunction. - Commands may include a
cooldownproperty (seconds) used by the interaction handler. - Commands are organized in folders (fun, moderation, economy, xp, ai, truth-or-dare, etc.).
- Discord (discord.js v14) — main runtime and components
- MongoDB (mongoose) — persistence
- Google Gemini via
@google/genai— AI assistant features (requiresGEMINI_API_KEY) - node-cron — scheduling
DISCORD_TOKEN(required) — bot tokenCLIENT_ID(required fordeploy-commands.js) — application idGUILD_ID(optional) — when presentdeploy-commands.jsdeploys commands to that guildMONGODB_URI(required) — MongoDB connection stringGEMINI_API_KEY(optional) — Google Gemini key for AIPORT(optional) — not used by default; an express server is present but commented out
- Intents requested in
index.js:Guilds,GuildMembers,GuildMessages,MessageContent,GuildMessageReactions,GuildVoiceStates,GuildPresences. - Privileged intents (Message Content, Guild Members, Presences) must be enabled in the Discord Developer Portal to use the corresponding features.
- Bot requires typical moderation permissions depending on features used (Ban/Kick, ManageChannels for tickets, ManageRoles for automatic role assignment, ManageMessages for purge, EmbedLinks + SendMessages).
- Scheduler runs in-process; running multiple instances will duplicate scheduled jobs unless you coordinate (e.g., leader election or single scheduler process).
- Database connections are per-process (the
Databasesingleton avoids re-connecting repeatedly in the same process). - Consider using Redis or another cache if you need high-throughput leaderboards or cross-process rate-limiting.
- Add new commands: create a file in
commands/<category>/exportingdataandexecute. Runnode deploy-commands.jsto register slash commands. - Add new events: place a new file in
events/exporting{ name, execute, once? }and the loader inindex.jswill attach it. - New DB models: update
models/schemas.jsand expose them viautils/database.jsfor convenience.