Skip to main content
Version: v2.10.0

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 GitHub 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 GitHub repository here .