← Back to lessons

Lesson 2 — How the Bot Works

Learn Node.js, Discord.js, the handler, and what each folder does.

1. What is Node.js?

Node.js is what runs your bot. It allows JavaScript to run on your computer instead of a browser. Your bot is just a JavaScript program running with Node.js.

node index.js

This command starts your bot.

2. What is Discord.js?

Discord.js is a library that lets your bot talk to Discord. It listens for events like messages, commands, buttons, and menus.

client.on("interactionCreate", interaction => {
  // This runs when a slash command or button is used
});

3. Entry Files

index.js → Starts the bot and loads the system

app.js → Creates the Discord client and logs into Discord

4. Folder Structure

commands/  → Slash commands (/ping)
context/   → Right-click commands
messages/  → Prefix commands (!ping)
events/    → Discord events (ready, interactionCreate)
buttons/   → Button click handlers
menus/     → Select menu handlers
modals/    → Modal submit handlers
models/    → Database models
schemas/   → Database schemas
utils/     → The handler (loads everything)
logs/      → Error logs

5. What is the Handler?

The handler is the system that automatically loads all commands, events, buttons, menus, and modals. Instead of importing every file manually, the handler reads the folders and loads them for you.

Interaction happens
→ events/interactionCreate.js runs
→ Handler checks what the interaction is
→ Handler finds the correct file
→ execute() runs

6. The execute() Function

Every command or button file has an execute() function. This is the code that runs when the command or button is used.

module.exports = {
  name: "ping",
  execute(interaction) {
    interaction.reply("Pong!");
  }
};

7. Full Bot Flow

index.js starts bot
→ app.js logs into Discord
→ utils handler loads files
→ User runs command
→ events/interactionCreate.js fires
→ Handler finds command file
→ execute() runs
→ Bot replies

Completion checklist

← Lesson 1 Next → Lesson 3