Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 627fa27

Browse files
committed
Discord-Modals v1.3.0: Add ModalActionRow class, fix typings and add support for JSON Modals.
1 parent b1a0ee4 commit 627fa27

15 files changed

+247
-84
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
Hello! This is the Change log section of Discord-Modals. Here you can see what things have been changed in the different versions, to be informed. This will be updated every time there are new versions. I hope you enjoy this package :)
44

5+
# v1.3.0 (Stable)
6+
7+
- Fix bug: 'INVALID_CLIENT' error when the Client is valid on `.showModal()` method.
8+
- **Featured:** Add `ModalActionRow` class.
9+
- Now the `TextInputComponent` returns an **Action Row Component**. Add `ModalActionRow` class and types.
10+
- **Featured:** Now the `.showModal()` method supports **JSON Modals**.
11+
- Fix error typings `ModalSubmitField`.
12+
- **Featured:** Implement `.update()` method to `ModalSubmitInteraction` class.
13+
- Fix types: `.showModal()` method, `.addComponents()` and `.setComponents()` of `Modal` class.
14+
- Typings: `discord-api-types` updated to **v10**.
15+
516
# v1.2.9 (Stable)
617

718
- Fix: Add types for reply options on `ModalSubmitInteraction` class.

DOCS.md

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Shows the Modal to the Interaction User.
4242

4343
## Modal
4444
Represents a Modal Form to be shown in response to an Interaction.
45-
- A modal can contain at most **5 Text Input Components**
45+
- A modal can contain at most **5 Action Rows**. Each of them can contain **1 Text Input Component**.
4646

