WOKCommands
Search
⌃K

Event Handling

WOKCommands can automatically handle your event listening in multiple files and folders. You can even setup "dynamic validations" to only run some events when a specific condition is met. More on that in the next section of the documentation.
Here is an example of how to setup a basic event listening system:
JavaScript
TypeScript
index.js
const { Client, IntentsBitField, Partials } = require("discord.js");
const path = require("path");
const WOK = require("wokcommands");
require("dotenv/config");
​
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.DirectMessages,
IntentsBitField.Flags.MessageContent,
],
partials: [Partials.Channel],
});
​
client.on("ready", () => {
console.log("The bot is ready");
​
new WOK({
client,
mongoUri: process.env.MONGO_URI || "",
commandsDir: path.join(__dirname, "commands"),
events: {
// Where the events are stored
dir: path.join(__dirname, "events"),
},
});
});
​
client.login(process.env.TOKEN);
index.ts
import { Client, IntentsBitField, Partials } from "discord.js";
import path from "path";
import WOK from "wokcommands";
require("dotenv/config");
​
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.DirectMessages,
IntentsBitField.Flags.MessageContent,
],
partials: [Partials.Channel],
});
​
client.on("ready", () => {
console.log("The bot is ready");
​
new WOK({
client,
mongoUri: process.env.MONGO_URI || "",
commandsDir: path.join(__dirname, "commands"),
events: {
// Where the events are stored
dir: path.join(__dirname, "events"),
},
});
});
​
client.login(process.env.TOKEN);

File/Folder Structure

Inside of your "events" folder should be nested folders that directly match the event you want to listen to. An example of the "messageCreate" event:
​
​
All files inside of the "messageCreate" folder will be ran when the event is emitted. The function inside of your event files should have all of the normal parameters for the event, plus an additional "WOKCommands" instance parameter at the end:
JavaScript
TypeScript
log-messages.js
module.exports = (message, instance) => {
console.log(message.content);
};
log-messages.ts
import { Message } from "discord.js";
import WOK from "wokcommands";
​
export default (message: Message, instance: WOK) => {
console.log(message.content);
};