← Back to lessons

Lesson 9 — How the Loader Works

Goal: Understand the “magic” without editing utils.

Introduction

Up until now, you’ve been creating commands like ping.js and they just work.

But what’s actually happening behind the scenes?

This lesson explains how your bot receives commands, finds the correct file, and executes it.

Step-by-step

Step 1 — Pick a command you created

Start with a command you already made, like: commands/ping.js

This file contains an execute() function that runs when the command is used.

Step 2 — Find interactionCreate event

Go to your events/ folder and open the interaction file.

This is where all Discord interactions enter your bot.

Step 3 — Follow the chain

You should see something like:

interactionCreate
→ identify interaction type
→ look up command/component
→ run checks (cooldowns/permissions)
→ execute()

Here’s what each step means:

  • Interaction received: The bot listens for events from Discord.
  • Identify type: It checks if it's a slash command, button, etc.
  • Find command: It matches the command name to a file.
  • Run checks: It verifies permissions or cooldowns.
  • Execute: It runs the command’s execute() function.
Step 4 — One sentence explanation

“When a user runs a slash command, the bot receives an interaction event, finds the matching command file, and executes its function.”

Why This Matters

Simple Analogy

Think of the system like a receptionist:

The receptionist receives the request and sends it to the right person.

← Lesson 8 Next → Lesson 10