Skip to content

Commit

Permalink
refactor: Modify discord.js links
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed May 6, 2024
1 parent 56cb0ce commit 1be63fe
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 109 deletions.
8 changes: 4 additions & 4 deletions guide/creating-your-bot/command-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ With the correct files identified, the last step is dynamically set each command

## Receiving command interactions

You will receive an interaction for every slash command executed. To respond to a command, you need to create a listener for the <DocsLink path="class/Client?scrollTo=e-interactionCreate" /> event that will execute code when your application receives an interaction. Place the code below in the `index.js` file you created earlier.
You will receive an interaction for every slash command executed. To respond to a command, you need to create a listener for the <DocsLink path="Client:Class#interactionCreate" /> event that will execute code when your application receives an interaction. Place the code below in the `index.js` file you created earlier.

```js
client.on(Events.InteractionCreate, interaction => {
console.log(interaction);
});
```

Not every interaction is a slash command (e.g. `MessageComponent` interactions). Make sure to only handle slash commands in this function by making use of the <DocsLink path="class/BaseInteraction?scrollTo=isChatInputCommand" type="method"/> method to exit the handler if another type is encountered. This method also provides typeguarding for TypeScript users, narrowing the type from `BaseInteraction` to <DocsLink path="class/ChatInputCommandInteraction" />.
Not every interaction is a slash command (e.g. `MessageComponent` interactions). Make sure to only handle slash commands in this function by making use of the <DocsLink path="BaseInteraction:Class#isChatInputCommand" type="method"/> method to exit the handler if another type is encountered. This method also provides typeguarding for TypeScript users, narrowing the type from `BaseInteraction` to <DocsLink path="ChatInputCommandInteraction:Class" />.

```js {2}
client.on(Events.InteractionCreate, interaction => {
Expand All @@ -88,7 +88,7 @@ client.on(Events.InteractionCreate, interaction => {

## Executing commands

When your bot receives a <DocsLink path="class/Client?scrollTo=e-interactionCreate" /> event, the interaction object contains all the information you need to dynamically retrieve and execute your commands!
When your bot receives a <DocsLink path="Client:Class#interactionCreate" /> event, the interaction object contains all the information you need to dynamically retrieve and execute your commands!

Let's take a look at the `ping` command again. Note the `execute()` function that will reply to the interaction with "Pong!".

Expand All @@ -103,7 +103,7 @@ module.exports = {
};
```

First, you need to get the matching command from the `client.commands` Collection based on the `interaction.commandName`. Your <DocsLink path="class/Client"/> instance is always available via `interaction.client`. If no matching command is found, log an error to the console and ignore the event.
First, you need to get the matching command from the `client.commands` Collection based on the `interaction.commandName`. Your <DocsLink path="Client:Class"/> instance is always available via `interaction.client`. If no matching command is found, log an error to the console and ignore the event.

With the right command identified, all that's left to do is call the command's `.execute()` method and pass in the `interaction` variable as its argument. In case something goes wrong, catch and log any error to the console.

Expand Down
6 changes: 3 additions & 3 deletions guide/creating-your-bot/event-handling.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Event handling

Node.js uses an event-driven architecture, making it possible to execute code when a specific event occurs. The discord.js library takes full advantage of this. You can visit the <DocsLink path="class/Client" /> documentation to see the full list of events.
Node.js uses an event-driven architecture, making it possible to execute code when a specific event occurs. The discord.js library takes full advantage of this. You can visit the <DocsLink path="Client:Class" /> documentation to see the full list of events.

