Events Setup
This is a simple overview of how to set up a simple function that is called when the "ready" event is triggered. All this code does is log to the console when the bot is ready.
- CommonJS
- ESM
- TypeScript
module.exports = (c, client, handler) => {
console.log(`${c.user.username} is ready!`);
};
export default function (c, client, handler) {
console.log(`${c.user.username} is ready!`);
};
import type { Client } from 'discord.js';
import type { CommandKit } from 'commandkit';
export default function (c: Client<true>, client: Client<true>, handler: CommandKit) {
console.log(`${c.user.username} is ready!`);
};
Parameters explained
The parameters might look a bit confusing at first, but they're actually quite simple. The first parameter c
is the client object that was returned as a parameter when the "ready" event was triggered. The second parameter client
is the Discord.js client that was instantiated in your main entry point file. Finally, the handler
parameter is the current CommandKit instance.
To better understand how the parameters work, here's another example but with the "messageCreate" event listener.
- CommonJS
- ESM
- TypeScript
module.exports = (message, client) => {
if (message.content === 'hey') {
message.reply('Hi!');
}
};
export default function (message, client, handler) {
if (message.content === 'hey') {
message.reply('Hi!');
}
};
import type { Message, Client } from 'discord.js';
import type { CommandKit } from 'commandkit';
export default function (message: Message<true>, client: Client<true>, handler: CommandKit) {
if (message.content === 'hey') {
message.reply('Hi!');
}
};
In this example you can see that the first parameter is the message
object that was returned as a parameter when the "messageCreate" event was triggered. The client
parameter is the same as the previous example.
Multiple parameters explained
But what if an event returns multiple parameters? Let's take for example the "messageUpdate" event. This event returns two parameters, oldMessage
and newMessage
. The parameters will follow the same behaviour as if you were using the client.on()
method.
- CommonJS
- ESM
- TypeScript
module.exports = (oldMessage, newMessage, client) => {
console.log(`Message edited from ${oldMessage.content} to ${newMessage.content}`);
};
export default function (oldMessage, newMessage, client) {
console.log(`Message edited from ${oldMessage.content} to ${newMessage.content}`);
};
import type { Message, PartialMessage } from 'discord.js';
import type { CommandKit } from 'commandkit';
export default function (oldMessage: Message<boolean> | PartialMessage, newMessage: Message<boolean> | PartialMessage, client: Client<true>, handler: CommandKit) {
console.log(`Message edited from ${oldMessage.content} to ${newMessage.content}`);
};
As you can see, even with multiple parameters, the last parameter will always be the client
object.
Stopping the event loop
The code above is just a simple example of how to set up an event function. But what if you want to stop the next event function in line from running? This is where the return
statement comes in handy.
- CommonJS
- ESM
- TypeScript
module.exports = (message, client) => {
if (message.content === 'hey') {
message.reply('Hi!');
return true; // This stops the event loop
}
}
export default function (message, client) {
if (message.content === 'hey') {
message.reply('Hi!');
return true; // This stops the event loop
}
}
import type { Message, Client } from 'discord.js';
import type { CommandKit } from 'commandkit';
export default function (message: Message<true>, client: Client<true>, handler: CommandKit) {
if (message.content === 'hey') {
message.reply('Hi!');
return true; // This stops the event loop
}
}
You can return any truthy value from this function to stop the next event function from running.