Skip to content

Commit

Permalink
refactor: form -> modal
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Nov 23, 2021
1 parent 9e74e07 commit 12ddc98
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ const Messages = {
TEXT_INPUT_MIN_LENGTH: 'FormInputText min length must be a number',
TEXT_INPUT_MAX_LENGTH: 'FormInputText max length must be a number',

FORM_CUSTOM_ID: 'MessageForm customId must be a string',
FORM_TITLE: 'MessageForm title must be a string',
MODAL_CUSTOM_ID: 'MessageForm customId must be a string',
MODAL_TITLE: 'MessageForm title must be a string',

SELECT_MENU_CUSTOM_ID: 'MessageSelectMenu customId must be a string',
SELECT_MENU_PLACEHOLDER: 'MessageSelectMenu placeholder must be a string',
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ exports.MessageButton = require('./structures/MessageButton');
exports.MessageCollector = require('./structures/MessageCollector');
exports.MessageComponentInteraction = require('./structures/MessageComponentInteraction');
exports.MessageEmbed = require('./structures/MessageEmbed');
exports.MessageForm = require('./structures/MessageForm');
exports.MessageMentions = require('./structures/MessageMentions');
exports.MessagePayload = require('./structures/MessagePayload');
exports.MessageReaction = require('./structures/MessageReaction');
exports.MessageSelectMenu = require('./structures/MessageSelectMenu');
exports.Modal = require('./structures/Modal');
exports.ModalTextInput = require('./structures/ModalTextInput');
exports.ModalSubmitInteraction = require('./structures/ModalSubmitInteraction');
exports.NewsChannel = require('./structures/NewsChannel');
exports.OAuth2Guild = require('./structures/OAuth2Guild');
Expand All @@ -138,7 +139,6 @@ exports.StoreChannel = require('./structures/StoreChannel');
exports.Team = require('./structures/Team');
exports.TeamMember = require('./structures/TeamMember');
exports.TextChannel = require('./structures/TextChannel');
exports.FormTextInput = require('./structures/FormTextInput');
exports.ThreadChannel = require('./structures/ThreadChannel');
exports.ThreadMember = require('./structures/ThreadMember');
exports.Typing = require('./structures/Typing');
Expand Down
4 changes: 2 additions & 2 deletions src/structures/BaseMessageComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class BaseMessageComponent {
break;
}
case MessageComponentTypes.INPUT_TEXT: {
const FormTextInput = require('./FormTextInput');
component = new FormTextInput(data);
const ModalTextInput = require('./ModalTextInput');
component = new ModalTextInput(data);
break;
}
default:
Expand Down
2 changes: 1 addition & 1 deletion src/structures/MessageActionRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MessageActionRow extends BaseMessageComponent {
* Components that can be placed in an action row
* * MessageButton
* * MessageSelectMenu
* @typedef {MessageButton|MessageSelectMenu|FormTextInput} MessageActionRowComponent
* @typedef {MessageButton|MessageSelectMenu|ModalTextInput} MessageActionRowComponent
*/

/**
Expand Down
28 changes: 14 additions & 14 deletions src/structures/MessageForm.js → src/structures/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const Util = require('../util/Util');
/**
* Represent a modal/form component.
*/
class MessageForm {
class Modal {
constructor(data = {}, client = null) {
this.type = 9;

/**
* The title of this form
* The title of this modal
* @type {string}
*/
this.title = data.title ?? null;
Expand All @@ -23,46 +23,46 @@ class MessageForm {
this.customId = data.custom_id ?? data.customId ?? null;

/**
* The components within this form.
* The components within this modal
* @type {MessageActionRow[]}
*/
this.components = data.components?.map(c => BaseMessageComponent.create(c, client)) ?? null;
}

/**
* Sets the custom id for this form.
* Sets the custom id for this modal
* @param {string} customId A unique string to be sent in the interaction when submitted
* @returns {MessageForm}
* @returns {Modal}
*/
setCustomId(customId) {
this.customId = Util.verifyString(customId, RangeError, 'FORM_CUSTOM_ID');
this.customId = Util.verifyString(customId, RangeError, 'MODAL_CUSTOM_ID');
return this;
}

/**
* Sets the title for this form.
* Sets the title for this modal
* @param {string} title The title for this form.
* @returns {MessageForm}
* @returns {Modal}
*/
setTitle(title) {
this.title = Util.verifyString(title, RangeError, 'FORM_TITLE');
this.title = Util.verifyString(title, RangeError, 'MODAL_TITLE');
return this;
}

/**
* Add components to this form.
* Add components to this modal
* @param {MessageActionRow[]} components The components to add.
* @returns {MessageForm}
* @returns {Modal}
*/
addComponents(...components) {
components.forEach(this.components.push);
return this;
}

/**
* Set the components in this form.
* Set the components in this modal
* @param {MessageActionRow[]} components The components to set.
* @returns {MessageForm}
* @returns {Modal}
*/
setComponents(...components) {
this.spliceComponents(0, this.components.length, components);
Expand Down Expand Up @@ -90,4 +90,4 @@ class MessageForm {
}
}

module.exports = MessageForm;
module.exports = Modal;
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
'use strict';

const BaseMessageComponent = require('./BaseMessageComponent');
const { FormInputTextStyles } = require('../util/Constants');
const { ModalTextInputStyles: FormInputTextStyles } = require('../util/Constants');
const Util = require('../util/Util');

/**
* The style of a form input's text field
* * SHORT
* * PARAGRAPH
* @typedef {string} FormInputTextStyle
* @typedef {string} ModalTextInputStyle
*/

/**
* Represents a text input component.
* @extends {BaseMessageComponent}
*/
class FormTextInput extends BaseMessageComponent {
class ModalTextInput extends BaseMessageComponent {
constructor(data = {}) {
super({ type: 'INPUT_TEXT' });

/**
* The style of this text input
* @type {FormInputTextStyle}
* @type {ModalTextInputStyle}
*/
this.style = data.style ? FormTextInput.resolveStyle(data.style) : null;
this.style = data.style ? ModalTextInput.resolveStyle(data.style) : null;

/**
* A unique string to be sent in the interaction when submitted
Expand Down Expand Up @@ -58,18 +58,18 @@ class FormTextInput extends BaseMessageComponent {

/**
* Sets the style of this text input
* @param {FormInputTextStyle|number} style The style to set this text input to
* @returns {FormTextInput}
* @param {ModalTextInputStyle|number} style The style to set this text input to
* @returns {ModalTextInput}
*/
setStyle(style) {
this.style = FormTextInput.resolveStyle(style);
this.style = ModalTextInput.resolveStyle(style);
return this;
}

/**
* Sets the custom id for this text input
* @param {string} customId The custom id to set
* @returns {FormTextInput}
* @returns {ModalTextInput}
*/
setCustomId(customId) {
this.customId = Util.verifyString(customId, RangeError, 'TEXT_INPUT_CUSTOM_ID');
Expand All @@ -79,7 +79,7 @@ class FormTextInput extends BaseMessageComponent {
/**
* Sets the label for this text input
* @param {string} label The label to set
* @returns {FormTextInput}
* @returns {ModalTextInput}
*/
setLabel(label) {
this.label = Util.verifyString(label, RangeError, 'TEXT_INPUT_LABEL');
Expand All @@ -89,7 +89,7 @@ class FormTextInput extends BaseMessageComponent {
/**
* Sets the placeholder for this text input
* @param {string} placeholder The placeholder to set
* @returns {FormTextInput}
* @returns {ModalTextInput}
*/
setPlaceholder(placeholder) {
this.placeholder = Util.verifyString(placeholder, RangeError, 'TEXT_INPUT_PLACEHOLDER');
Expand All @@ -99,7 +99,7 @@ class FormTextInput extends BaseMessageComponent {
/**
* Sets the minimum length of this text input.
* @param {number} minLength The minimum length of this text input
* @returns {FormTextInput}
* @returns {ModalTextInput}
*/
setMinLength(minLength) {
if (typeof minLength !== 'number') throw new TypeError('TEXT_INPUT_MIN_LENGTH');
Expand All @@ -110,7 +110,7 @@ class FormTextInput extends BaseMessageComponent {
/**
* Sets the maximum length of this text input.
* @param {number} maxLength The maximum length of this text input
* @returns {FormTextInput}
* @returns {ModalTextInput}
*/
setMaxLength(maxLength) {
if (typeof maxLength !== 'number') throw new TypeError('TEXT_INPUT_MAX_LENGTH');
Expand All @@ -134,4 +134,4 @@ class FormTextInput extends BaseMessageComponent {
}
}

module.exports = FormTextInput;
module.exports = ModalTextInput;
4 changes: 2 additions & 2 deletions src/structures/interfaces/InteractionResponses.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ class InteractionResponses {
return options.fetchReply ? this.fetchReply() : undefined;
}

async presentForm(formData) {
async presentModal(modalData) {
await this.client.api.interactions(this.id, this.token).callback.post({
data: {
type: InteractionResponseTypes.MODAL,
data: formData,
data: modalData,
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ exports.MessageButtonStyles = createEnum([null, 'PRIMARY', 'SECONDARY', 'SUCCESS
* @typedef {string} FormInputTextStyle
* @see {@link Add link}
*/
exports.FormInputTextStyles = createEnum([null, 'SHORT', 'PARAGRAPH']);
exports.ModalTextInputStyles = createEnum([null, 'SHORT', 'PARAGRAPH']);

/**
* The required MFA level for a guild
Expand Down Expand Up @@ -1121,7 +1121,7 @@ function createEnum(keys) {
* @property {InteractionType} InteractionTypes The type of an {@link Interaction} object.
* @property {MembershipState} MembershipStates The value set for a team member's membership state.
* @property {MessageButtonStyle} MessageButtonStyles The style of a message button.
* @property {FormInputTextStyle} FormInputTextStyles The style of a text input component.
* @property {ModalInputTextStyle} ModalInputTextStyles The style of a text input component.
* @property {MessageComponentType} MessageComponentTypes The type of a message component.
* @property {MFALevel} MFALevels The required MFA level for a guild.
* @property {NSFWLevel} NSFWLevels NSFW level of a guild.
Expand Down
2 changes: 1 addition & 1 deletion typings/enums.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const enum MessageButtonStyles {
LINK = 5,
}

export const enum FormTextInputStyles {
export const enum ModalTextInputStyles {
SHORT = 1,
PARAGRAPH = 2,
}
Expand Down
32 changes: 16 additions & 16 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import {
ChannelTypes,
DefaultMessageNotificationLevels,
ExplicitContentFilterLevels,
FormTextInputStyles,
ModalTextInputStyles,
InteractionResponseTypes,
InteractionTypes,
InviteTargetType,
Expand Down Expand Up @@ -1506,10 +1506,10 @@ export class MessageButton extends BaseMessageComponent {
private static resolveStyle(style: MessageButtonStyleResolvable): MessageButtonStyle;
}

export class FormInputText extends BaseMessageComponent {
export class ModalTextInput extends BaseMessageComponent {
// TODO: Add API Types
public constructor(data?: FormInputText | FormTextInputOptions);
public style: FormTextInputStyle;
public constructor(data?: ModalTextInput | ModalTextInputOptions);
public style: ModalTextInputStyle;
public customId: string;
public label: string;
public placeholder: string | null;
Expand All @@ -1523,16 +1523,16 @@ export class FormInputText extends BaseMessageComponent {
public setMaxLength(maxLength: number): this;
// TODO: Add API Types
public toJSON(): unknown;
public static resolveStyle(style: FormInputTextStyleResolvable): FormTextInputStyle;
public static resolveStyle(style: FormInputTextStyleResolvable): ModalTextInputStyle;
}

export class MessageForm {
export class Modal {
// TODO: Add API types.
public constructor(data?: MessageForm | MessageFormOptions);
public constructor(data?: Modal | Modal);
public title: string;
public customId: string;
public components: MessageActionRowComponentResolvable[];
public setCustomId(customId: string): this;
public setCustomID(customId: string): this;
public setTitle(title: string): this;
public addComponents(...components: MessageActionRowComponentResolvable[]): this;
public setComponents(...components: MessageActionRowComponentResolvable[]): this;
Expand Down Expand Up @@ -4596,7 +4596,7 @@ export type MessageActionRowComponent = MessageButton | MessageSelectMenu;
export type MessageActionRowComponentOptions =
| (Required<BaseMessageComponentOptions> & MessageButtonOptions)
| (Required<BaseMessageComponentOptions> & MessageSelectMenuOptions)
| (Required<BaseMessageComponentOptions> & FormTextInputOptions);
| (Required<BaseMessageComponentOptions> & ModalTextInputOptions);

export type MessageActionRowComponentResolvable = MessageActionRowComponent | MessageActionRowComponentOptions;

Expand Down Expand Up @@ -4629,26 +4629,26 @@ export type MessageButtonOptions = InteractionButtonOptions | LinkButtonOptions;

export type MessageButtonStyle = keyof typeof MessageButtonStyles;

export type FormTextInputStyle = keyof typeof FormTextInputStyles;
export type ModalTextInputStyle = keyof typeof ModalTextInputStyles;

export interface FormTextInputOptions extends BaseMessageComponentOptions {
export interface ModalTextInputOptions extends BaseMessageComponentOptions {
type: 'TEXT_INPUT' | MessageComponentTypes.TEXT_INPUT;
style: FormTextInputStyle | FormTextInputStyles;
style: ModalTextInputStyle | ModalTextInputStyles;
label: string;
placeholder?: string;
minLength?: number;
maxLength?: number;
}

export interface MessageFormOptions {
export interface Modal {
customID: string;
title: string;
components: MessageActionRowComponentResolvable[];
}

export type MessageButtonStyleResolvable = MessageButtonStyle | MessageButtonStyles;

export type FormInputTextStyleResolvable = FormTextInputStyle | FormTextInputStyles;
export type FormInputTextStyleResolvable = ModalTextInputStyle | ModalTextInputStyles;

export interface MessageCollectorOptions extends CollectorOptions<[Message]> {
max?: number;
Expand All @@ -4660,7 +4660,7 @@ export type MessageComponent =
| MessageActionRow
| MessageButton
| MessageSelectMenu
| FormInputText;
| ModalTextInput;

export type MessageComponentCollectorOptions<T extends MessageComponentInteraction> = Omit<
InteractionCollectorOptions<T>,
Expand All @@ -4677,7 +4677,7 @@ export type MessageComponentOptions =
| MessageActionRowOptions
| MessageButtonOptions
| MessageSelectMenuOptions
| FormTextInputOptions;
| ModalTextInputOptions;

export type MessageComponentType = keyof typeof MessageComponentTypes;

Expand Down

0 comments on commit 12ddc98

Please sign in to comment.