::: tip
This page assumes you've followed the guide up to this point, and created your `index.js` and individual slash commands according to those pages.
Expand Down Expand Up @@ -43,7 +43,7 @@ client.on(Events.InteractionCreate, async interaction => {
:::
::::

Currently, the event listeners are in the `index.js` file. <DocsLink path="class/Client?scrollTo=e-ready" /> emits once when the `Client` becomes ready for use, and <DocsLink path="class/Client?scrollTo=e-interactionCreate" /> emits whenever an interaction is received. Moving the event listener code into individual files is simple, and we'll be taking a similar approach to the [command handler](/creating-your-bot/command-handling.md).
Currently, the event listeners are in the `index.js` file. <DocsLink path="Client:Class#ready" /> emits once when the `Client` becomes ready for use, and <DocsLink path="Client:Class#interactionCreate" /> emits whenever an interaction is received. Moving the event listener code into individual files is simple, and we'll be taking a similar approach to the [command handler](/creating-your-bot/command-handling.md).

::: warning
You're only going to move these two events from `index.js`. The code for [loading command files](/creating-your-bot/command-handling.html#loading-command-files) will stay here!
Expand Down Expand Up @@ -194,7 +194,7 @@ client.login(token);

You'll notice the code looks very similar to the command loading above it - read the files in the events folder and load each one individually.

The <DocsLink path="class/Client" /> class in discord.js extends the [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter) class. Therefore, the `client` object exposes the [`.on()`](https://nodejs.org/api/events.html#events_emitter_on_eventname_listener) and [`.once()`](https://nodejs.org/api/events.html#events_emitter_once_eventname_listener) methods that you can use to register event listeners. These methods take two arguments: the event name and a callback function. These are defined in your separate event files as `name` and `execute`.
The <DocsLink path="Client:Class" /> class in discord.js extends the [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter) class. Therefore, the `client` object exposes the [`.on()`](https://nodejs.org/api/events.html#events_emitter_on_eventname_listener) and [`.once()`](https://nodejs.org/api/events.html#events_emitter_once_eventname_listener) methods that you can use to register event listeners. These methods take two arguments: the event name and a callback function. These are defined in your separate event files as `name` and `execute`.

The callback function passed takes argument(s) returned by its respective event, collects them in an `args` array using the `...` [rest parameter syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), then calls `event.execute()` while passing in the `args` array using the `...` [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax). They are used here because different events in discord.js have different numbers of arguments. The rest parameter collects these variable number of arguments into a single array, and the spread syntax then takes these elements and passes them to the `execute` function.

Expand Down
4 changes: 2 additions & 2 deletions guide/interactions/context-menus.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ client.on(Events.InteractionCreate, interaction => {

## Extracting data from context menus

For user context menus, you can get the targeted user by accessing the `targetUser` or `targetMember` property from the <DocsLink path="class/UserContextMenuCommandInteraction" />.
For user context menus, you can get the targeted user by accessing the `targetUser` or `targetMember` property from the <DocsLink path="UserContextMenuCommandInteraction:Class" />.

For message context menus, you can get the targeted message by accessing the `targetMessage` property from the <DocsLink path="class/MessageContextMenuCommandInteraction" />.
For message context menus, you can get the targeted message by accessing the `targetMessage` property from the <DocsLink path="MessageContextMenuCommandInteraction:Class" />.

```js {4}
client.on(Events.InteractionCreate, interaction => {
Expand Down
22 changes: 11 additions & 11 deletions guide/interactions/modals.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ This page is a follow-up to the [interactions (slash commands) page](/slash-comm
Unlike message components, modals aren't strictly components themselves. They're a callback structure used to respond to interactions.

::: tip
You can have a maximum of five <DocsLink path="class/ActionRowBuilder" />s per modal builder, and one <DocsLink path="class/TextInputBuilder" /> within an <DocsLink path="class/ActionRowBuilder" />. Currently, you can only use <DocsLink path="class/TextInputBuilder" />s in modal action rows builders.
You can have a maximum of five <DocsLink path="ActionRowBuilder:Class" />s per modal builder, and one <DocsLink path="TextInputBuilder:Class" /> within an <DocsLink path="ActionRowBuilder:Class" />. Currently, you can only use <DocsLink path="TextInputBuilder:Class" />s in modal action rows builders.
:::

To create a modal you construct a new <DocsLink path="class/ModalBuilder" />. You can then use the setters to add the custom id and title.
To create a modal you construct a new <DocsLink path="ModalBuilder:Class" />. You can then use the setters to add the custom id and title.

```js {1,7-13}
const { Events, ModalBuilder } = require('discord.js');
Expand All @@ -37,10 +37,10 @@ The custom id is a developer-defined string of up to 100 characters. Use this fi

The next step is to add the input fields in which users responding can enter free-text. Adding inputs is similar to adding components to messages.

At the end, we then call <DocsLink path="class/ChatInputCommandInteraction?scrollTo=showModal" type="method"/> to display the modal to the user.
At the end, we then call <DocsLink path="ChatInputCommandInteraction:Class#showModal" type="method"/> to display the modal to the user.

::: warning
If you're using typescript you'll need to specify the type of components your action row holds. This can be done by specifying the generic parameter in <DocsLink path="class/ActionRowBuilder" />
If you're using typescript you'll need to specify the type of components your action row holds. This can be done by specifying the generic parameter in <DocsLink path="ActionRowBuilder:Class" />

```diff
- new ActionRowBuilder()
Expand Down Expand Up @@ -106,7 +106,7 @@ Currently there are two different input styles available:

### Input properties

In addition to the `customId`, `label` and `style`, a text input can be customised in a number of ways to apply validation, prompt the user, or set default values via the <DocsLink path="class/TextInputBuilder" /> methods:
In addition to the `customId`, `label` and `style`, a text input can be customised in a number of ways to apply validation, prompt the user, or set default values via the <DocsLink path="TextInputBuilder:Class" /> methods:

```js
const input = new TextInputBuilder()
Expand All @@ -126,13 +126,13 @@ const input = new TextInputBuilder()

### Interaction collectors

Modal submissions can be collected within the scope of the interaction that showed it by utilising an <DocsLink path="class/InteractionCollector"/>, or the <DocsLink path="class/ChatInputCommandInteraction?scrollTo=awaitModalSubmit" type="method"/> promisified method. These both provide instances of the <DocsLink path="class/ModalSubmitInteraction"/> class as collected items.
Modal submissions can be collected within the scope of the interaction that showed it by utilising an <DocsLink path="InteractionCollector:Class"/>, or the <DocsLink path="ChatInputCommandInteraction:Class#awaitModalSubmit" type="method"/> promisified method. These both provide instances of the <DocsLink path="ModalSubmitInteraction:Class"/> class as collected items.

For a detailed guide on receiving message components via collectors, please refer to the [collectors guide](/popular-topics/collectors.md#interaction-collectors).

### The interactionCreate event

To receive a <DocsLink path="class/ModalSubmitInteraction"/> event, attach an <DocsLink path="class/Client?scrollTo=e-interactionCreate"/> event listener to your client and use the <DocsLink path="class/BaseInteraction?scrollTo=isModalSubmit" type="method"/> type guard to make sure you only receive modals:
To receive a <DocsLink path="ModalSubmitInteraction:Class"/> event, attach an <DocsLink path="Client:Class#interactionCreate"/> event listener to your client and use the <DocsLink path="BaseInteraction:Class#isModalSubmit" type="method"/> type guard to make sure you only receive modals:

```js {1,4}
client.on(Events.InteractionCreate, interaction => {
Expand All @@ -143,15 +143,15 @@ client.on(Events.InteractionCreate, interaction => {

## Responding to modal submissions

The <DocsLink path="class/ModalSubmitInteraction"/> class provides the same methods as the <DocsLink path="class/ChatInputCommandInteraction"/> class. These methods behave equally:
The <DocsLink path="ModalSubmitInteraction:Class"/> class provides the same methods as the <DocsLink path="ChatInputCommandInteraction:Class"/> class. These methods behave equally:
- `reply()`
- `editReply()`
- `deferReply()`
- `fetchReply()`
- `deleteReply()`
- `followUp()`

If the modal was shown from a <DocsLink path="class/ButtonInteraction"/> or <DocsLink path="class/StringSelectMenuInteraction"/>, it will also provide these methods, which behave equally:
If the modal was shown from a <DocsLink path="ButtonInteraction:Class"/> or <DocsLink path="StringSelectMenuInteraction:Class"/>, it will also provide these methods, which behave equally:
- `update()`
- `deferUpdate()`

Expand All @@ -165,12 +165,12 @@ client.on(Events.InteractionCreate, async interaction => {
```

::: tip
If you're using typescript, you can use the <DocsLink path="class/ModalSubmitInteraction?scrollTo=isFromMessage" type="method"/> typeguard, to make sure the received interaction was from a `MessageComponentInteraction`.
If you're using typescript, you can use the <DocsLink path="ModalSubmitInteraction:Class#isFromMessage" type="method"/> typeguard, to make sure the received interaction was from a `MessageComponentInteraction`.
:::

## Extracting data from modal submissions

You'll most likely need to read the data sent by the user in the modal. You can do this by accessing the <DocsLink path="class/ModalSubmitInteraction?scrollTo=fields"/>. From there you can call <DocsLink path="class/ModalSubmitFields?scrollTo=getTextInputValue" type="method"/> with the custom id of the text input to get the value.
You'll most likely need to read the data sent by the user in the modal. You can do this by accessing the <DocsLink path="ModalSubmitInteraction:Class#fields"/>. From there you can call <DocsLink path="ModalSubmitFields:Class#getTextInputValue" type="method"/> with the custom id of the text input to get the value.

```js {5-7}
client.on(Events.InteractionCreate, interaction => {
Expand Down
4 changes: 2 additions & 2 deletions guide/message-components/action-rows.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ If you're using TypeScript, you'll need to specify the type of components your a

## Sending action rows

Once one or many components are inside your row(s), send them in the `components` property of your <DocsLink path="typedef/InteractionReplyOptions" /> (extends <DocsLink path="typedef/BaseMessageOptions" />).
Once one or many components are inside your row(s), send them in the `components` property of your <DocsLink path="InteractionReplyOptions:Interface" /> (extends <DocsLink path="BaseMessageOptions:Interface" />).

```js {4}
const row = new ActionRowBuilder()
Expand All @@ -37,4 +37,4 @@ const row = new ActionRowBuilder()
await interaction.reply({ components: [row] });
```

To learn how to create the buttons and select menus that will go inside your row, including more detailed examples on how you might use them, continue on to the other pages in this section.
To learn how to create the buttons and select menus that will go inside your row, including more detailed examples on how you might use them, continue on to the other pages in this section.
4 changes: 2 additions & 2 deletions guide/message-components/buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The custom id is a developer-defined string of up to 100 characters. Use this fi
## Sending buttons
To send your buttons, create an action row and add the buttons as components. Then, send the row in the `components` property of <DocsLink path="typedef/InteractionReplyOptions" /> (extends <DocsLink path="typedef/BaseMessageOptions" />).
To send your buttons, create an action row and add the buttons as components. Then, send the row in the `components` property of <DocsLink path="InteractionReplyOptions:Interface" /> (extends <DocsLink path="BaseMessageOptions:Interface" />).
```js {1,19-20,24}
const { ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandBuilder } = require('discord.js');
Expand Down Expand Up @@ -157,7 +157,7 @@ const button = new ButtonBuilder()
## Emoji buttons
If you want to use a guild emoji within a <DocsLink path="class/ButtonBuilder"/>, you can use the <DocsLink path="class/ButtonBuilder?scrollTo=setEmoji" type="method"/> method:
If you want to use a guild emoji within a <DocsLink path="ButtonBuilder:Class"/>, you can use the <DocsLink path="ButtonBuilder:Class#setEmoji" type="method"/> method:
```js {5}
const button = new ButtonBuilder()
Expand Down
Loading

0 comments on commit 1be63fe

Please sign in to comment.