# 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:

![](https://848057878-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MY8r5FJ7sEk7fNghuKr%2Fuploads%2F2OjO6o1hJBSYNuBHDEHe%2Fimage.png?alt=media\&token=c288487e-4460-47f4-895d-0fa8bd04176b)

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 %}
