Skip to main content

Custom Event

aoitelegram is a powerful module for creating Telegram bots based on the aoijs syntax. Consequently, aoitelegram hasn't overlooked the creation of custom events. Now, events can be created using both aoitelegram and js syntax, similar to creating functions.

How to Use

const { AoiClient, CustomEvent } = require("aoitelegram");

const bot = new AoiClient({
token: "",
});

const event = new CustomEvent(bot);

// Attaching a handler to the event can be done using a method in the main file, but it's recommended to use loadEvents to load the handlers
event.command({
listen: "eventName",
once: true,
// type: "aoitelegram" (default)
code: `
$print[$eventData]
$print[$eventData[1]] // index args
`
})

event.command({
listen: "eventName",
once: true,
type: "js",
callback: (...args) => {
console.log(args);
}
})

event.emit("eventName", 1, 2, 3, 4, 5, 6, 7);

// or
event.loadEvents("./events/", true);
note

To trigger an event within aoitelegram code, you need to use the function $emitData[nameEvent;...args]

note

Also, besides the events created by you, aoitelegram@0.8.0 and above supports events such as: process:beforeExit, process:SIGTERM, process:SIGINT, process:worker, process:warning, process:unhandledRejection, process:uncaughtExceptionMonitor, process:uncaughtException, process:rejectionHandled, process:multipleResolves, process:message, process:exit, process:disconnect

Example of Creation

export default {
listen: "eventName",
type: "js",
callback: (...args) => {
const math = args.map((arg) => arg / 2);
console.log(math);
},
};