Listening to Events
Updating Seyfert’s Configuration
Before starting this section, you need to update the seyfert.config.mjs
file to specify the location of the event files for Seyfert.
Listening to Events
Each event file must export the createEvent
function as the default export so Seyfert can load it.
The createEvent
function takes an object with two properties: data
and run
.
Let’s listen to the botReady
event as the first example:
import { function createEvent<E extends ClientNameEvents | CustomEventsKeys>(data: { data: { name: E; once?: boolean; }; run: (...args: ResolveEventParams<E>) => any;}): { data: { name: E; once?: boolean; }; run: (...args: ResolveEventParams<E>) => any;}
Creates an event with the specified data and run function.
createEvent } from 'seyfert';
export default createEvent<"botReady">(data: { data: { name: "botReady"; once?: boolean; }; run: (args_0: ClientUser, args_1: UsingClient, args_2: number) => any;}): { data: { name: "botReady"; once?: boolean; }; run: (args_0: ClientUser, args_1: UsingClient, args_2: number) => any;}
Creates an event with the specified data and run function.
createEvent({// botReady is triggered when all shards and servers are ready.// `once` ensures the event runs only once. data: { name: "botReady"; once?: boolean;}
data: { once?: boolean
once: true, name: "botReady"
name: 'botReady' }, run: (args_0: ClientUser, args_1: UsingClient, args_2: number) => any
run(user: ClientUser
user, client: UsingClient
client) {
client: UsingClient
client.BaseClient.logger: Logger
logger.Logger.info(...args: any[]): void
Logs an info message.
info(`${user: ClientUser
user.username: string
username} is ready`);
}})
As a second example, let’s look at the guildDelete
event emitted by Discord when a bot is removed from a server or the server is deleted:
import { function createEvent<E extends ClientNameEvents | CustomEventsKeys>(data: { data: { name: E; once?: boolean; }; run: (...args: ResolveEventParams<E>) => any;}): { data: { name: E; once?: boolean; }; run: (...args: ResolveEventParams<E>) => any;}
Creates an event with the specified data and run function.
createEvent } from 'seyfert';
export default createEvent<"guildDelete">(data: { data: { name: "guildDelete"; once?: boolean; }; run: (args_0: APIUnavailableGuild | Guild<"cached">, args_1: UsingClient, args_2: number) => any;}): { ...;}
Creates an event with the specified data and run function.
createEvent({ data: { name: "guildDelete"; once?: boolean;}
data: { name: "guildDelete"
name: 'guildDelete' }, run: (args_0: APIUnavailableGuild | Guild<"cached">, args_1: UsingClient, args_2: number) => any
run(unguild: APIUnavailableGuild | Guild<"cached">
unguild, client: UsingClient
client) { // unguild is the server from which the bot was removed or deleted. // It is also possible that the server was simply deleted. if (unguild: APIUnavailableGuild | Guild<"cached">
unguild.unavailable?: boolean | undefined
true
if this guild is unavailable due to an outage
unavailable) return;
client: UsingClient
client.BaseClient.logger: Logger
logger.Logger.info(...args: any[]): void
Logs an info message.
info(`I was removed from: ${unguild: APIUnavailableGuild | Guild<"cached">
unguild.APIUnavailableGuild.id: string
Guild id
id}`); }})
After completing these steps, your project structure should look like this:
Directorysrc
Directorycommands
- ping.ts
Directoryevents
- botReady.ts
- guildDelete.ts
- index.ts
- package.json
- seyfert.config.mjs
- tsconfig.json
- .env