Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/discord.js/src/structures/ModalSubmitFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,37 @@ class ModalSubmitFields {
getUploadedFiles(customId, required = false) {
return this._getTypedComponent(customId, [ComponentType.FileUpload], ['attachments'], required).attachments ?? null;
}

/**
* Get radio group component
*
* @param {string} customId The custom id of the component
* @param {boolean} [required=false] Whether to throw an error if the component value is not found or empty
* @returns {?string} The selected radio group option value, or null if none were selected and not required
*/
getRadioGroup(customId, required = false) {
return this._getTypedComponent(customId, [ComponentType.RadioGroup], ['value'], required).value;
}

/**
* Get checkbox group component
*
* @param {string} customId The custom id of the component
* @returns {string[]} The selected checkbox group option values
*/
getCheckboxGroup(customId) {
return this._getTypedComponent(customId, [ComponentType.CheckboxGroup]).values;
}

/**
* Get checkbox component
*
* @param {string} customId The custom id of the component
* @returns {boolean} Whether this checkbox was selected
*/
getCheckbox(customId) {
return this._getTypedComponent(customId, [ComponentType.Checkbox]).value;
}
}

module.exports = ModalSubmitFields;
21 changes: 20 additions & 1 deletion packages/discord.js/src/structures/ModalSubmitInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ const getAttachment = lazy(() => require('./Attachment'));
* @property {Collection<Snowflake, Attachment>} [attachments] The resolved attachments
*/

/**
* @typedef {BaseModalData} RadioGroupModalData
* @property {string} customId The custom id of the radio group
* @property {?string} value The value selected for the radio group
*/

/**
* @typedef {BaseModalData} CheckboxGroupModalData
* @property {string} customId The custom id of the checkbox group
* @property {string[]} values The values selected for the checkbox group
*/

/**
* @typedef {BaseModalData} CheckboxModalData
* @property {string} customId The custom id of the checkbox
* @property {boolean} value Whether this checkbox was selected
*/

/**
* @typedef {BaseModalData} TextInputModalData
* @property {string} customId The custom id of the field
Expand All @@ -45,7 +63,8 @@ const getAttachment = lazy(() => require('./Attachment'));
*/

/**
* @typedef {SelectMenuModalData|TextInputModalData|FileUploadModalData} ModalData
* @typedef {SelectMenuModalData|TextInputModalData|FileUploadModalData|RadioGroupModalData|
* CheckboxGroupModalData|CheckboxModalData} ModalData
*/

/**
Expand Down
41 changes: 40 additions & 1 deletion packages/discord.js/src/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const { ComponentType } = require('discord-api-types/v10');
/**
* @typedef {StringSelectMenuComponentData|TextInputComponentData|UserSelectMenuComponentData|
* RoleSelectMenuComponentData|MentionableSelectMenuComponentData|ChannelSelectMenuComponentData|
* FileUploadComponentData} ComponentInLabelData
* FileUploadComponentData|RadioGroupComponentData|CheckboxGroupComponentData|
* CheckboxComponentData} ComponentInLabelData
*/

/**
Expand Down Expand Up @@ -52,6 +53,44 @@ const { ComponentType } = require('discord-api-types/v10');
* @property {boolean} [required] Whether this component is required in modals
*/

/**
* @typedef {Object} RadioGroupOption
* @property {string} value The value of the radio group option
* @property {string} label The label to use
* @property {string} [description] The optional description for the radio group option
* @property {boolean} [default] Whether this option is default selected
*/

/**
* @typedef {BaseComponentData} RadioGroupComponentData
* @property {string} customId The custom id of the radio group
* @property {RadioGroupOption[]} options The options in this radio group (2-10)
* @property {boolean} [required] Whether this component is required in modals
*/

/**
* @typedef {Object} CheckboxGroupOption
* @property {string} value The value of the checkbox group option
* @property {string} label The label to use
* @property {string} [description] The optional description for the checkbox group option
* @property {boolean} [default] Whether this option is default selected
*/

