From 585169f2f097ffb1940d17f549e4290aa55acde2 Mon Sep 17 00:00:00 2001 From: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com> Date: Tue, 19 Apr 2022 10:00:55 -0400 Subject: [PATCH] types: cleanup *Data type definitions (#7716) --- packages/discord.js/typings/index.d.ts | 32 +++++++++++-------- packages/discord.js/typings/index.test-d.ts | 35 +++++++++++++++++++-- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index d151f7481..7567a0528 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -216,27 +216,29 @@ export class Activity { export type ActivityFlagsString = keyof typeof ActivityFlags; export interface BaseComponentData { - type?: ComponentType; + type: ComponentType; } -export type MessageActionRowComponentData = ButtonComponentData | SelectMenuComponentData; +export type MessageActionRowComponentData = + | JSONEncodable + | ButtonComponentData + | SelectMenuComponentData; -export type ModalActionRowComponentData = TextInputComponentData; +export type ModalActionRowComponentData = JSONEncodable | TextInputComponentData; export type ActionRowComponentData = MessageActionRowComponentData | ModalActionRowComponentData; export type ActionRowComponent = MessageActionRowComponent | ModalActionRowComponent; -export type ActionRowComponentBuilder = MessageActionRowComponentBuilder | ModalActionRowComponentBuilder; - -export interface ActionRowData extends BaseComponentData { +export interface ActionRowData | ActionRowComponentData> + extends BaseComponentData { components: T[]; } export class ActionRowBuilder extends BuilderActionRow { constructor( data?: Partial< - | ActionRowData + | ActionRowData> | APIActionRowComponent >, ); @@ -587,7 +589,7 @@ export class ButtonComponent extends Component { export type ComponentEmojiResolvable = APIMessageComponentEmoji | string; export class ButtonBuilder extends BuilderButtonComponent { - public constructor(data?: ButtonComponentData | (Omit & { type?: ComponentType.Button })); + public constructor(data?: Partial | Partial); public static from(other: JSONEncodable | APIButtonComponent): ButtonBuilder; public override setEmoji(emoji: ComponentEmojiResolvable): this; } @@ -4736,9 +4738,9 @@ export interface MakeErrorOptions { stack: string; } -export type ActionRowComponentOptions = - | (Required & ButtonComponentData) - | (Required & SelectMenuComponentData); +export type MemberMention = UserMention | `<@!${Snowflake}>`; + +export type ActionRowComponentOptions = ButtonComponentData | SelectMenuComponentData; export type MessageActionRowComponentResolvable = MessageActionRowComponent | ActionRowComponentOptions; @@ -4748,6 +4750,7 @@ export interface MessageActivity { } export interface BaseButtonComponentData extends BaseComponentData { + style: ButtonStyle; disabled?: boolean; emoji?: ComponentEmojiResolvable; label?: string; @@ -4795,7 +4798,8 @@ export interface MessageEditOptions { allowedMentions?: MessageMentionOptions; components?: ( | JSONEncodable> - | (Required & ActionRowData) + | ActionRow + | ActionRowData | APIActionRowComponent )[]; } @@ -4836,7 +4840,7 @@ export interface MessageOptions { embeds?: (JSONEncodable | APIEmbed)[]; components?: ( | JSONEncodable> - | (Required & ActionRowData) + | ActionRowData | APIActionRowComponent )[]; allowedMentions?: MessageMentionOptions; @@ -4864,6 +4868,7 @@ export interface MessageReference { export type MessageResolvable = Message | Snowflake; export interface SelectMenuComponentData extends BaseComponentData { + type: ComponentType.SelectMenu; customId: string; disabled?: boolean; maxValues?: number; @@ -4889,6 +4894,7 @@ export interface SelectMenuComponentOptionData { } export interface TextInputComponentData extends BaseComponentData { + type: ComponentType.TextInput; customId: string; style: TextInputStyle; label: string; diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index 93ec1ebf9..2d00e4007 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -526,6 +526,34 @@ client.on('guildCreate', async g => { const channel = g.channels.cache.random(); if (!channel) return; + if (channel.isText()) { + const row: ActionRowData = { + type: ComponentType.ActionRow, + components: [ + new ButtonBuilder(), + { type: ComponentType.Button, style: ButtonStyle.Primary, label: 'string', customId: 'foo' }, + { type: ComponentType.Button, style: ButtonStyle.Link, label: 'test', url: 'test' }, + { type: ComponentType.SelectMenu, customId: 'foo' }, + new SelectMenuBuilder(), + // @ts-expect-error + { type: ComponentType.TextInput, style: TextInputStyle.Paragraph, customId: 'foo', label: 'test' }, + // @ts-expect-error + new TextInputBuilder(), + ], + }; + + const row2 = new ActionRowBuilder({ + type: ComponentType.ActionRow, + components: [ + { type: ComponentType.Button, style: ButtonStyle.Primary, label: 'string', customId: 'foo' }, + { type: ComponentType.Button, style: ButtonStyle.Link, label: 'test', url: 'test' }, + { type: ComponentType.SelectMenu, customId: 'foo' }, + ], + }); + + channel.send({ components: [row, row2] }); + } + if (channel.isThread()) { const fetchedMember = await channel.members.fetch({ member: '12345678' }); expectType(fetchedMember); @@ -738,7 +766,7 @@ client.on('messageCreate', async message => { // Check that both builders and builder data can be sent in messages const row = new ActionRowBuilder(); - const buttonsRow = { + const buttonsRow: ActionRowData = { type: ComponentType.ActionRow, components: [ new ButtonBuilder(), @@ -747,12 +775,12 @@ client.on('messageCreate', async message => { { type: ComponentType.Button, label: 'another test', - style: ButtonStyle.Link as const, + style: ButtonStyle.Link, url: 'https://discord.js.org', }, ], }; - const selectsRow = { + const selectsRow: ActionRowData = { type: ComponentType.ActionRow, components: [ new SelectMenuBuilder(), @@ -765,6 +793,7 @@ client.on('messageCreate', async message => { }, ], }; + const buildersEmbed = new UnsafeEmbedBuilder(); const embedData = { description: 'test', color: 0xff0000 }; channel.send({ components: [row, buttonsRow, selectsRow], embeds: [embed, buildersEmbed, embedData] });