How to use events

Events are one of the most important features of the package as they allow you to perform certain actions when something happens. For example, let's say you want a message to be sent when a user levels up, well, with the levelUp event you can do this easily. However, if you don't want to use it, it's also possible as the function addXp indicates if the user has leveled up.

The package comes with 7 events, which you can see in the Events section. Putting that aside, let's see how to use them.


The manager has an option called eventPath, which indicates the path of the folder where the events are located. The manager can automatically load events (including discord.js events), but for this to work, you must follow certain rules, which are:

  • The folder can only contain .js files. If there are subfolders within the main folder, they will not be read.
  • The name can be anything, but it is recommended to use a descriptive name.
  • The file must export an object with the following structure:
  const { LevelManagerEvents } = require('discord-xplus')
  module.exports = {
    eventType: 'name of the event' ,
    // Also you can use LevelManagerEvents
    // eventType: LevelManagerEvents.LevelUp,

    // args are the arguments that the event will receive
    run: (client, ...args) => {
      // code
    }
  }
  • eventType: It's the name of the event. You can use either the name of the event or the event itself, but it is recommended to use the name of the event so that if the event name changes, you won't have to change it in all your events.

  • run: It's the function that will be executed when the event is emitted. ...args are the arguments that the event will receive. These arguments depend on the event, but the documentation of each event specifies which arguments it receives.


The structure of your project should be as follows:

  my-project/
  ├── index.js
  └── src/
      └── events/
          ├── levelUpMessage.js
          └── bypassMessage.js

Events

The available events are:

  • managerReady: Emitted when the manager is ready.
  • levelUp: Emitted when a user levels up.
  • xpAdded: Emitted when XP is added to a user.
  • levelDown: Emitted when a user levels down.
  • bypass: Emitted when a user levels up with a command or event, without adding XP.
  • rankUp: Emitted when a user ranks up.
  • rankDown: Emitted when a user ranks down.