# Dynamic Validations

{% embed url="<https://youtu.be/_67r4wq23BA?t=723>" %}

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:

{% hint style="info" %}
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.
{% endhint %}

{% tabs %}
{% tab title="JavaScript" %}
{% code title="index.js" %}

```javascript
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);
```

{% endcode %}
{% endtab %}

{% tab title="TypeScript" %}
{% code title="index.ts" %}

```typescript
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);
```

{% endcode %}
{% endtab %}
{% endtabs %}

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:

![](/files/PBrG3O5gtwtjNlBuT7FV)

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wornoffkeys.com/events/dynamic-validations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
