Skip to content

Custom Events

Written by


aoi.js has a built-in way to create Custom Events, these can be used for various things. This guide will be covering their usage, how they work, and an example.

1
const { CustomEvent } = require("aoi.js");
1
const event = new CustomEvent(client); // Creates a new instance of the CustomEvent class, passing the "client" as an argument.
2
3
event.command({
4
listen: "eventName", // This should represent the name of the event, in this case it is "eventName".
5
code: `
6
code
7
` // The code of the event that will be triggered once the event is emitted.
8
});
9
10
event.listen("eventName"); // This will make the client "listen" or in other words, wait for the event to be triggered. It does not trigger the event yet.

To emit a previously created custom event you have to options.

You can use the native node.js method by using emit:

1
EventEmitter.emit(eventName, ...args);

On the other side you have the aoi.js method with functions:

$eventEmit[eventName, ...args]

It would basically do the same as the node.js method.


1
const { AoiClient, LoadCommands, CustomEvent } = require("aoi.js");
2
3
const client = AoiClient({
4
token: "DISCORD BOT TOKEN",
5
prefix: "DISCORD BOT PREFIX",
6
intents: ["MessageContent", "GuildMembers", "GuildMessages", "Guilds"],
7
events: ["onMessage", "onInteractionCreate"],
8
database: {
9
type: "aoi.db",
10
db: require("aoi.db"),
11
tables: ["main"],
12
path: "./database/",
13
extraOptions: {
14
dbType: "KeyValue"
15
}
16
}
17
});
18
19
const event = new CustomEvent(client);
20
21
event.command({
22
listen: "log",
23
code: `$log[Hello!] `
24
});
25
26
event.listen("log");

Inside of aoi.js command files or aoi.js related code you can use the $eventEmit & $eventData functions to interact with created events.

1
$eventEmit[log];

Would do the same as

1
event.emit("log");

Both emit the created log event, which causes Hello! to appear in your console.