DEV Community

Gilles Heinesch
Gilles Heinesch

Posted on • Updated on • Originally published at Medium

A Good Event Handler For Your First Discord.JS Bot

Why Do I need This?

Maybe you ask yourself, why do I even need an event handler. That's quite simple to answer. It makes it much simpler for you push an update for an event and fix bugs. You don't have to have all events in the starter javascript file any more. This means that your launcher (starter) file has much less code without all these events. You can create a file for each event you want to have.

const fs = require('fs'); // fs is the package we need to read all files which are in folders
Enter fullscreen mode Exit fullscreen mode

What Benefits Does The Following Event Handler Offer?

  • You can create a file for each event you want to have (All events here)
  • Much clearer overview of all events and easy to find bugs and add new features to your events
  • Main launcher file has much less code

Let's Start With Programming

First of all we need a package named fs to read all files which are situated in folders. For this package, we don't need to install anything because this package is included in the Node.JS Bundle. You can read more about this here (This link is also the documentation of fs ).

In this line we require the package fs.

const fs = require('fs'); // fs is the package we need to read all files which are in folders

fs.readdir('./events/', (err, files) => { // We use the method readdir to read what is in the events folder.
    // Code
});
Enter fullscreen mode Exit fullscreen mode

As you can see, we immediately start using our fs package. For our purposes, we use the method readdir() (If you want to read more about this method, you can do this here).
In the third line, we use the readdir() method to read the contents of the ./events directory with a callback of 2 arguments (err (error) and files (files which are in this folder)).
Our current folder structure does look like this:

- main.js
    - events
Enter fullscreen mode Exit fullscreen mode

The first new line (the 4th line) we have added, simply checks if there was an error during checking all contents of the ./events folder. If yes, throw an error in the console with the error message to know what went wrong.
The next new line needs the files argument which we got from the readdir() function we used to check all contents of the ./events directory.

files is an array of the names of the files in the directory excluding '.' and '..' .

This means, that we can use the forEach() function to go through every single event file with the callback file .

In this new line (6th line), we simply require the event file from the events folder. This returns us all about informations about the single event file.

In the first line we added, we check if the eventFunction variable is disabled. If yes, return without throwing any error.
Then we have 3 new variables. The first one defines the name of the event by checking if the eventFunction has a property event with the name of the event. If not, the variable returns the name of the event file. Example: For the ready.jsfile, the event variable would be ready .
The next new variable defines the emitter. This is the "executor" or "engine" of the event. In our case, this is the client (the Discord bot).
The last variable which is new, is once . This variable simply returns the once property of the eventFunction . This property determines if this event should run once.

Next, we use a try catch block. If there is an error during the execution of the code in the try{} block, it immediately throws an error with the error stack ( error.stack )
In the try{} block, the emitter (client) runs the event, using all above defined settings.

Conclusion

This event handler can really help you to better organize your files of your Discord bot. If you have any questions, you can visit our forum for further support!

Top comments (0)