mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-01 00:10:07 +00:00
chore: apps/site/ > site/
This commit is contained in:
4
site/tutorial/nodejs/EventHandler/_category_.json
Normal file
4
site/tutorial/nodejs/EventHandler/_category_.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"label": "Event Handler",
|
||||
"position": 8
|
||||
}
|
||||
119
site/tutorial/nodejs/EventHandler/event-manager.md
Normal file
119
site/tutorial/nodejs/EventHandler/event-manager.md
Normal file
@@ -0,0 +1,119 @@
|
||||
---
|
||||
sidebar_position: 2
|
||||
---
|
||||
|
||||
# Create Event Manager
|
||||
|
||||
In order to process certain events, you must provide the Discordeno client with functions for these events.
|
||||
|
||||
```js
|
||||
const Discord = require("discordeno");
|
||||
const config = require("./config.json");
|
||||
|
||||
const client = Discord.createBot({
|
||||
events: {
|
||||
ready(client, payload) {
|
||||
console.log(`Successfully connected Shard ${payload.shardId} to the gateway`);
|
||||
},
|
||||
|
||||
async messageCreate(client, message) {
|
||||
if (message.content === "!ping") {
|
||||
await client.helpers.sendMessage(message.channelId, { content: "pong" });
|
||||
}
|
||||
|
||||
console.log(`Received message: ${message.content || message.embeds}`);
|
||||
},
|
||||
},
|
||||
intents: ["Guilds", "GuildMessages"],
|
||||
token: config.token,
|
||||
});
|
||||
|
||||
Discord.startBot(client);
|
||||
```
|
||||
|
||||
As you listen to more and more events, the functions code grows along with them, so you can quickly lose track.
|
||||
|
||||
To avoid this, we recommend storing the event functions divided into files in a separate folder.
|
||||
|
||||
## Create Event Folder
|
||||
|
||||
Create a folder called `events` in your project folder.
|
||||
|
||||
:::info note
|
||||
|
||||
The event files have to be named using camelCase so that they can be understood by the client. e.g `message` ->
|
||||
`messageCreate.js`. You can check the typings see how the events are called.
|
||||
|
||||
:::
|
||||
|
||||
Ready Event:
|
||||
|
||||
```js
|
||||
module.exports = (client, payload) => {
|
||||
if (payload.shardId + 1 === client.gateway.maxShards) {
|
||||
// All Shards are ready
|
||||
console.log(`Successfully connected to the gateway as ${payload.user.username}#${payload.user.discriminator}`);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## Load your Events
|
||||
|
||||
```js
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const resolveFolder = (folderName) => path.resolve(__dirname, ".", folderName);
|
||||
|
||||
class EventManager {
|
||||
constructor(client) {
|
||||
this.cache = new Map();
|
||||
this._events = {};
|
||||
}
|
||||
|
||||
load(options = {}) {
|
||||
const eventsFolder = resolveFolder("../events");
|
||||
fs.readdirSync(eventsFolder).map(async (file) => {
|
||||
if (!file.endsWith(".js")) return;
|
||||
|
||||
const fileName = path.join(eventsFolder, file);
|
||||
const event = require(fileName);
|
||||
const eventName = file.split(".")[0];
|
||||
|
||||
this._events[`${eventName}`] = event;
|
||||
});
|
||||
|
||||
return this._events;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EventManager;
|
||||
```
|
||||
|
||||
The code above, which can also be found in the
|
||||
[template repo](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Managers/EventManager.js) will loop
|
||||
through all the files in the `events` folder and load the functions into the `_events` object.
|
||||
|
||||
In order to let the client know which events should be processed, you need to pass the functions in the
|
||||
`createBot<options>.events` object.
|
||||
|
||||
```js
|
||||
const Discord = require("discordeno");
|
||||
const config = require("./config.json");
|
||||
|
||||
const EventManager = require("./Managers/EventManager.js");
|
||||
const events = new EventManager({});
|
||||
|
||||
const client = Discord.createBot({
|
||||
events: events.load({}),
|
||||
intents: ["Guilds", "GuildMessages"],
|
||||
token: config.token,
|
||||
});
|
||||
|
||||
Discord.startBot(client);
|
||||
```
|
||||
|
||||
Moreover, you can customize the `EventManager` and add more functionality to it and make it exactly fit your needs or
|
||||
even emit events, by extending it.
|
||||
|
||||
Of course you wonder what you can do with all of this now. We will explain this further on the next page.
|
||||
29
site/tutorial/nodejs/EventHandler/getting-started.md
Normal file
29
site/tutorial/nodejs/EventHandler/getting-started.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
sidebar_position: 1
|
||||
---
|
||||
|
||||
# Getting Started with the Event Handler
|
||||
|
||||
An event handler is essential to process the data, which Discord sends to you.
|
||||
|
||||
With a good implementation, you will have a nice code structure and thus have a good overview in long term.
|
||||
|
||||
Since the `EventEmitter` class is commonly used you probably already know it from other libraries.
|
||||
|
||||
Discordeno decided against it as it comes with several downsides which are mentioned below.
|
||||
|
||||
- It's easy to create memory leaks, when you add too many listeners or go carelessly with it.
|
||||
- Many fragmented parts of event code complicate maintenance.
|
||||
- ErrorHandling is difficult and debugging is harder when many listeners are open for the same events.
|
||||
|
||||
Performance plays a more important role than handling, however this event management system can be easily implemented
|
||||
since it only needs a few changes in your code.
|
||||
|
||||
In the following we will show you, how to create an event manager, which is compatible with Discordeno's Client.
|
||||
|
||||
:::info template
|
||||
|
||||
You can also copy the
|
||||
[`EventManager` from the template repo](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Managers/EventManager.js).
|
||||
|
||||
:::
|
||||
75
site/tutorial/nodejs/EventHandler/handle-event.md
Normal file
75
site/tutorial/nodejs/EventHandler/handle-event.md
Normal file
@@ -0,0 +1,75 @@
|
||||
---
|
||||
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](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Structures)
|
||||
|
||||
:::
|
||||
|
||||
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`.
|
||||
|
||||
```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`.
|
||||
|
||||
```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.
|
||||
|
||||
```js
|
||||
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}`);
|
||||
}
|
||||
};
|
||||
```
|
||||
Reference in New Issue
Block a user