# Event Handling

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

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:

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

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

{% endcode %}
{% endtab %}

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

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

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

### 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](https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-messageCreate)" event:

![](/files/JhfX9GABqu6NrgOh22Bp)

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:

{% tabs %}
{% tab title="JavaScript" %}
{% code title="log-messages.js" %}

```javascript
module.exports = (message, instance) => {
  console.log(message.content);
};
```

{% endcode %}
{% endtab %}

{% tab title="TypeScript" %}
{% code title="log-messages.ts" %}

```typescript
import { Message } from "discord.js";
import WOK from "wokcommands";

export default (message: Message, instance: WOK) => {
  console.log(message.content);
};
```

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


---

# 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/what-is-a-feature.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.
