Events

Events

Events are a way to trigger actions in your application. They are used to listen to changes in the application and respond to them.

Create an event

First, we will execute a command from the CLI of the Mineral framework.

mineral make:event <name>

Listening

An event listener is a function that listens to a specific event and triggers an action when the event is dispatched.

Basically, Mineral offers an event system that can be used in two different ways.

Functional approach

From the Client, we can chain event listeners using the following pattern:

client.events.<context>.<event>(<listener>)

Object-oriented approach

For benchmarking purposes, we recommend using the ‘functional’ approach, but when developing applications requiring more than 2 events or commands, we recommend using the object-oriented approach.

final class <ClassName> extends <Event> {
@override
FutureOr<void> handle(<params>) {}
}

Using classes allows you to organise your code better and also to use services contained within the IoC easily thanks to mixins like Logger.

final class Ready extends ReadyEvent with Logger {
@override
void handle(Bot bot) {
logger.info('${bot.username} is ready');
}
}

You can find the list of events in the API documentation.