Skip to content

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.js in package.json
  • new Client({ intents: [...] }) and client.login(token)
  • SlashCommandBuilder and a deploy-commands script

Where things usually live

src/index.ts
Creates the client, loads commands and events, logs in.
src/commands/
One file per command, exporting data and execute.
src/events/
Handlers for events such as ready and interactionCreate.
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

  1. index.ts, for intents and how commands are loaded
  2. The interactionCreate handler, which dispatches commands
  3. The command file

How a request moves through the code

  1. 1The bot logs in and receives the ready event.
  2. 2A user runs a slash command; Discord sends an interaction.
  3. 3The interactionCreate handler looks up the command by name.
  4. 4execute runs and must reply or defer within a few seconds.
  5. 5Buttons 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.