Listen to an event
Events are used to listen for events emitted by multiple sources like discord client or zumito framework. This allows you to execute code when a specific event is emitted.
To start listening to an event, you just need to run the following command:
npx zumito-cli create eventIt will ask you for the name of the event, and the module where you want to create it.
After that, it will create a file in the src/modules/<module>/events folder with the name of the event.
The file will contain the following code:
import { FrameworkEvent, EventParameters } from 'zumito-framework';
export class EventName extends FrameworkEvent {
once = false; source = 'discord'
async execute({ interaction, client, framework, }: EventParameters): Promise<void> { // Event code }
}To start defining the event, you need to define the once property. This property defines if the event will be executed only once or every time the event is emitted.
Now, let’s define an example event for the interactionCreate event.
First, we will define the once property to false cause we want to execute the event every time the event is emitted:
once = false;Next, we will define the source property to discord cause we want to listen to a discord event. for other use cases we can listen to other sources like framework:
source = 'discord';Now, we need to define the execute function. This function will be executed every time the event is emitted.
The execute function receives an EventParameters object, which contains the following properties:
message- The message that triggered the event. Only available formessageCreateevent.interaction- The interaction that triggered the event. Only available forinteractionCreateevent.client- The Discord.js client.framework- The Zumito Framework instance.
Now, let’s define the event code:
async execute({ interaction, client, framework, }: EventParameters): Promise<void> { interaction.reply('Hello world!');}