Skip to main content
Version: v2.8.6 (current)

Event handling​

tip

πŸ’‘ This page requires you to have a valid main file, have at least created one command following these steps and followed the last step command handling.

info

ℹ️ This section can be done for both slash-commands and message-commands, as Client#messageCreate as message-commands are executed from within the Client#messageCreate event.

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/
β”‚ β”‚ └── play.js
β”‚ β”œβ”€β”€ config.js
β”‚ └── index.js
β”œβ”€β”€ package-lock.json
└── package.json

Individual event files​

Loading event files​

To begin, remove any code from index.js that is related to client.on().

Then, add the following event-loading code into the index.js file, right after the command-loading section:

const loadEvents = (folderPath) => {
// Read all files from the specified folder
const eventFiles = fs.readdirSync(folderPath);

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

// If it's a directory, recursively load events from it
if (stat.isDirectory()) {
loadEvents(filePath);
} else if (file.endsWith('.js')) {
// Require the event module
const event = require(filePath);

// If the event should be handled only once
if (event.once) {
if (filePath.includes(`${path.sep}magmastream${path.sep}`)) {
// Handle Magmastream events differently
client.manager.on(event.name, (...args) => event.execute(client, ...args));
} else {
// Register a one-time event listener
client.once(event.name, (...args) => event.execute(client, ...args));
}
} else {
if (filePath.includes(`${path.sep}magmastream${path.sep}`)) {
// Handle Magmastream events differently
client.manager.on(event.name, (...args) => event.execute(client, ...args));
} else {
// Register a persistent event listener
client.on(event.name, (...args) => event.execute(client, ...args));
}
}
console.log(`[INFO] Loaded event: ${event.name}`);
}
}
};

This code uses a the exact method to load files as the command loader.

If you want to make sure, you have the exact code for the index.js file, you can review it on the GitHub repository here .

Event files structure​

Create a new folder inside the src/ folder, named events and two subfolder named discord and magmastream inside it.

warning

⚠️ Please make sure to select below the command creation "step" you need. The section below is divided into "Discord Events" and "Magmastream Events".

Let’s begin by creating three files named raw.js, ready.js and interactionCreate.js inside the events/discord folder.

The files should be structured as follows:

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

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

execute(client) {
// Initialize Lavalink
client.manager.init(client.user.id);

// Print a message to the console indicating that the client is ready
console.log(`[INFO] Ready! Logged in as ${client.user.tag}`);
},
};

if you want to make sure, you have the exact code for the different events etc. take a look at the example GitHub repository here .

Next steps​

You can implement additional events by creating new files within a dedicated subfolder in the events folder, but these are the ones we're going to use for the examples as we go on.

Next: We'll create a [deployment script] for our slash commands.

Resulting code​

If you want to compare your code to the final result after completing all the steps in this guide, you can review it on the GitHub repository here .