Setting up Events

A simple way to setup your discord.js events. All you need to do is set the property name as the event name, and then pass in the parameters needed for the events in the execute function, and set up the rest of the events functionality in it. You can name the event.js file as anything you want. Here is a sample ready event in which I will name it readyEvent.js:

readyEvent.js
module.exports = {
    name: "ready", // name of the discord.js event
    once: true, // if this event should run once only
    async execute(client) { // when the event is invoked we execute this function
      console.log(`${client.user.tag}`,"Ready!");
    },
  };

Properties

name

This is the name of the discord client event. You can find the full list of discord event names in the official Discord.js Documentation - Client Constructor

Type: String

once

If this discord event should run once or whenever it is called. You would only need this property if you are using the ready event or creating a slash event.

Type: Boolean

async execute(client, ...args)

This is the method used to execute and handle the discord event trigger. The first argument is always the Discord Client object. The following arguments must be the parameters from the event that you trying to trigger. For example, if the event is the guildMemberUpdate, then the execute arguments would look like this:

It is optional to make the execute function, an asynchronous function

execute(client, oldMember, newMember) { 
    // do something
},

returns;

Last updated