XpEvents.on()
Register callback handlers for logs and level events.
All callback properties are optional, and each callback can return either void or Promise<void>.
levelUp and levelDown are emitted by addXP(), removeXP(), setXP(), addLevel(), removeLevel(), and setLevel() whenever a user's level changes.
Use this before connect() if you want to intercept startup logs as well.
Parametersβ
| Parameter | Type | Required | Description |
|---|---|---|---|
| XpEventCallback | XpEventCallback | β | The callback to run when the event is emitted. |
Examplesβ
const { XpEvents } = require("simply-xp");
XpEvents.on({
debug: (xpFunction, message) => {
// Handle debug event
},
error: (xpFunction, message) => {
// Handle error event
},
info: (xpFunction, message) => {
// Handle info event
},
custom: (message) => {
// Handle your own custom event payloads
},
levelDown: (data, lostRoles) => {
// Handle levelDown event
},
levelUp: (data, newRoles) => {
// Handle levelUp event
},
warn: (xpFunction, message) => {
// Handle warn event
},
});
XpEvents.add()
Register callbacks without replacing the ones already registered.
XpEvents.on() holds a single callback object β calling it again replaces whatever was there before,
including handlers registered by a plugin or by another part of your project. XpEvents.add() appends
instead, so any number of listeners can receive the same events.
Always use add() in a plugin. Calling on() would silently delete the project author's own handlers.
Parametersβ
| Parameter | Type | Required | Description |
|---|---|---|---|
| XpEventCallback | XpEventCallback | β | The callbacks to run when the events are emitted. |
Returnsβ
() => void- Call it to remove these callbacks again.
Throwsβ
XpFatal- If the callbacks argument is not an object.
Exampleβ
const { XpEvents } = require("simply-xp");
const removeListener = XpEvents.add({
levelUp: (data, newRoles) => {
// Runs alongside every other registered listener
},
});
// Later, when you no longer need it:
removeListener();