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