Skip to main content
Version: v2.10.5

Event handling​

note

This page requires a valid main file, at least one command from creating commands, and the command handler in place. It applies to both slash commands and message commands.


Before you continue​

Assuming you've followed the guide so far, your project directory should look something like this:

discord-bot/
├── node_modules/
├── src/
│ ├── commands/
│ │ └── music/
│ │ └── play.js
│ ├── config.js
│ └── index.js
├── package-lock.json
└── package.json

Loading event files​

Remove any existing client.on() calls from index.js. Then add the following event loader right after the command-loading section:

const loadEvents = (folderPath) => {
const eventFiles = fs.readdirSync(folderPath);

for (const file of eventFiles) {
const filePath = path.join(folderPath, file);
const stat = fs.statSync(filePath);

if (stat.isDirectory()) {
loadEvents(filePath);
} else if (file.endsWith(".js")) {
const event = require(filePath);

if (event.once) {
if (filePath.includes(`${path.sep}magmastream${path.sep}`)) {
client.manager.on(event.name, (...args) => event.execute(client, ...args));
} else {
client.once(event.name, (...args) => event.execute(client, ...args));
}
} else {
if (filePath.includes(`${path.sep}magmastream${path.sep}`)) {
client.manager.on(event.name, (...args) => event.execute(client, ...args));
} else {
client.on(event.name, (...args) => event.execute(client, ...args));
}
}
console.log(`[INFO] Loaded event: ${event.name}`);
}
}
};

const eventsPath = path.join(__dirname, "events");
loadEvents(eventsPath);

This uses the same recursive pattern as the command loader. Files inside an events/magmastream/ subfolder are registered on client.manager; all others are registered on client.

You can review the complete index.js on the GitRyx repository here .


Event file structure​

Create an events/ folder inside src/ with two subfolders: discord/ and magmastream/.

note

Select the tab below for the events relevant to your setup.

Create raw.js, ready.js, and interactionCreate.js inside events/discord/. If you are using message commands, also create messageCreate.js.

const { Events } = require("discord.js");

module.exports = {
name: Events.ClientReady,
once: true,

async execute(client) {
await client.manager.init({ clientId: client.user.id });
console.log(`[INFO] Ready! Logged in as ${client.user.tag}`);
},
};

Your project directory should now look like this:

discord-bot/
├── node_modules/
├── src/
│ ├── commands/
│ │ └── music/
│ │ └── play.js
│ ├── events/
│ │ ├── discord/
│ │ │ ├── interactionCreate.js
│ │ │ ├── messageCreate.js
│ │ │ ├── raw.js
│ │ │ └── ready.js
│ │ └── magmastream/
│ │ ├── nodeConnect.js
│ │ ├── queueEnd.js
│ │ └── trackStart.js
│ ├── config.js
│ └── index.js
├── package-lock.json
└── package.json

Next steps​

Add more events by creating new files in the relevant subfolder. Next, create a deployment script to register your slash commands with Discord.


Resulting code​

You can review the complete example on the GitRyx repository here .