Event Handler
Guide on how to organize your events in folders (Event Handler).
Some bugs were found with the event handler and currently these 2 events cannot be used in the handler and still must be used in the main file: - bot.Status - bot.LoopCommand
MUST INCLUDE ALL THESE EVENT CALLBACKS IN THE MAIN FILE TO BE ABLE TO USE THEM IN THE HANDLER! (If not using an event in the bot, you do not need to define its callback) bot.onBan(); bot.onUnban(); bot.onBotJoin(); bot.onBotLeave(); bot.onChannelCreate(); bot.onChannelDelete(); bot.onEmojiCreate(); bot.onEmojiDelete(), bot.onGuildUpdate(); bot.onJoined(); bot.onLeave(); bot.onMessageDelete(); bot.onRateLimit(); bot.onRoleCreate(); bot.onRoleDelete(); bot.onUserUpdate();

Be sure your Executables and Awaited Commands do not share the same command name.

Step 1
Step 2
Step 3
Final Step
Insert the following code in your main file (server.js) or (index.js) etc.

Code:

1
const fs = require('fs') //remove if "fs" is already defined!
2
const events = fs.readdirSync("./events/");
3
for (const Files of events) {
4
const eventFile = fs.readdirSync(`./events/${Files}/`).filter(file => file.endsWith(".js"))
5
for (const event of eventFile) {
6
const obj = require(`./events/${Files}/${event}`);
7
if (typeof(obj) != "object") {
8
console.log("invalid module.exports data ...shutting down");
9
process.exit();
10
}
11
const eventName = Object.keys(obj);
12
const eventData = Object.values(obj)[0];
13
if (typeof(eventData) != "object") {
14
if (typeof(eventData) != "number") {
15
console.log("invalid event data ...shutting down");
16
process.exit();
17
}
18
}
19
bot[eventName](eventData);
20
if(bot.vars){
21
bot.Variables(eventData);
22
} if(bot.awaitedCommands){
23
bot.AwaitedCommand(eventData);
24
}
25
26
console.log(`Loaded Event: ${event}`);
27
}
28
}
Copied!
1: In the main directory, make a new folder called events .
2: Inside the eventsfolder, make another folder with a name of your choice. Normally a name that helps you organize your commands how you choose.
3: Inside that folder will be all the event command files related to that category.
The folder path should look something like this: Keep in mind though, I'm using Visual Studio Code (VSC) so your theme might look a little different than mine if you're using anything different but it's the exact same concept.
1
events/subfoldername/name.js
Copied!
Use the following format inside each command file:
You're replacing the event header name with module.exports.EVENTNAME = ({ in your command files.
1
module.exports.ReadyCommand = ({
2
name: "name or channel ID or variable",
3
code: `
4
Code Here
5
`
6
})
Copied!

You can now move all the events that you have in the main file, into their own respective files. It should look similar to the images in step 2 and step 3.

Include the event callbacks you are using into your main file under the bot.messageEvent() callback and below the handler. Best to add them all for less confusion later.

Last modified 5mo ago
Copy link