Creating Events

To create event listeners, you can create a folder inside of your defined events directory with the name of the event you want your bot to subscribe to.

DJS-Commander assumes a specific file structure for your events. For example, let's say I want to listen to the ready event, which is triggered when the bot comes online and is ready. I'd create a folder structure that looks something like this:

events/
└── ready/

The folder name inside the events folder must exactly match the event you want your bot to subscribe to (casing matters).

Functions inside these folders are executed in ascending order so you can prioritize your events however you see fit.

The most common thing to do when our bot is ready is to log to the console that it's online and ready for use! We can achieve that by creating a file inside the ready folder that exports a function by default. Here's an example, I'm going to name my file console-log.js

events/ready/console-log.js
module.exports = (argument, client, handler) => {
  console.log(`${client.user.tag} is online.`);
};

Now if you try taking your bot online, you should see the message above logged to the console. Let's go over the different parameters we have access to in this function:

  • argument: This is the argument(s) you receive when the event you're listening to is triggered. In the case of the ready event, the argument is the client object. However if you're listening to another event such as messageCreate, the argument will be the message object. Here's an awesome cheatsheet which shows you the list of all events and their resulting arguments.

  • client: This is your Discord.js Client instance which was defined in the main entry point of your project.

  • handler: This is the DJS-Commander CommandHandler instance which was also defined in the main entry point of your project.

For certain events such as channelUpdate, there can be multiple arguments that are received when the event is triggered. Remember that the client and handler parameters are pushed forward depending on the number of arguments received from the event.

Here's an example of the channelUpdate event listener:

events/channelUpdate/test.js
module.exports = (oldChannel, newChannel, client, handler) => {
  // this event function provides 2 parameters (oldChannel & newChannel) causing client and handler parameters to change positions.
};

Since these parameters are not being destructured like with commands, you can feel free to name them whatever you like.

You can also conditionally stop the execution of the next event function lined up by returning a truthy value from the exported function. Like this:

events/messageCreate/delete-blacklist.js
// the goal of this function is to delete a blacklisted word using the messageCreate event listener
// once the message is deleted, running the next function in line doesn't make sense since you can't interact with a deleted message
// to avoid that from happening you can return a truthy value from this function

module.exports = async (message, client, handler) => {
    if (message.content.includes('blacklisted_word')) {
        await message.delete();
        return true // this will ensure the next event function isn't executed
    }
};

Last updated