-
-
Notifications
You must be signed in to change notification settings - Fork 1
The Event System
Events are emitted when certain things happen, whether that be any of the Revolt events or a Voltare event like the logger
event. Along with the given properties of an event, Voltare provides a ClientEvent
object as the first parameter for every event handler. From the object, you can get and set variables that are apart of that particular event. Event handlers are fired in an order based on the before
and after
s of each listener.
These are the Revolt events that Voltare emits:
export interface VoltareClientEvents {
error(error: any): void;
ready(): void;
connecting(): void;
connected(): void;
disconnected(): void;
packet(packet: ClientboundNotification): void;
message(message: Message): void;
messageUpdate(message: Message): void;
messageDelete(messageId: string): void;
channelCreate(channel: Channel): void;
channelUpdate(packet: ChannelUpdatePacket): void;
channelDelete(channelId: string): void;
groupUserJoin(serverId: string, userId: string): void;
groupUserLeave(serverId: string, userId: string): void;
serverUpdate(packet: ServerUpdatePacket): void;
serverDelete(serverId: string): void;
serverMemberJoin(serverId: string, user: string): void;
serverMemberUpdate(packet: ServerMemberUpdatePacket): void;
serverMemberLeave(serverId: string, user: string): void;
serverRoleUpdate(packet: ServerRoleUpdatePacket): void;
serverRoleDelete(serverId: string, roleId: string): void;
userUpdate(packet: UserUpdatePacket): void;
userRelationshipUpdate(packet: UserRelationshipUpdate): void;
typingStart(channelId: string, userId: string): void;
typingStop(channelId: string, userId: string): void;
}
Aside from Revolt's events, Voltare has its own set of events it uses internally.
-
logger
- An event specialized for custom loggers to use.-
level: string
- What level the log is in. Technically this can be anything, but it is usuallydebug
,info
,warn
, anderror
. -
group: string
- What module/group this log is representing. -
args: any[]
- The arguments of the log. -
extra?: LoggerExtra
- The extra data of the log.
-
-
beforeConnect
- This event is fired before connecting to Discord. -
beforeDisconnect
- This event is fired before disconnecting from Discord.-
reconnect: boolean | 'auto'
- The "reconnect" argument being sent to Eris.
-
-
afterConnect
- This event is fired after connecting to Discord. -
atferDisconnect
- This event is fired after disconnecting from Discord.-
reconnect: boolean | 'auto'
- The "reconnect" argument being sent to Eris.
-
You can create listeners within a module, or outside of one. However all listeners are associated with a group name. If you are creating listeners in a module, then the group name would be your module name.
/** in a load() function... */
this.registerEvent(
'messageCreate', // event name
(event, message) => { // event handler, you can also bind functions within a module
// ...
},
{
// What listeners to be fired BEFORE this one.
before: ['moduleName', ...]
// What listeners to be fired AFTER this one.
after: ['moduleName', ...]
}
);
/** outside of a module... */
client.events.register(
'test', // group name
'messageCreate', // event name
(event, message) => { // event handler
// ...
},
{
// What listeners to be fired BEFORE this one.
before: ['moduleName', ...]
// What listeners to be fired AFTER this one.
after: ['moduleName', ...]
}
);
ClientEvent
objects are used for every event in Voltare and is used to store variables across the listeners. If there is a listener that checks for certain variables, you can set them and set that listener to be fired after
yours. You can also do vice versa, and check for variables that a listener will set later and make sure that that listener is fired before
yours.
/** inside the event handler function... */
// Make sure a specific variable exists...
event.has('example/variable') // -> true/false
// Get a variable...
event.get('example/prefix') // -> can be ANYTHING
// Set a variable...
event.set('example/ran', true)
For TypeScript devs:
event.get
returnsany
, so make sure to set what you expect withevent.get('...') as [thing]
.
To make sure variables do not clash with each other, you can prefix all of you variables with the name of the module and a slash. Example:
example/variable
You can also completely cancel a handler by skipping it. Make sure that the handler that you want to skip is set to be "fired" after
yours.
event.skip('moduleName')