Skip to content

Commit

Permalink
feat(Interactions): add modal and text input interactions (#243)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `APIBaseMessageComponent` was renamed to `APIBaseComponent`
  • Loading branch information
suneettipirneni committed Feb 10, 2022
1 parent 71c4e6a commit bf0f66b
Show file tree
Hide file tree
Showing 24 changed files with 536 additions and 44 deletions.
28 changes: 28 additions & 0 deletions deno/payloads/v8/_interactions/modalSubmit.ts
@@ -0,0 +1,28 @@
import type { APIActionRowComponent, APIModalComponent } from '../channel.ts';
import type { APIBaseInteraction, InteractionType, ComponentType } from '../mod.ts';

export interface ModalSubmitComponent {
type: ComponentType;
custom_id: string;
value: string;
}

export interface ModalSubmitActionRowComponent extends Omit<APIActionRowComponent<APIModalComponent>, 'components'> {
components: ModalSubmitComponent[];
}

export interface APIModalSubmission {
/**
* A developer-defined identifier for the component, max 100 characters
*/
custom_id: string;
/**
* A list of child components
*/
components?: ModalSubmitActionRowComponent[];
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object
*/
export type APIModalSubmitInteraction = APIBaseInteraction<InteractionType.ModalSubmit, APIModalSubmission>;
29 changes: 29 additions & 0 deletions deno/payloads/v8/_interactions/responses.ts
@@ -1,6 +1,7 @@
import type { MessageFlags } from '../mod.ts';
import type { RESTPostAPIWebhookWithTokenJSONBody } from '../../../v8.ts';
import type { APIApplicationCommandOptionChoice } from './applicationCommands.ts';
import type { APIActionRowComponent, APIModalComponent } from '../channel.ts';

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-type
Expand All @@ -10,6 +11,7 @@ export enum InteractionType {
ApplicationCommand,
MessageComponent,
ApplicationCommandAutocomplete,
ModalSubmit,
}

/**
Expand All @@ -32,6 +34,11 @@ export interface APIApplicationCommandAutocompleteResponse {
data: APICommandAutocompleteInteractionResponseCallbackData;
}

export interface APIModalInteractionResponse {
type: InteractionResponseType.Modal;
data: APIModalInteractionResponseCallbackData;
}

export interface APIInteractionResponseChannelMessageWithSource {
type: InteractionResponseType.ChannelMessageWithSource;
data: APIInteractionResponseCallbackData;
Expand Down Expand Up @@ -79,6 +86,10 @@ export enum InteractionResponseType {
* For autocomplete interactions
*/
ApplicationCommandAutocompleteResult,
/**
* Respond to an interaction with an modal for a user to fill-out
*/
Modal,
}

/**
Expand All @@ -92,3 +103,21 @@ export type APIInteractionResponseCallbackData = Omit<
export interface APICommandAutocompleteInteractionResponseCallbackData {
choices?: APIApplicationCommandOptionChoice[];
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-modal
*/
export interface APIModalInteractionResponseCallbackData {
/**
* A developer-defined identifier for the component, max 100 characters
*/
custom_id: string;
/**
* The title of the popup modal
*/
title: string;
/**
* Between 1 and 5 (inclusive) components that make up the modal
*/
components: APIActionRowComponent<APIModalComponent>[];
}
79 changes: 71 additions & 8 deletions deno/payloads/v8/channel.ts
Expand Up @@ -3,10 +3,10 @@
*/

import type { Permissions, Snowflake } from '../../globals.ts';
import type { APIApplication } from './application.ts';
import type { APIPartialEmoji } from './emoji.ts';
import type { APIGuildMember } from './guild.ts';
import type { APIMessageInteraction } from './interactions.ts';
import type { APIApplication } from './application.ts';
import type { APIRole } from './permissions.ts';
import type { APISticker, APIStickerItem } from './sticker.ts';
import type { APIUser } from './user.ts';
Expand Down Expand Up @@ -406,7 +406,7 @@ export interface APIMessage {
/**
* Sent if the message contains components like buttons, action rows, or other interactive components
*/
components?: APIActionRowComponent[];
components?: APIActionRowComponent<APIMessageComponent>[];
/**
* Sent if the message contains stickers
*
Expand Down Expand Up @@ -976,7 +976,7 @@ export interface APIAllowedMentions {
/**
* https://discord.com/developers/docs/interactions/message-components#component-object
*/
export interface APIBaseMessageComponent<T extends ComponentType> {
export interface APIBaseComponent<T extends ComponentType> {
/**
* The type of the component
*/
Expand All @@ -999,22 +999,27 @@ export enum ComponentType {
* Select Menu component
*/
SelectMenu,
/**
* Text Input component
*/
TextInput,
}

/**
* https://discord.com/developers/docs/interactions/message-components#action-rows
*/
export interface APIActionRowComponent extends APIBaseMessageComponent<ComponentType.ActionRow> {
export interface APIActionRowComponent<T extends APIActionRowComponentTypes>
extends APIBaseComponent<ComponentType.ActionRow> {
/**
* The components in the ActionRow
*/
components: Exclude<APIMessageComponent, APIActionRowComponent>[];
components: Exclude<T, APIActionRowComponent<T>>[];
}

/**
* https://discord.com/developers/docs/interactions/message-components#buttons
*/
interface APIButtonComponentBase<Style extends ButtonStyle> extends APIBaseMessageComponent<ComponentType.Button> {
interface APIButtonComponentBase<Style extends ButtonStyle> extends APIBaseComponent<ComponentType.Button> {
/**
* The label to be displayed on the button
*/
Expand Down Expand Up @@ -1078,10 +1083,18 @@ export enum ButtonStyle {
Link,
}

/**
* https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-styles
*/
export enum TextInputStyle {
Short = 1,
Paragraph,
}

/**
* https://discord.com/developers/docs/interactions/message-components#select-menus
*/
export interface APISelectMenuComponent extends APIBaseMessageComponent<ComponentType.SelectMenu> {
export interface APISelectMenuComponent extends APIBaseComponent<ComponentType.SelectMenu> {
/**
* A developer-defined identifier for the select menu, max 100 characters
*/
Expand Down Expand Up @@ -1140,7 +1153,57 @@ export interface APISelectMenuOption {
default?: boolean;
}

/**
* https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure
*/
export interface APITextInputComponent extends APIBaseComponent<ComponentType.TextInput> {
/**
* One of text input styles
*/
style: TextInputStyle;
/**
* The custom id for the text input
*/
custom_id: string;
/**
* Text that appears on top of the text input field, max 80 characters
*/
label: string;
/**
* Placeholder for the text input
*/
placeholder?: string;
/**
* The pre-filled text in the text input
*/
value?: string;
/**
* Minimal length of text input
*/
min_length?: number;
/**
* Maximal length of text input
*/
max_length?: number;
/**
* Whether or not this text input is required or not
*/
required?: boolean;
}

export type APIActionRowComponentTypes = APIMessageComponent | APIModalComponent;

/**
* https://discord.com/developers/docs/interactions/message-components#message-components
*/
export type APIMessageComponent = APIActionRowComponent | APIButtonComponent | APISelectMenuComponent;
export type APIMessageComponent =
| APIActionRowComponent<APIMessageComponent>
| APIButtonComponent
| APISelectMenuComponent;

export type APIMessageActionRowComponent = APIActionRowComponent<APIMessageComponent>;

// Modal components
export type APIModalComponent = APIActionRowComponent<APIModalComponent> | APITextInputComponent;

export type APIModalActionRowComponent = APIActionRowComponent<APIModalComponent>;
1 change: 1 addition & 0 deletions deno/payloads/v8/interactions.ts
Expand Up @@ -16,6 +16,7 @@ export * from './_interactions/messageComponents.ts';
export * from './_interactions/ping.ts';
export * from './_interactions/responses.ts';
export * from './_interactions/applicationCommands.ts';
export * from './_interactions/modalSubmit.ts';

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object
Expand Down
28 changes: 28 additions & 0 deletions deno/payloads/v9/_interactions/modalSubmit.ts
@@ -0,0 +1,28 @@
import type { APIActionRowComponent, APIModalComponent } from '../channel.ts';
import type { APIBaseInteraction, InteractionType, ComponentType } from '../mod.ts';

export interface ModalSubmitComponent {
type: ComponentType;
custom_id: string;
value: string;
}

export interface ModalSubmitActionRowComponent extends Omit<APIActionRowComponent<APIModalComponent>, 'components'> {
components: ModalSubmitComponent[];
}

export interface APIModalSubmission {
/**
* A developer-defined identifier for the component, max 100 characters
*/
custom_id: string;
/**
* A list of child components
*/
components?: ModalSubmitActionRowComponent[];
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object
*/
export type APIModalSubmitInteraction = APIBaseInteraction<InteractionType.ModalSubmit, APIModalSubmission>;
29 changes: 29 additions & 0 deletions deno/payloads/v9/_interactions/responses.ts
@@ -1,6 +1,7 @@
import type { MessageFlags } from '../mod.ts';
import type { RESTPostAPIWebhookWithTokenJSONBody } from '../../../v9.ts';
import type { APIApplicationCommandOptionChoice } from './applicationCommands.ts';
import type { APIActionRowComponent, APIModalComponent } from '../channel.ts';

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-type
Expand All @@ -10,6 +11,7 @@ export enum InteractionType {
ApplicationCommand,
MessageComponent,
ApplicationCommandAutocomplete,
ModalSubmit,
}

/**
Expand All @@ -32,6 +34,11 @@ export interface APIApplicationCommandAutocompleteResponse {
data: APICommandAutocompleteInteractionResponseCallbackData;
}

export interface APIModalInteractionResponse {
type: InteractionResponseType.Modal;
data: APIModalInteractionResponseCallbackData;
}

export interface APIInteractionResponseChannelMessageWithSource {
type: InteractionResponseType.ChannelMessageWithSource;
data: APIInteractionResponseCallbackData;
Expand Down Expand Up @@ -79,6 +86,10 @@ export enum InteractionResponseType {
* For autocomplete interactions
*/
ApplicationCommandAutocompleteResult,
/**
* Respond to an interaction with an modal for a user to fill-out
*/
Modal,
}

/**
Expand All @@ -92,3 +103,21 @@ export type APIInteractionResponseCallbackData = Omit<
export interface APICommandAutocompleteInteractionResponseCallbackData {
choices?: APIApplicationCommandOptionChoice[];
}

/**
* https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-modal
*/
export interface APIModalInteractionResponseCallbackData {
/**
* A developer-defined identifier for the component, max 100 characters
*/
custom_id: string;
/**
* The title of the popup modal
*/
title: string;
/**
* Between 1 and 5 (inclusive) components that make up the modal
*/
components: APIActionRowComponent<APIModalComponent>[];
}

0 comments on commit bf0f66b

Please sign in to comment.