Event Handling

Event Handling

Similar to the commands, reciple also provides built-in modules for handling events.

Structure

You can use the EventModule class to handle events of any event emitter. The EventModule class is a subclass of BaseModule. This module requires the following properties:

  • emitter — The event emitter to listen to.
  • event — The event to listen to.

To execute the event, you can use the onEvent method. The onEvent method takes the parameters of the event.

import { EventModule } from 'reciple';

class ExampleModule extends EventModule {
    emitter = process;
    event = 'warning';

    onEvent(warning) {
        useLogger().warn(warning);
    }
}

export default new ExampleModule();

Client Events

You can use the ClientEventModule class to handle client events. This module class is a subclass of EventModule. This module automatically uses the client as the event emitter, means that you don’t need to set the emitter property. This module requires the following properties:

  • event — The event to listen to.

An onEvent method is required to handle the event.

import { ClientEventModule } from 'reciple';

class ExampleModule extends ClientEventModule {
    event = 'messageCreate';

    onEvent(message) {
        if (message.content === 'hello') {
            message.reply('world');
        }
    }
}

export default new ExampleModule();
hello hello world

REST Events

Similar to the ClientEventModule, you can use the RESTEventModule class to handle rest events. This module also automatically uses the rest client as the event emitter, means that you don’t need to set the emitter property. This module requires the following properties:

  • event — The event to listen to.

An onEvent method is required to handle the event.

import { RESTEventModule } from 'reciple';

class ExampleModule extends RESTEventModule {
    event = 'rateLimited';

    onEvent() {
        useLogger().warn('Rate limited');
    }
}

export default new ExampleModule();