Custom Events
Table of Content
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.
CustomEvent Class
1const { CustomEvent } = require("aoi.js");
1const event = new CustomEvent(client); // Creates a new instance of the CustomEvent class, passing the "client" as an argument.2
3event.command({4 listen: "eventName", // This should represent the name of the event, in this case it is "eventName".5 code: `6 code7 ` // The code of the event that will be triggered once the event is emitted.8});9
10event.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.
Emitting Custom Events
To emit a previously created custom event you have to options.
Node.js / Discord.js Method
You can use the native node.js
method by using emit
:
1EventEmitter.emit(eventName, ...args);
Aoi.js Function Method
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.
Example Usage of Custom Events
1const { AoiClient, LoadCommands, CustomEvent } = require("aoi.js");2
3const 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
19const event = new CustomEvent(client);20
21event.command({22 listen: "log",23 code: `$log[Hello!] `24});25
26event.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
1event.emit("log");
Both emit the created log
event, which causes Hello!
to appear in your console.