WOKCommands
Search
⌃K

Dynamic Validations

This is a system to only run specific event files when a specific condition is met. For example you might only want to listen for the "messageCreate" event whenever a human sent a message and not a bot. You can use dynamic validations to achieve this without adding complexity to your code.
Here is how you setup dynamic validations:
The below dynamic validations are the default ones for WOKCommands. You do not need to implement these manually, but you can always add in your own.
JavaScript
TypeScript
index.js
const {
Client,
IntentsBitField,
InteractionType,
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"),
​
// Dynamic validations
// IMPORTANT: These are the default dynamic validations that come with WOKCommands
// no need to implement them manually
​
// The name of the Discord.JS event
interactionCreate: {
// The name of your dynamic validation folder
// This has no correlation to the Discord.JS event name
// This will look for a direct parent folder called "isButton" and only
// fire interactionCreate events in that folder if this method returns true.
// In this case you should put button events in the "interactionCreate/isButton" folder
isButton: (interaction) => interaction.isButton(),
​
// In this case you should put slash command events in the "interactionCreate/isCommand" folder
isCommand: (interaction) =>
interaction.type === InteractionType.ApplicationCommand,
​
// In this case you should put auto complete events in the "interactionCreate/isAutocomplete" folder
isAutocomplete: (interaction) =>
interaction.type === InteractionType.ApplicationCommandAutocomplete,
},
messageCreate: {
isHuman: (message) => !message.author.bot,
},
},
});
});
​
client.login(process.env.TOKEN);
index.ts
import {
Client,
IntentsBitField,
Interaction,
InteractionType,
Message,
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"),
​
// Dynamic validations
// IMPORTANT: These are the default dynamic validations that come with WOKCommands
// no need to implement them manually
​
// The name of the Discord.JS event
interactionCreate: {
// The name of your dynamic validation folder
// This has no correlation to the Discord.JS event name
// This will look for a direct parent folder called "isButton" and only
// fire interactionCreate events in that folder if this method returns true.
// In this case you should put button events in the "interactionCreate/isButton" folder
isButton: (interaction: Interaction) => interaction.isButton(),
​
// In this case you should put slash command events in the "interactionCreate/isCommand" folder
isCommand: (interaction: Interaction) =>
interaction.type === InteractionType.ApplicationCommand,
​
// In this case you should put auto complete events in the "interactionCreate/isAutocomplete" folder
isAutocomplete: (interaction: Interaction) =>
interaction.type === InteractionType.ApplicationCommandAutocomplete,
},
messageCreate: {
isHuman: (message: Message) => !message.author.bot,
},
},
});
});
​
client.login(process.env.TOKEN);
As mentioned in the comments, you can specify a Discord.JS event name and a name for your dynamic validation. Your name can be anything, it doesn't have any correlation with Discord.JS method names at all.
The direct parent of your event files should match the dynamic validation name. In the example of a "log-messages" event file where we only want to log messages from humans and not bots:
​
​
The "isHuman" dynamic validation must return true for the "log-messages" event handler to be ran. As you can see from our index file, the "isHuman" method returns true or false depending on if the message author is a bot or a human.