forked from discordjs/discord.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modals): modals, input text components and modal submits, v13 st…
…yle (discordjs#7431)
- Loading branch information
1 parent
cd93f5e
commit 0ce4318
Showing
17 changed files
with
752 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict'; | ||
|
||
const BaseMessageComponent = require('./BaseMessageComponent'); | ||
const Util = require('../util/Util'); | ||
|
||
class Modal { | ||
/** | ||
* @typedef {Object} ModalOptions | ||
* @property {string} [customId] A unique string to be sent in the interaction when clicked | ||
* @property {string} [title] The title to be displayed on this modal | ||
* @property {MessageActionRow[]|MessageActionRowOptions[]} [components] | ||
* Action rows containing interactive components for the modal (text input components) | ||
*/ | ||
|
||
/** | ||
* @param {Modal|ModalOptions} data Modal to clone or raw data | ||
* @param {Client} client The client constructing this Modal, if provided | ||
*/ | ||
constructor(data = {}, client = null) { | ||
/** | ||
* A list of MessageActionRows in the modal | ||
* @type {MessageActionRow[]} | ||
*/ | ||
this.components = data.components?.map(c => BaseMessageComponent.create(c, client)) ?? []; | ||
|
||
/** | ||
* A unique string to be sent in the interaction when submitted | ||
* @type {?string} | ||
*/ | ||
this.customId = data.custom_id ?? data.customId ?? null; | ||
|
||
/** | ||
* The title to be displayed on this modal | ||
* @type {?string} | ||
*/ | ||
this.title = data.title ?? null; | ||
} | ||
|
||
/** | ||
* Adds components to the modal. | ||
* @param {...MessageActionRowResolvable[]} components The components to add | ||
* @returns {Modal} | ||
*/ | ||
addComponents(...components) { | ||
this.components.push(...components.flat(Infinity).map(c => BaseMessageComponent.create(c))); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the components of the modal. | ||
* @param {...MessageActionRowResolvable[]} components The components to set | ||
* @returns {Modal} | ||
*/ | ||
setComponents(...components) { | ||
this.spliceComponents(0, this.components.length, components); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the custom id for this modal | ||
* @param {string} customId A unique string to be sent in the interaction when submitted | ||
* @returns {Modal} | ||
*/ | ||
setCustomId(customId) { | ||
this.customId = Util.verifyString(customId, RangeError, 'MODAL_CUSTOM_ID'); | ||
return this; | ||
} | ||
|
||
/** | ||
* Removes, replaces, and inserts components in the modal. | ||
* @param {number} index The index to start at | ||
* @param {number} deleteCount The number of components to remove | ||
* @param {...MessageActionRowResolvable[]} [components] The replacing components | ||
* @returns {Modal} | ||
*/ | ||
spliceComponents(index, deleteCount, ...components) { | ||
this.components.splice(index, deleteCount, ...components.flat(Infinity).map(c => BaseMessageComponent.create(c))); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the title of this modal | ||
* @param {string} title The title to be displayed on this modal | ||
* @returns {Modal} | ||
*/ | ||
setTitle(title) { | ||
this.title = Util.verifyString(title, RangeError, 'MODAL_TITLE'); | ||
return this; | ||
} | ||
|
||
toJSON() { | ||
return { | ||
components: this.components.map(c => c.toJSON()), | ||
custom_id: this.customId, | ||
title: this.title, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Modal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
const { TypeError } = require('../errors'); | ||
const { MessageComponentTypes } = require('../util/Constants'); | ||
|
||
class ModalSubmitFieldsResolver { | ||
constructor(components) { | ||
/** | ||
* The components within the modal | ||
* @type {PartialModalActionRow[]} The components in the modal | ||
*/ | ||
this.components = components; | ||
} | ||
|
||
/** | ||
* The extracted fields from the modal | ||
* @type {PartialInputTextData[]} The fields in the modal | ||
* @private | ||
*/ | ||
get _fields() { | ||
return this.components.reduce((previous, next) => previous.concat(next.components), []); | ||
} | ||
|
||
/** | ||
* Gets a field given a custom id from a component | ||
* @param {string} customId The custom id of the component | ||
* @returns {?PartialInputTextData} | ||
*/ | ||
getField(customId) { | ||
const field = this._fields.find(f => f.customId === customId); | ||
if (!field) throw new TypeError('MODAL_SUBMIT_INTERACTION_FIELD_NOT_FOUND', customId); | ||
return field; | ||
} | ||
|
||
/** | ||
* Gets the value of a text input component given a custom id | ||
* @param {string} customId The custom id of the text input component | ||
* @returns {?string} | ||
*/ | ||
getTextInputValue(customId) { | ||
const field = this.getField(customId); | ||
const expectedType = MessageComponentTypes[MessageComponentTypes.TEXT_INPUT]; | ||
if (field.type !== expectedType) { | ||
throw new TypeError('MODAL_SUBMIT_INTERACTION_FIELD_TYPE', customId, field.type, expectedType); | ||
} | ||
return field.value; | ||
} | ||
} | ||
|
||
module.exports = ModalSubmitFieldsResolver; |
Oops, something went wrong.