mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +00:00
Discordeno.js guide (#2313)
* Update Template and Add Discordeno.js notes in favour of depreciating Discord Structures * Add package.json * deno fmt Co-authored-by: meister03 <meisterpi@gmail.com> Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com> Co-authored-by: LTS20050703 <lts20050703@gmail.com>
This commit is contained in:
co-authored by
meister03
LTS20050703
LTS20050703
parent
b5105cbc32
commit
405e4a7533
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"label": "Command Handler",
|
||||
"position": 7
|
||||
"position": 9
|
||||
}
|
||||
|
||||
@@ -7,11 +7,11 @@ sidebar_position: 2
|
||||
Currently, you probably have something like this in your code:
|
||||
|
||||
```js
|
||||
const Discord = require("discordeno");
|
||||
const Discord = require("discordeno.js");
|
||||
// Ideally you should move to an `.env` file
|
||||
const config = require("./config.json");
|
||||
|
||||
const client = Discord.createBot({
|
||||
const bot = Discord.createBot({
|
||||
events: {
|
||||
messageCreate(client, message) {
|
||||
if (message.content === "!ping") {
|
||||
@@ -19,9 +19,10 @@ const client = Discord.createBot({
|
||||
}
|
||||
},
|
||||
},
|
||||
intents: ["Guilds", "GuildMessages"],
|
||||
intents: Discord.Intents.Guilds | Discord.Intents.GuildMessages,
|
||||
token: config.token,
|
||||
});
|
||||
const client = Discord.enableCachePlugin(bot, {});
|
||||
|
||||
Discord.startBot(client);
|
||||
```
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"label": "Event Handler",
|
||||
"position": 6
|
||||
"position": 8
|
||||
}
|
||||
|
||||
@@ -27,9 +27,9 @@ This file should be called `messageCreate.js`.
|
||||
const Message = require("./structures/Message");
|
||||
|
||||
module.exports = async (client, payload) => {
|
||||
const message = new Message(client, payload);
|
||||
const message = client.messages.forge(payload);
|
||||
|
||||
if (message.isBot) return;
|
||||
if (message.author.bot) return;
|
||||
if (message.content === "!ping") return await message.reply("pong");
|
||||
};
|
||||
```
|
||||
@@ -42,7 +42,7 @@ This file should be called `interactionCreate.js`.
|
||||
const Interaction = require("./structures/Interaction");
|
||||
|
||||
module.exports = async (client, payload) => {
|
||||
const interaction = new Interaction(client, payload);
|
||||
const interaction = client.interactions.forge(payload);
|
||||
|
||||
if (interaction.data.name === "ping") return await interaction.reply({ content: "pong" });
|
||||
};
|
||||
@@ -65,7 +65,7 @@ In order to fire the "real event" a small code snippet has to be added to the `r
|
||||
const User = require("../Structures/User");
|
||||
|
||||
module.exports = async (client, payload) => {
|
||||
client.user = new User(client, payload.user);
|
||||
client.user = client.users.forge(payload.user);
|
||||
|
||||
if (payload.shardId + 1 === client.gateway.maxShards) {
|
||||
// All Shards are ready
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"label": "Structures",
|
||||
"position": 5
|
||||
"position": 7
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
sidebar_position: 5
|
||||
---
|
||||
|
||||
# Create Collectors
|
||||
|
||||
Some of your commands or features are sometimes based on user interactions. E.g. if a user presses a button and you want
|
||||
to know whether it was pressed. This is actually done by listening to the `interactionCreate` event.
|
||||
|
||||
But sometimes you need to access locale variables or don't want to "hardcode" the part.
|
||||
|
||||
That's why it's sometimes recommended to create collectors.
|
||||
|
||||
Collectors are listeners that listen to a specific event. In addition, you can provide a filter, so you only receive
|
||||
certain interactions.
|
||||
|
||||
## Use a Collector
|
||||
|
||||
:::note Template The template code is used below. You must have the EventManager part to use the collector feature. :::
|
||||
|
||||
We have a pre-made class for collectors which you can find
|
||||
[here](https://github.com/meister03/discordeno.js/blob/master/Util/Collectors.js).
|
||||
|
||||
```js
|
||||
const Discord = require("discordeno.js");
|
||||
const filter = (m) => m.data?.customId === "warn_modal" && m.user.id === interaction.user.id;
|
||||
const listener = client.eventListener; // When the eventListener property is named different
|
||||
const collector = new Discord.Collector("interactionCreate", {
|
||||
client: client,
|
||||
timeout: 60000,
|
||||
filter,
|
||||
max: 20,
|
||||
listener,
|
||||
});
|
||||
collector.on("collect", (m) => {
|
||||
const interaction = client.interactions.forge(m);
|
||||
// Stop Collector
|
||||
// collector.stop();
|
||||
});
|
||||
|
||||
// Fires on a timeout, when the collector has reached the max amount of interactions or when it has been closed
|
||||
collector.on("end", (collected) => {
|
||||
// Map of Collected Interactions
|
||||
console.log(collected);
|
||||
});
|
||||
```
|
||||
|
||||
As you can see, this opens up many possibilities. You can listen to any event and get the interaction you need.
|
||||
|
||||
### Collector Options
|
||||
|
||||
`filter`: Function, just fire the event if the filter returns true. `timeout`: Number, the time in milliseconds until
|
||||
the collector times out. `max`: Number, the max amount of interactions the collector can collect. `listener`: Function,
|
||||
the listener that will be fired when the collector collects an interaction. Just required when client property is named
|
||||
differently.
|
||||
@@ -19,7 +19,7 @@ just using raw data because they still execute methods, which takes more time to
|
||||
:::
|
||||
|
||||
We already have a Template for `Components`, which can be found
|
||||
[here](https://github.com/discordeno/discordeno/tree/main/template/nodejs/structures/Component.js).
|
||||
[here](https://github.com/meister03/discordeno.js/tree/master/Structures/Component.js).
|
||||
|
||||
## Different Components:
|
||||
|
||||
@@ -102,12 +102,15 @@ This code will obviously not work because it's a missing a lot required of data.
|
||||
a class to Discord, we need sth. to transform it to a json object.
|
||||
|
||||
We have a pre-made class for components which you can find
|
||||
[here](https://github.com/discordeno/discordeno/tree/main/template/nodejs/structures/Component.js).
|
||||
[here](https://github.com/meister03/discordeno.js/tree/master/Structures/Component.js).
|
||||
|
||||
### Button
|
||||
|
||||
```js
|
||||
const button = new Component()
|
||||
const Discord = require("discordeno.js");
|
||||
const message = client.messages.forge(rawMessage);
|
||||
|
||||
const button = new Discord.Component()
|
||||
.setType("BUTTON")
|
||||
.setStyle("LINK")
|
||||
.setLabel("Click me!")
|
||||
@@ -115,14 +118,14 @@ const button = new Component()
|
||||
.toJSON();
|
||||
|
||||
// Button with raw types
|
||||
const button2 = new Component()
|
||||
const button2 = new Discord.Component()
|
||||
.setType(2)
|
||||
.setStyle(4)
|
||||
.setLabel("DO NOT CLICK")
|
||||
.setCustomId("12345")
|
||||
.toJSON();
|
||||
|
||||
const actionRow = new Component()
|
||||
const actionRow = new Discord.Component()
|
||||
.setType("ACTION_ROW")
|
||||
.setComponents(button, button2)
|
||||
.toJSON();
|
||||
@@ -130,7 +133,8 @@ const actionRow = new Component()
|
||||
// Message to send
|
||||
const messageOptions = { content: "hello", components: [actionRow] };
|
||||
|
||||
await client.helpers.sendMessage(channelId, messageOptions); // You can also use the Message Structure
|
||||
// await client.helpers.sendMessage(channelId, messageOptions); // Do it the raw way
|
||||
message.channel.send(messageOptions); // Do it with the structure
|
||||
```
|
||||
|
||||
As you can see, for simplicity you can use strings instead of numbers (types), which are hard to remember.
|
||||
@@ -138,7 +142,10 @@ As you can see, for simplicity you can use strings instead of numbers (types), w
|
||||
### Select Menu
|
||||
|
||||
```js
|
||||
const selectMenu = new Component()
|
||||
const Discord = require("discordeno.js");
|
||||
const message = client.messages.forge(rawMessage);
|
||||
|
||||
const selectMenu = new Discord.Component()
|
||||
.setType("SELECT_MENU")
|
||||
.setCustomId("12345")
|
||||
.setOptions([
|
||||
@@ -162,19 +169,23 @@ const selectMenu = new Component()
|
||||
.setPlaceholder("Select an option")
|
||||
.toJSON();
|
||||
|
||||
const actionRow = new Component()
|
||||
const actionRow = new Discord.Component()
|
||||
.setType("ACTION_ROW")
|
||||
.setComponents(selectMenu)
|
||||
.toJSON();
|
||||
|
||||
const messageOptions = { content: "hello", components: [actionRow] };
|
||||
|
||||
client.helpers.sendMessage(channelId, messageOptions); // You can also use the Message Structure
|
||||
// await client.helpers.sendMessage(channelId, messageOptions); // Do it the raw way
|
||||
message.channel.send(messageOptions); // Do it with the structure
|
||||
```
|
||||
|
||||
### Text Input
|
||||
|
||||
```js
|
||||
const Discord = require("discordeno.js");
|
||||
const interaction = client.messages.forge(rawInteraction);
|
||||
|
||||
const textInput = new Component()
|
||||
.setType("TEXT_INPUT")
|
||||
.setStyle("SHORT")
|
||||
@@ -199,7 +210,7 @@ const textInput2 = new Component()
|
||||
const actionRow = new Component().setType("ACTION_ROW").setComponents(textInput).toJSON();
|
||||
const actionRow2 = new Component().setType("ACTION_ROW").setComponents(textInput2).toJSON();
|
||||
|
||||
new Interaction(client, interaction).popupModal({
|
||||
interaction.popupModal({
|
||||
customId: "ban_modal",
|
||||
title: "Ban User",
|
||||
components: [actionRow, actionRow2],
|
||||
@@ -210,10 +221,3 @@ new Interaction(client, interaction).popupModal({
|
||||
|
||||
When a user clicks a button or selects an option from a Select Menu, Discord sends an `interactionCreate` event, which
|
||||
contains the information necessary to process it.
|
||||
|
||||
:::note Collecting
|
||||
|
||||
An `InteractionCollector` can also be used to handle prompts, which requires some tweaks, but will be added soon in the
|
||||
guide and the template repo.
|
||||
|
||||
:::
|
||||
|
||||
@@ -52,28 +52,49 @@ the data, how the methods are named and how you want to process the request.
|
||||
|
||||
## Using Template Structures:
|
||||
|
||||
When you are migrating from another library, you'll likely choose to continue using special structures. Therefore why we
|
||||
have ready-made structures in our template repo:
|
||||
When you are migrating from another library and you want to utilize the djs-like wrapper, you'll likely choose to
|
||||
continue using special structures. Therefore we have ready-made structures for the wrapper `Discordeno.js`.
|
||||
|
||||
- [Guild](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Structures/Guild.js)
|
||||
- [Channel](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Structures/Channel.js)
|
||||
- [Role](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Structures/Role.js)
|
||||
- [Member](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Structures/Member.js)
|
||||
- [User](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Structures/User.js)
|
||||
- [Message](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Structures/Message.js)
|
||||
- [Interaction](https://github.com/discordeno/discordeno/tree/main/template/nodejs/Structures/Interaction.js)
|
||||
- [Guild](https://github.com/meister03/discordeno.js/tree/master/Structures/Guild.js)
|
||||
- [Channel](https://github.com/meister03/discordeno.js/tree/master/Structures/Channel.js)
|
||||
- [Role](https://github.com/meister03/discordeno.js/tree/master/Structures/Role.js)
|
||||
- [Member](https://github.com/meister03/discordeno.js/tree/master/Structures/Member.js)
|
||||
- [User](https://github.com/meister03/discordeno.js/tree/master/Structures/User.js)
|
||||
- [Message](https://github.com/meister03/discordeno.js/tree/master/Structures/Message.js)
|
||||
- [Interaction](https://github.com/meister03/discordeno.js/tree/master/Structures/Interaction.js)
|
||||
- [Emoji](https://github.com/meister03/discordeno.js/tree/master/Structures/Emoji.js)
|
||||
- [Webhook](https://github.com/meister03/discordeno.js/tree/master/Structures/Webhook.js)
|
||||
- [Embed](https://github.com/meister03/discordeno.js/tree/master/Structures/Embed.js)
|
||||
- [Component](https://github.com/meister03/discordeno.js/tree/master/Structures/Component.js)
|
||||
- [Collection](https://github.com/meister03/discordeno.js/tree/master/Structures/Collection.js)
|
||||
|
||||
We recommend that you clone the whole template repo, since some structures are based on other files.
|
||||
We recommend that you check the wrappers [Readme](https://github.com/meister03/discordeno.js#discordclient) in order to
|
||||
construct the client for following the Guide
|
||||
|
||||
**Using the Structures:**
|
||||
|
||||
```js
|
||||
const Guild = require("./structures/Guild"); // Path to your structure
|
||||
const guild = new Guild(client, data); // DiscordenoClient and DiscordenoPayloadData
|
||||
const Discord = require("discordeno.js");
|
||||
const client = new Discord.Client(clientOptions, cacheOptions); //See the Readme above
|
||||
Discord.startBot(client);
|
||||
const guild = client.guilds.forge(guildData);
|
||||
const channel = guild.channels.forge(channelData);
|
||||
const role = guild.roles.forge(roleData);
|
||||
const member = guild.members.forge(memberData);
|
||||
const user = guild.users.forge(userData);
|
||||
const message = guild.messages.forge(messageData);
|
||||
const interaction = guild.interactions.forge(interactionData);
|
||||
const emoji = guild.emojis.forge(emojiData);
|
||||
|
||||
const webhook = new Discord.Webhook(client, webhookData);
|
||||
const embed = new Discord.Embed(embedData); // embedData is optional
|
||||
const component = new Discord.Component(componentData); // componentData is optional
|
||||
const collection = new Discord.Collection();
|
||||
```
|
||||
|
||||
Some popular methods have been added to the structures so that you can use them without having to come up with your own.
|
||||
Of course, you can add your own methods and customize the structures to fit your needs.
|
||||
In order to use the Structures from the Wrapper, you need to invoke the `.forge` method with the raw discord data,
|
||||
whereas it will construct the structure for you.
|
||||
|
||||
Next we're going to give a better insight into how create [`Embeds`](embeds) and [`Components`](components) with the
|
||||
template structures.
|
||||
wrappers structures.
|
||||
|
||||
@@ -57,16 +57,15 @@ await channel.send({ embeds: [embed] });
|
||||
```
|
||||
|
||||
You probably want more methods which you can use to create embeds.
|
||||
[We also have a Template for this](https://github.com/discordeno/discordeno/tree/main/template/nodejs/structures/Embed.js)
|
||||
[Here is how the Embed Structure looks like](https://github.com/meister03/discordeno.js/blob/master/Structures/Embed.js)
|
||||
|
||||
### Using the Embed Structure:
|
||||
|
||||
```js
|
||||
const Embed = require("./structures/Embed"); // Path to structure
|
||||
const Channel = require("./structures/Channel"); // Path to structure
|
||||
const Discord = require("discordeno.js");
|
||||
|
||||
const channel = new Channel(client, data);
|
||||
const showCaseEmbed = new Embed()
|
||||
const channel = client.channels.forge(channelData);
|
||||
const showCaseEmbed = new Discord.Embed()
|
||||
.setColor(0x00AE86)
|
||||
.setTitle("A Random Title")
|
||||
.setURL("https://github.com/discordeno")
|
||||
|
||||
@@ -13,6 +13,9 @@ different language.
|
||||
|
||||
This guide will help you making your first Discord Bot using Node.js or even migrating your Bot from a other Library.
|
||||
|
||||
Moreover this guide will utilize two different options. One option to use the Discordeno package without any frameworks
|
||||
and one, which uses the wrapper called `Discordeno.js`, which aims to achieve a djs-like interface.
|
||||
|
||||
:::important Disclaimer
|
||||
|
||||
Some features are not documented yet. If you want to know more about them, kindly ask for help in the
|
||||
|
||||
@@ -22,7 +22,8 @@ In order to keep track of the dependencies, you need to initialize NPM, which ge
|
||||
$ npm init --yes
|
||||
```
|
||||
|
||||
Then you need to install Discordeno. Go to your terminal and run the following command:
|
||||
Then you need to install Discordeno. When you want to go along with the wrapper named `Discordeno.js`, then install it
|
||||
too. Go to your terminal and run the following command:
|
||||
|
||||
```cli
|
||||
$ npm install discordeno
|
||||
|
||||
@@ -53,7 +53,7 @@ const client = Discord.createBot({
|
||||
}
|
||||
},
|
||||
},
|
||||
intents: ["Guilds"],
|
||||
intents: Discord.Intents.Guilds | Discord.Intents.GuildMessages,
|
||||
token: config.token,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user