How to understand a Discord bot (discord.js) codebase
A discord.js bot is a long-running process that connects to Discord's gateway and reacts to events. Slash commands are defined as data, registered with Discord through a separate script, and handled when an interaction event arrives.
How to tell it's Discord bot (discord.js)
discord.jsinpackage.jsonnew Client({ intents: [...] })andclient.login(token)SlashCommandBuilderand adeploy-commandsscript
Where things usually live
- src/index.ts
- Creates the client, loads commands and events, logs in.
- src/commands/
- One file per command, exporting
dataandexecute. - src/events/
- Handlers for events such as
readyandinteractionCreate. - src/deploy-commands.ts
- Registers command definitions with Discord's API.
Conventions, not rules. Real projects rename and move things, so confirm against the repository.
Where to start reading
index.ts, for intents and how commands are loaded- The
interactionCreatehandler, which dispatches commands - The command file
How a request moves through the code
- The bot logs in and receives the
readyevent. - A user runs a slash command; Discord sends an interaction.
- The
interactionCreatehandler looks up the command by name. executeruns and must reply or defer within a few seconds.- Buttons and menus send further interactions handled by custom IDs.
Questions to ask about the repository
- Which intents does the bot request, and which features need them?
- List the commands and what each does.
- How are command definitions deployed, globally or per server?
What trips people up
- Changing a command's definition needs the deploy script to run again; code changes alone aren't enough.
- Missing intents mean events never arrive, which looks like a handler bug.