Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Add new Events

ap4gh edited this page Apr 10, 2019 · 2 revisions

Event refers to an entity that gets triggered when some action happens in the server. Eg. member joins server, member gets banned etc. All events resides in the /events/ directory. Lets create a simple trigger for a client(bot) event that gets executed whenever a new channel is created on any server. All the client events can be found here.

STEP 1 - Write an Event

First decide which event you want to work on, each event has its own directory. Here we are going to work with message event. So lets create a file named Hi.js inside /events/message/ directory. Any file name prefixed with Bot. is a client specific event handler file. If your event is bot specific, prefix your file name with Bot eg. Bot.Hi.js.

Every event is a constructor and needs to be initialised. Create a base class for your event in this file.

Hi.js 👇

const BaseEvent = require('../BaseEvent');

class Hi extends BaseEvent {
  async handle(message) {
    if (message.content.toLowerCase() === 'hi') message.reply('hello');
  }
}

module.exports = Hi;

The handle function will handle your execution code. Each event throws an object when it is triggered, in this case it is message object. Passing this object to handle function will help you utilise it.

STEP 2 - Registering Event

After writing this event handler, you must register it in /configurations/events.js file. Under the event name, add the file name in the associated array. /configurations/events.js 👇

module.exports = {
  message: ['Bot.SharedCommandHandler', 'Hi']
};

Note: Do not add .js extension in the file name.