Files
discordeno/site/tutorial/nodejs/EventHandler/handle-event.md
Jonathan Ho 987b4058cb Site: update site structure and add architecture page (#2675)
* remove blog

* fix: license

* chore: bump docusaurus

* separate benchmark page

* move to sub folder

* fix blog path

* Chore: update site

* fix: only deploy on push and dispatch

* fix: on:

* ci: merge workflow

* ci: fix if

* refactor: dump preset
2022-12-04 08:36:10 -06:00

1.9 KiB

sidebar_position
sidebar_position
3

Handle Events

When an event is fired, Discordeno sends two important things: the client instance and the payload.

As mentioned in the Structure section, the payload object does not contain any functions as it's a plain json object.

In order to take use of our nice built structures, we need to transform the payload into a structure.

:::info

The Structures can be found here

:::

Sometimes it's important to listen to events, in order to get informed of changes and updating the cache based on it.

Message Event

This file should be called messageCreate.js.

const Message = require("./structures/Message");

module.exports = async (client, payload) => {
  const message = client.messages.forge(payload);

  if (message.author.bot) return;
  if (message.content === "!ping") return await message.reply("pong");
};

Interaction Event

This file should be called interactionCreate.js.

const Interaction = require("./structures/Interaction");

module.exports = async (client, payload) => {
  const interaction = client.interactions.forge(payload);

  if (interaction.data.name === "ping") return await interaction.reply({ content: "pong" });
};

Ready Event

This file should be called ready.js.

:::tip

There is a small difference with the ready Event. The Event is fired shard wise, in other words it fires every time a shard becomes ready.

:::

In order to fire the "real event" a small code snippet has to be added to the ready Event.

const User = require("../Structures/User");

module.exports = async (client, payload) => {
  client.user = client.users.forge(payload.user);

  if (payload.shardId + 1 === client.gateway.maxShards) {
    // All Shards are ready
    console.log(`Successfully connected to the gateway as ${client.user.tag}`);
  }
};