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:
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
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 theready
event, the argument is the client object. However if you're listening to another event such asmessageCreate
, 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.jsClient
instance which was defined in the main entry point of your project.handler
: This is the DJS-CommanderCommandHandler
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:
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:
Last updated