/**
* @typedef {BaseComponentData} CheckboxGroupComponentData
* @property {string} customId The custom id of the checkbox group
* @property {CheckboxGroupOption[]} options The options in this checkbox group
* @property {number} [minValues] The minimum number of options that must be selected (0-10)
* @property {number} [maxValues] The maximum number of options that can be selected (defaults to options length)
* @property {boolean} [required] Whether this component is required in modals
*/

/**
* @typedef {BaseComponentData} CheckboxComponentData
* @property {string} customId The custom id of the checkbox
* @property {boolean} [default] Whether this component is default selected in modals
*/

/**
* @typedef {BaseComponentData} BaseSelectMenuComponentData
* @property {string} customId The custom id of the select menu
Expand Down
64 changes: 63 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ export class ActionRowBuilder<
}

export type ComponentInLabelData =
| CheckboxComponentData
| CheckboxGroupComponentData
| RadioGroupComponentData
| StringSelectMenuComponentData
| TextInputComponentData
| UserSelectMenuComponentData
Expand Down Expand Up @@ -2828,7 +2831,28 @@ export interface FileUploadModalData extends BaseModalData<ComponentType.FileUpl
attachments: ReadonlyCollection<Snowflake, Attachment>;
}

export type ModalData = FileUploadModalData | SelectMenuModalData | TextInputModalData;
export interface RadioGroupModalData extends BaseModalData<ComponentType.RadioGroup> {
customId: string;
value: string | null;
}

export interface CheckboxGroupModalData extends BaseModalData<ComponentType.CheckboxGroup> {
customId: string;
values: readonly string[];
}

export interface CheckboxModalData extends BaseModalData<ComponentType.Checkbox> {
customId: string;
value: boolean;
}

export type ModalData =
| CheckboxGroupModalData
| CheckboxModalData
| FileUploadModalData
| RadioGroupModalData
| SelectMenuModalData
| TextInputModalData;

export interface LabelModalData extends BaseModalData<ComponentType.Label> {
component: ModalData;
Expand Down Expand Up @@ -2907,6 +2931,10 @@ export class ModalSubmitFields<Cached extends CacheType = CacheType> {
public getSelectedMentionables(customId: string, required?: boolean): ModalSelectedMentionables<Cached> | null;
public getUploadedFiles(customId: string, required: true): ReadonlyCollection<Snowflake, Attachment>;
public getUploadedFiles(customId: string, required?: boolean): ReadonlyCollection<Snowflake, Attachment> | null;
public getRadioGroup(customId: string, required: true): string;
public getRadioGroup(customId: string, required?: boolean): string | null;
public getCheckboxGroup(customId: string): readonly string[];
public getCheckbox(customId: string): boolean;
}

export interface ModalMessageModalSubmitInteraction<
Expand Down Expand Up @@ -7469,6 +7497,40 @@ export interface FileUploadComponentData extends BaseComponentData {
type: ComponentType.FileUpload;
}

export interface RadioGroupOption {
default?: boolean;
description?: string;
label: string;
value: string;
}
export interface RadioGroupComponentData extends BaseComponentData {
customId: string;
options: readonly RadioGroupOption[];
required?: boolean;
type: ComponentType.RadioGroup;
}

export interface CheckboxGroupOption {
default?: boolean;
description?: string;
label: string;
value: string;
}
export interface CheckboxGroupComponentData extends BaseComponentData {
customId: string;
maxValues?: number;
minValues?: number;
options: readonly CheckboxGroupOption[];
required?: boolean;
type: ComponentType.CheckboxGroup;
}

export interface CheckboxComponentData extends BaseComponentData {
customId: string;
default?: boolean;
type: ComponentType.Checkbox;
}

export type MessageTarget =
| Interaction
| InteractionWebhook
Expand Down