Files
discordeno/site/docs/nodejs/EventHandler/handle-event.md
meister03 b7b2120a6f docs(site, template): add Nodejs Bot Guide (#1998)
* Inital Docs

* Finish Design

* Finish Design

* Add FILE Structure'

* Add Not Ready Template

* Update Template

* Add Command Manager and first Command ping

* Add Command Manager and first Command ping

* Add Embeds Guide & EventManager

* INITIAL FINISH: GUIDE

* FIX TYPOS, LTS's Review, sidebar_position

* Update site/docs/nodejs/getting-started.md

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>

* Fixed Position? Add some comments on template, add changes in review

* Add Interaction Handling, Modals

* format files

* Update site/docs/nodejs/getting-started.md

1

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>

* Update site/docs/nodejs/CommandHandler/command-manager.md

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>

* Fix Review things

* itoh's suggestions

The guide looks very very veery good overall I like it. This commit adds my personal style of writing.
Please review it 🙏.

PS: I hope these are not too many changes .-.

* deno fmt

Co-authored-by: meister03 <root@_HOSTNAME_>
Co-authored-by: ITOH <to@itoh.at>
Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
2022-02-15 13:54:05 +00: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, its 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 = new Message(client, payload);

  if (message.isBot) 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 = new Interaction(client, 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 = new User(client, payload.user);

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