mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
chore(builders): simplify types (#7784)
* chore(builders): simplify types * chore: removed uneeded partial
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
|||||||
ComponentType,
|
ComponentType,
|
||||||
APIMessageActionRowComponent,
|
APIMessageActionRowComponent,
|
||||||
APIModalActionRowComponent,
|
APIModalActionRowComponent,
|
||||||
|
APIActionRowComponentTypes,
|
||||||
} from 'discord-api-types/v10';
|
} from 'discord-api-types/v10';
|
||||||
import type { ButtonBuilder, SelectMenuBuilder, TextInputBuilder } from '..';
|
import type { ButtonBuilder, SelectMenuBuilder, TextInputBuilder } from '..';
|
||||||
import { ComponentBuilder } from './Component';
|
import { ComponentBuilder } from './Component';
|
||||||
@@ -12,34 +13,22 @@ export type MessageComponentBuilder =
|
|||||||
| MessageActionRowComponentBuilder
|
| MessageActionRowComponentBuilder
|
||||||
| ActionRowBuilder<MessageActionRowComponentBuilder>;
|
| ActionRowBuilder<MessageActionRowComponentBuilder>;
|
||||||
export type ModalComponentBuilder = ModalActionRowComponentBuilder | ActionRowBuilder<ModalActionRowComponentBuilder>;
|
export type ModalComponentBuilder = ModalActionRowComponentBuilder | ActionRowBuilder<ModalActionRowComponentBuilder>;
|
||||||
|
|
||||||
export type MessageActionRowComponentBuilder = ButtonBuilder | SelectMenuBuilder;
|
export type MessageActionRowComponentBuilder = ButtonBuilder | SelectMenuBuilder;
|
||||||
export type ModalActionRowComponentBuilder = TextInputBuilder;
|
export type ModalActionRowComponentBuilder = TextInputBuilder;
|
||||||
|
export type AnyComponentBuilder = MessageActionRowComponentBuilder | ModalActionRowComponentBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an action row component
|
* Represents an action row component
|
||||||
*/
|
*/
|
||||||
export class ActionRowBuilder<
|
export class ActionRowBuilder<T extends AnyComponentBuilder> extends ComponentBuilder<
|
||||||
T extends MessageActionRowComponentBuilder | ModalActionRowComponentBuilder =
|
APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>
|
||||||
| MessageActionRowComponentBuilder
|
|
||||||
| ModalActionRowComponentBuilder,
|
|
||||||
> extends ComponentBuilder<
|
|
||||||
Omit<
|
|
||||||
Partial<APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>> & {
|
|
||||||
type: ComponentType.ActionRow;
|
|
||||||
},
|
|
||||||
'components'
|
|
||||||
>
|
|
||||||
> {
|
> {
|
||||||
/**
|
/**
|
||||||
* The components within this action row
|
* The components within this action row
|
||||||
*/
|
*/
|
||||||
public readonly components: T[];
|
public readonly components: T[];
|
||||||
|
|
||||||
public constructor({
|
public constructor({ components, ...data }: Partial<APIActionRowComponent<APIActionRowComponentTypes>> = {}) {
|
||||||
components,
|
|
||||||
...data
|
|
||||||
}: Partial<APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>> = {}) {
|
|
||||||
super({ type: ComponentType.ActionRow, ...data });
|
super({ type: ComponentType.ActionRow, ...data });
|
||||||
this.components = (components?.map((c) => createComponentBuilder(c)) ?? []) as T[];
|
this.components = (components?.map((c) => createComponentBuilder(c)) ?? []) as T[];
|
||||||
}
|
}
|
||||||
@@ -64,9 +53,10 @@ export class ActionRowBuilder<
|
|||||||
}
|
}
|
||||||
|
|
||||||
public toJSON(): APIActionRowComponent<ReturnType<T['toJSON']>> {
|
public toJSON(): APIActionRowComponent<ReturnType<T['toJSON']>> {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||||
return {
|
return {
|
||||||
...this.data,
|
...this.data,
|
||||||
components: this.components.map((component) => component.toJSON()) as ReturnType<T['toJSON']>[],
|
components: this.components.map((component) => component.toJSON()),
|
||||||
};
|
} as APIActionRowComponent<ReturnType<T['toJSON']>>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,37 +3,26 @@ import type {
|
|||||||
APIActionRowComponent,
|
APIActionRowComponent,
|
||||||
APIActionRowComponentTypes,
|
APIActionRowComponentTypes,
|
||||||
APIBaseComponent,
|
APIBaseComponent,
|
||||||
APIMessageActionRowComponent,
|
|
||||||
APIMessageComponent,
|
|
||||||
APIModalActionRowComponent,
|
|
||||||
APIModalComponent,
|
|
||||||
ComponentType,
|
ComponentType,
|
||||||
} from 'discord-api-types/v10';
|
} from 'discord-api-types/v10';
|
||||||
|
|
||||||
|
export type AnyAPIActionRowComponent = APIActionRowComponentTypes | APIActionRowComponent<APIActionRowComponentTypes>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a discord component
|
* Represents a discord component
|
||||||
*/
|
*/
|
||||||
export abstract class ComponentBuilder<
|
export abstract class ComponentBuilder<
|
||||||
DataType extends Partial<APIBaseComponent<ComponentType>> & {
|
DataType extends Partial<APIBaseComponent<ComponentType>> = APIBaseComponent<ComponentType>,
|
||||||
type: ComponentType;
|
> implements JSONEncodable<AnyAPIActionRowComponent>
|
||||||
} = APIBaseComponent<ComponentType>,
|
|
||||||
> implements
|
|
||||||
JSONEncodable<
|
|
||||||
| APIModalComponent
|
|
||||||
| APIMessageComponent
|
|
||||||
| APIActionRowComponent<APIModalActionRowComponent | APIMessageActionRowComponent>
|
|
||||||
>
|
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The API data associated with this component
|
* The API data associated with this component
|
||||||
*/
|
*/
|
||||||
public readonly data: DataType;
|
public readonly data: Partial<DataType>;
|
||||||
|
|
||||||
public abstract toJSON():
|
public abstract toJSON(): AnyAPIActionRowComponent;
|
||||||
| APIActionRowComponentTypes
|
|
||||||
| APIActionRowComponent<APIModalActionRowComponent | APIMessageActionRowComponent>;
|
|
||||||
|
|
||||||
public constructor(data: DataType) {
|
public constructor(data: Partial<DataType>) {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { APIMessageComponent, APIModalComponent, ComponentType } from 'discord-api-types/v10';
|
import { APIMessageComponent, APIModalComponent, ComponentType } from 'discord-api-types/v10';
|
||||||
import { ActionRowBuilder, ButtonBuilder, ComponentBuilder, SelectMenuBuilder, TextInputBuilder } from '../index';
|
import { ActionRowBuilder, ButtonBuilder, ComponentBuilder, SelectMenuBuilder, TextInputBuilder } from '../index';
|
||||||
import type { MessageComponentBuilder, ModalComponentBuilder } from './ActionRow';
|
import type { AnyComponentBuilder, MessageComponentBuilder, ModalComponentBuilder } from './ActionRow';
|
||||||
|
|
||||||
export interface MappedComponentTypes {
|
export interface MappedComponentTypes {
|
||||||
[ComponentType.ActionRow]: ActionRowBuilder;
|
[ComponentType.ActionRow]: ActionRowBuilder<AnyComponentBuilder>;
|
||||||
[ComponentType.Button]: ButtonBuilder;
|
[ComponentType.Button]: ButtonBuilder;
|
||||||
[ComponentType.SelectMenu]: SelectMenuBuilder;
|
[ComponentType.SelectMenu]: SelectMenuBuilder;
|
||||||
[ComponentType.TextInput]: TextInputBuilder;
|
[ComponentType.TextInput]: TextInputBuilder;
|
||||||
|
|||||||
@@ -11,9 +11,7 @@ import { ComponentBuilder } from '../Component';
|
|||||||
/**
|
/**
|
||||||
* Represents a non-validated button component
|
* Represents a non-validated button component
|
||||||
*/
|
*/
|
||||||
export class UnsafeButtonBuilder extends ComponentBuilder<
|
export class UnsafeButtonBuilder extends ComponentBuilder<APIButtonComponent> {
|
||||||
Partial<APIButtonComponent> & { type: ComponentType.Button }
|
|
||||||
> {
|
|
||||||
public constructor(data?: Partial<APIButtonComponent>) {
|
public constructor(data?: Partial<APIButtonComponent>) {
|
||||||
super({ type: ComponentType.Button, ...data });
|
super({ type: ComponentType.Button, ...data });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,7 @@ import { UnsafeSelectMenuOptionBuilder } from './UnsafeSelectMenuOption';
|
|||||||
/**
|
/**
|
||||||
* Represents a non-validated select menu component
|
* Represents a non-validated select menu component
|
||||||
*/
|
*/
|
||||||
export class UnsafeSelectMenuBuilder extends ComponentBuilder<
|
export class UnsafeSelectMenuBuilder extends ComponentBuilder<APISelectMenuComponent> {
|
||||||
Partial<Omit<APISelectMenuComponent, 'options'>> & { type: ComponentType.SelectMenu }
|
|
||||||
> {
|
|
||||||
/**
|
/**
|
||||||
* The options within this select menu
|
* The options within this select menu
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -2,9 +2,7 @@ import { ComponentType, type TextInputStyle, type APITextInputComponent } from '
|
|||||||
import { ComponentBuilder } from '../../index';
|
import { ComponentBuilder } from '../../index';
|
||||||
import isEqual from 'fast-deep-equal';
|
import isEqual from 'fast-deep-equal';
|
||||||
|
|
||||||
export class UnsafeTextInputBuilder extends ComponentBuilder<
|
export class UnsafeTextInputBuilder extends ComponentBuilder<APITextInputComponent> {
|
||||||
Partial<APITextInputComponent> & { type: ComponentType.TextInput }
|
|
||||||
> {
|
|
||||||
public constructor(data?: APITextInputComponent & { type?: ComponentType.TextInput }) {
|
public constructor(data?: APITextInputComponent & { type?: ComponentType.TextInput }) {
|
||||||
super({ type: ComponentType.TextInput, ...data });
|
super({ type: ComponentType.TextInput, ...data });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import type {
|
|||||||
import { ActionRowBuilder, createComponentBuilder, JSONEncodable, ModalActionRowComponentBuilder } from '../../index';
|
import { ActionRowBuilder, createComponentBuilder, JSONEncodable, ModalActionRowComponentBuilder } from '../../index';
|
||||||
|
|
||||||
export class UnsafeModalBuilder implements JSONEncodable<APIModalInteractionResponseCallbackData> {
|
export class UnsafeModalBuilder implements JSONEncodable<APIModalInteractionResponseCallbackData> {
|
||||||
public readonly data: Partial<Omit<APIModalInteractionResponseCallbackData, 'components'>>;
|
public readonly data: Partial<APIModalInteractionResponseCallbackData>;
|
||||||
public readonly components: ActionRowBuilder<ModalActionRowComponentBuilder>[] = [];
|
public readonly components: ActionRowBuilder<ModalActionRowComponentBuilder>[] = [];
|
||||||
|
|
||||||
public constructor({ components, ...data }: Partial<APIModalInteractionResponseCallbackData> = {}) {
|
public constructor({ components, ...data }: Partial<APIModalInteractionResponseCallbackData> = {}) {
|
||||||
|
|||||||
20
packages/discord.js/typings/index.d.ts
vendored
20
packages/discord.js/typings/index.d.ts
vendored
@@ -28,6 +28,7 @@ import {
|
|||||||
userMention,
|
userMention,
|
||||||
ModalActionRowComponentBuilder,
|
ModalActionRowComponentBuilder,
|
||||||
ModalBuilder as BuildersModal,
|
ModalBuilder as BuildersModal,
|
||||||
|
AnyComponentBuilder,
|
||||||
} from '@discordjs/builders';
|
} from '@discordjs/builders';
|
||||||
import { Collection } from '@discordjs/collection';
|
import { Collection } from '@discordjs/collection';
|
||||||
import { BaseImageURLOptions, ImageURLOptions, RawFile, REST, RESTOptions } from '@discordjs/rest';
|
import { BaseImageURLOptions, ImageURLOptions, RawFile, REST, RESTOptions } from '@discordjs/rest';
|
||||||
@@ -232,17 +233,12 @@ export interface ActionRowData<T extends ActionRowComponentBuilder | ActionRowCo
|
|||||||
components: T[];
|
components: T[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ActionRowBuilder<
|
export class ActionRowBuilder<T extends AnyComponentBuilder = AnyComponentBuilder> extends BuilderActionRow<T> {
|
||||||
T extends MessageActionRowComponentBuilder | ModalActionRowComponentBuilder =
|
|
||||||
| MessageActionRowComponentBuilder
|
|
||||||
| ModalActionRowComponentBuilder,
|
|
||||||
> extends BuilderActionRow<T> {
|
|
||||||
constructor(
|
constructor(
|
||||||
data?:
|
data?: Partial<
|
||||||
| ActionRowData<ActionRowComponentData | ActionRowComponentBuilder>
|
| ActionRowData<ActionRowComponentData | ActionRowComponentBuilder>
|
||||||
| (Omit<APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>, 'type'> & {
|
| APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>
|
||||||
type?: ComponentType.ActionRow;
|
>,
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
public static from(
|
public static from(
|
||||||
other:
|
other:
|
||||||
@@ -577,9 +573,7 @@ export class ButtonBuilder extends BuilderButtonComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class SelectMenuBuilder extends BuilderSelectMenuComponent {
|
export class SelectMenuBuilder extends BuilderSelectMenuComponent {
|
||||||
public constructor(
|
public constructor(data?: Partial<SelectMenuComponentData | APISelectMenuComponent>);
|
||||||
data?: SelectMenuComponentData | (Omit<APISelectMenuComponent, 'type'> & { type?: ComponentType.SelectMenu }),
|
|
||||||
);
|
|
||||||
public static from(other: JSONEncodable<APISelectMenuComponent> | APISelectMenuComponent): SelectMenuBuilder;
|
public static from(other: JSONEncodable<APISelectMenuComponent> | APISelectMenuComponent): SelectMenuBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -593,7 +587,7 @@ export class ModalBuilder extends BuildersModal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class TextInputBuilder extends BuilderTextInputComponent {
|
export class TextInputBuilder extends BuilderTextInputComponent {
|
||||||
public constructor(data?: TextInputComponentData | APITextInputComponent);
|
public constructor(data?: Partial<TextInputComponentData | APITextInputComponent>);
|
||||||
public static from(other: JSONEncodable<APITextInputComponent> | APITextInputComponent): TextInputBuilder;
|
public static from(other: JSONEncodable<APITextInputComponent> | APITextInputComponent): TextInputBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -808,8 +808,8 @@ client.on('interactionCreate', async interaction => {
|
|||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
interaction.reply({ content: 'Hi!', components: [[button]] });
|
interaction.reply({ content: 'Hi!', components: [[button]] });
|
||||||
|
|
||||||
// @ts-expect-error
|
|
||||||
void new ActionRowBuilder({});
|
void new ActionRowBuilder({});
|
||||||
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
await interaction.reply({ content: 'Hi!', components: [button] });
|
await interaction.reply({ content: 'Hi!', components: [button] });
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user