mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
fix: don't create new instances of builders classes (#7343)
This commit is contained in:
@@ -3,6 +3,8 @@ import type { ButtonComponent, SelectMenuComponent } from '..';
|
|||||||
import type { Component } from './Component';
|
import type { Component } from './Component';
|
||||||
import { createComponent } from './Components';
|
import { createComponent } from './Components';
|
||||||
|
|
||||||
|
export type MessageComponent = ActionRowComponent | ActionRow;
|
||||||
|
|
||||||
export type ActionRowComponent = ButtonComponent | SelectMenuComponent;
|
export type ActionRowComponent = ButtonComponent | SelectMenuComponent;
|
||||||
|
|
||||||
// TODO: Add valid form component types
|
// TODO: Add valid form component types
|
||||||
@@ -10,7 +12,7 @@ export type ActionRowComponent = ButtonComponent | SelectMenuComponent;
|
|||||||
/**
|
/**
|
||||||
* Represents an action row component
|
* Represents an action row component
|
||||||
*/
|
*/
|
||||||
export class ActionRow<T extends ActionRowComponent> implements Component {
|
export class ActionRow<T extends ActionRowComponent = ActionRowComponent> implements Component {
|
||||||
public readonly components: T[] = [];
|
public readonly components: T[] = [];
|
||||||
public readonly type = ComponentType.ActionRow;
|
public readonly type = ComponentType.ActionRow;
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { APIMessageComponent, ComponentType } from 'discord-api-types/v9';
|
import { APIMessageComponent, ComponentType } from 'discord-api-types/v9';
|
||||||
import { ActionRow, ButtonComponent, Component, SelectMenuComponent } from '../index';
|
import { ActionRow, ButtonComponent, Component, SelectMenuComponent } from '../index';
|
||||||
import type { ActionRowComponent } from './ActionRow';
|
import type { MessageComponent } from './ActionRow';
|
||||||
|
|
||||||
export interface MappedComponentTypes {
|
export interface MappedComponentTypes {
|
||||||
[ComponentType.ActionRow]: ActionRow<ActionRowComponent>;
|
[ComponentType.ActionRow]: ActionRow;
|
||||||
[ComponentType.Button]: ButtonComponent;
|
[ComponentType.Button]: ButtonComponent;
|
||||||
[ComponentType.SelectMenu]: SelectMenuComponent;
|
[ComponentType.SelectMenu]: SelectMenuComponent;
|
||||||
}
|
}
|
||||||
@@ -15,14 +15,15 @@ export interface MappedComponentTypes {
|
|||||||
export function createComponent<T extends keyof MappedComponentTypes>(
|
export function createComponent<T extends keyof MappedComponentTypes>(
|
||||||
data: APIMessageComponent & { type: T },
|
data: APIMessageComponent & { type: T },
|
||||||
): MappedComponentTypes[T];
|
): MappedComponentTypes[T];
|
||||||
export function createComponent(data: APIMessageComponent): Component {
|
export function createComponent<C extends MessageComponent>(data: C): C;
|
||||||
|
export function createComponent(data: APIMessageComponent | MessageComponent): Component {
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case ComponentType.ActionRow:
|
case ComponentType.ActionRow:
|
||||||
return new ActionRow(data);
|
return data instanceof ActionRow ? data : new ActionRow(data);
|
||||||
case ComponentType.Button:
|
case ComponentType.Button:
|
||||||
return new ButtonComponent(data);
|
return data instanceof ButtonComponent ? data : new ButtonComponent(data);
|
||||||
case ComponentType.SelectMenu:
|
case ComponentType.SelectMenu:
|
||||||
return new SelectMenuComponent(data);
|
return data instanceof SelectMenuComponent ? data : new SelectMenuComponent(data);
|
||||||
default:
|
default:
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
throw new Error(`Cannot serialize component type: ${data.type as number}`);
|
throw new Error(`Cannot serialize component type: ${data.type as number}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user