4747
```javascript
4848
new Modal(data)
@@ -69,7 +69,7 @@ The Custom Id of the Modal.
6969
7070
#### .components
7171
The Text Input Components of the Modal.
72-
> Returns: [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
72+
> Returns: [Array<TextInputComponent>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
7373
7474
### Methods
7575

@@ -96,7 +96,7 @@ Sets the Custom Id of the Modal. (Max. 100 characters)
9696
> Returns: [Modal](#modal)
9797
9898
```js
99-
.addComponents(TextInputComponent)
99+
.addComponents(...TextInputComponent)
100100
```
101101
Adds the Components of the Modal.
102102

@@ -107,7 +107,7 @@ Adds the Components of the Modal.
107107
> Returns: [Modal](#modal)
108108
109109
```js
110-
.setComponents(TextInputComponent)
110+
.setComponents(...TextInputComponent)
111111
```
112112
Sets the Components of the Modal.
113113

@@ -150,6 +150,7 @@ new TextInputComponent(data)
150150
| `.required` | `.setRequired()` |
151151
| `.style` | `.setStyle()` |
152152
| `.value` | `.setDefaultValue()` |
153+
| | `.toJSON()` |
153154

154155
### Properties
155156

@@ -275,13 +276,71 @@ Sets a Default/Pre-filled Value of the Text Input Component. (Max. 4000 characte
275276

276277
> Returns: [TextInputComponent](#textinputcomponent)
277278
279+
```js
280+
.toJSON()
281+
```
282+
Transforms the TextInputComponent to a plain object.
283+
284+
> Returns: [APITextInputComponent](https://discord.com/developers/docs/interactions/message-components#text-inputs)
285+
278286
### TextInputStyle
279287

280288
| Text |Value|Description|
281289
| ------------ | ------------ |------------ |
282290
| SHORT |1 [[Short]](https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-styles)| A single-line input |
283291
| LONG |2 [[Paragraph]](https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-styles)| A multi-line input |
284292

293+
## ModalActionRow
294+
Represents a Modal Action Row, that contains a Text Input Component.
295+
296+
| Properties |Methods|
297+
| ------------ | ------------ |
298+
| `.type` | `.addComponent()` |
299+
| `.components` | `.componentToJSON()` |
300+
| | `.toJSON()` |
301+
302+
### Properties
303+
304+
#### .type
305+
The type of the Modal Action Row (1).
306+
> Returns: [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)
307+
308+
#### .components
309+
The Text Input Component of this Action Row.
310+
> Returns: [Array<APITextInputComponent>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
311+
312+
### Methods
313+
314+
```js
315+
.addComponent(TextInputComponent)
316+
```
317+
Adds a Text Input Component.
318+
319+
| Parameter |Type|Description|
320+
| ------------ | ------------ |------------ |
321+
| *TextInputComponent* |[TextInputComponent](#textinputcomponent)| The Text Input Component to add in the Action Row. |
322+
323+
> Returns: [ModalActionRow](#modalactionrow)
324+
325+
```js
326+
.componentToJSON(TextInputComponent)
327+
```
328+
Transforms a Text Input Component to a plain object.
329+
330+
| Parameter |Type|Description|
331+
| ------------ | ------------ |------------ |
332+
| *TextInputComponent* |[TextInputComponent](#textinputcomponent)| The Text Input Component to transform. |
333+
334+
> Returns: [APITextInputComponent](https://discord.com/developers/docs/interactions/message-components#text-inputs)
335+
336+
```js
337+
.toJSON()
338+
```
339+
Transforms the Modal Action Row to a plain object.
340+
341+
> Returns: [APIModalActionRowComponent<APITextInputComponent>](https://discord.com/developers/docs/interactions/message-components#text-inputs)
342+
343+
285344
## ModalSubmitField
286345
Represents a Field of a Modal Submit Interaction.
287346

@@ -320,9 +379,9 @@ Represents a Modal Submit Interaction.
320379
| `.guildId` | `.editReply()` |
321380
| `.user` | `.deleteReply()` |
322381
| `.member` | `.followUp()` |
323-
| `.memberPermissions` | `.isFromMessage()` |
324-
| `.locale` | `.isRepliable()` |
325-
| `.guildLocale` | |
382+
| `.memberPermissions` | `.update()` |
383+
| `.locale` | `.isFromMessage()` |
384+
| `.guildLocale` | `.isRepliable()` |
326385
| `.message` | |
327386
| `.version` | |
328387
| `.webhook` | |
@@ -445,6 +504,7 @@ Discord-Modals integrates some errors to avoid issues and respect the structures
445504
| `TEXT_INPUT_VALUE` | TextInputComponent value must be a string. |
446505
| `MODAL_CUSTOM_ID` | Modal customId must be a string. |
447506
| `MODAL_TITLE` | Modal title must be a string. |
507+
| `MODAL_INTERACTION_MESSAGE` | This Modal Submit Interaction does not have a message to update. |
448508

449509

450510
# 🔨 Do you want to contribute to Discord-Modals?

index.d.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
APITextInputComponent,
44
APIModalInteractionResponseCallbackData,
55
APIModalSubmitInteraction,
6-
} from "discord-api-types/v9";
6+
APIModalActionRowComponent,
7+
} from "discord-api-types/v10";
78
import {
89
Client,
910
User,
@@ -16,6 +17,7 @@ import {
1617
MessagePayload,
1718
InteractionReplyOptions,
1819
InteractionDeferReplyOptions,
20+
InteractionUpdateOptions,
1921
} from "discord.js";
2022

2123
export default function (client: Client): void;
@@ -93,7 +95,7 @@ export class TextInputComponent extends BaseMessageComponent {
9395
setPlaceholder(placeholder: string): TextInputComponent;
9496
setRequired(required: boolean): TextInputComponent;
9597
setDefaultValue(value: string): TextInputComponent;
96-
toJSON(): APITextInputComponent;
98+
toJSON(): APIModalActionRowComponent<APITextInputComponent>;
9799
}
98100

99101
export type TextInputStyle = "SHORT" | "LONG";
@@ -116,11 +118,22 @@ export class Modal {
116118
export class ModalSubmitField extends BaseMessageComponent {
117119
constructor(data?: ModalSubmitFieldData);
118120

119-
type: string;
121+
type: MessageComponentType;
120122
customId: string;
121123
value: string;
122124
}
123125

126+
export class ModalActionRow {
127+
constructor();
128+
129+
type: number;
130+
components: APITextInputComponent[];
131+
132+
addComponent(component: TextInputComponent): ModalActionRow;
133+
componentToJSON(component: TextInputComponent): APITextInputComponent;
134+
toJSON(): APIModalActionRowComponent<APITextInputComponent>;
135+
}
136+
124137
export class ModalSubmitInteraction extends Interaction {
125138
constructor(client?: Client, data?: APIModalSubmitInteraction);
126139

@@ -156,6 +169,9 @@ export class ModalSubmitInteraction extends Interaction {
156169
followUp(
157170
options: string | MessagePayload | InteractionReplyOptions
158171
): Promise<void>;
172+
update(
173+
options: string | MessagePayload | InteractionUpdateOptions
174+
): Promise<void>
159175
}
160176

161177
export function showModal(

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Client, version } = require('discord.js');
1+
const { version } = require('discord.js');
22
let ModalSubmitInteraction = require('./src/structures/ModalSubmitInteraction');
33
const { Error } = require('./src/structures/errors');
44
const { InteractionTypes } = require('./src/util/Constants');
@@ -40,6 +40,7 @@ if (discordjsVersion.includes('v13')) {
4040
module.exports.TextInputComponent = require('./src/structures/TextInputComponent');
4141
module.exports.ModalSubmitInteraction = require('./src/structures/ModalSubmitInteraction');
4242
module.exports.ModalSubmitField = require('./src/structures/ModalSubmitField');
43+
module.exports.ModalActionRow = require('./src/structures/ModalActionRow');
4344
module.exports.showModal = require('./src/structures/ShowModal');
4445
module.exports.Interaction = require('./src/structures/Interaction');
4546
module.exports.InteractionResponses = require('./src/structures/interfaces/InteractionResponses');
@@ -50,6 +51,7 @@ if (discordjsVersion.includes('v13')) {
5051
module.exports.TextInputComponent = require('./src/structures/TextInputComponent');
5152
module.exports.ModalSubmitInteraction = require('./src/v14/ModalSubmitInteraction');
5253
module.exports.ModalSubmitField = require('./src/structures/ModalSubmitField');
54+
module.exports.ModalActionRow = require('./src/structures/ModalActionRow');
5355
module.exports.showModal = require('./src/v14/ShowModal');
5456
module.exports.Interaction = require('./src/v14/Interaction');
5557
module.exports.InteractionResponses = require('./src/v14/interfaces/InteractionResponses');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "discord-modals",
3-
"version": "1.2.9",
3+
"version": "1.3.0",
44
"description": "Discord-Modals is a package that allows your discord.js v13 and v14 bot to create, and interact with Modals, a new Discord feature.",
55
"main": "index.js",
66
"files": [

src/structures/Modal.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class Modal {
2121
constructor(data = {}, client = null) {
2222

2323
/**
24-
* The Text Input Components of the Modal.
25-
* @type {BaseMessageComponent}
24+
* The Title of the Modal.
25+
* @type {String}
2626
*/
2727

28-
this.components = data.components?.map(c => BaseMessageComponent.create(c, client)) ?? [];
28+
this.title = data.title ?? null;
2929

3030
/**
3131
* The Custom Id of the Modal.
@@ -35,11 +35,11 @@ class Modal {
3535
this.customId = data.custom_id ?? data.customId ?? null;
3636

3737
/**
38-
* The Title of the Modal.
39-
* @type {String}
38+
* The Text Input Components of the Modal.
39+
* @type {BaseMessageComponent}
4040
*/
4141

42-
this.title = data.title ?? null;
42+
this.components = data.components?.map(c => BaseMessageComponent.create(c, client)) ?? [];
4343

4444
}
4545

src/structures/ModalActionRow.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const TextInputComponent = require("./TextInputComponent");
2+
const { MessageComponentTypes, TextInputStyles } = require("../util/Constants");
3+
4+
/**
5+
* Represents a Modal Action Row, that contains a Text Input Component.
6+
*/
7+
8+
class ModalActionRow {
9+
constructor() {
10+
11+
/**
12+
* The type of the Modal Action Row.
13+
*/
14+
15+
this.type = MessageComponentTypes.ACTION_ROW;
16+
17+
/**
18+
* The Text Input Component of this action row
19+
*/
20+
21+
this.components = [];
22+
}
23+
24+
/**
25+
* Adds a Text Input Component.
26+
* @param {TextInputComponent} component
27+
*/
28+
29+
addComponent(component) {
30+
this.components.push(component);
31+
return this;
32+
}
33+
34+
componentToJSON(component) {
35+
return {
36+
type: MessageComponentTypes[component.type],
37+
custom_id: component.customId,
38+
label: component.label,
39+
style: TextInputStyles[component.style],
40+
min_length: component.minLength,
41+
max_length: component.maxLength,
42+
required: component.required,
43+
value: component.value,
44+
placeholder: component.placeholder,
45+
};
46+
}
47+
48+
toJSON() {
49+
return {
50+
type: MessageComponentTypes.ACTION_ROW,
51+
components: this.components.map(c => this.componentToJSON(c)),
52+
};
53+
}
54+
}
55+
56+
module.exports = ModalActionRow;

src/structures/ModalSubmitInteraction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ class ModalSubmitInteraction extends Interaction {
9696
editReply() {}
9797
deleteReply() {}
9898
followUp() {}
99+
update() {}
99100
}
100101

101102
InteractionResponses.applyToClass(ModalSubmitInteraction, [
102103
"deferUpdate",
103-
"update",
104104
"showModal",
105105
]);
106106

src/structures/ShowModal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function showModal(modal, options){
2222
if (!options.client) throw new Error('CLIENT_REQUIRED');
2323
if (!options.interaction) throw new Error('INTERACTION_REQUIRED');
2424
if (!(modal instanceof Modal)) throw new Error('INVALID_MODAL');
25-
if (!(options.client instanceof Client)) throw new Error('INVALID_CLIENT');
25+
if (!options.client.api) throw new Error('INVALID_CLIENT');
2626
if (!(options.interaction instanceof Interaction)) throw new Error('INVALID_INTERACTION');
2727

2828
const _modal = modal instanceof Modal ? modal : new Modal(modal, options.client);

0 commit comments

Comments
 (0)