From d6b56d0080c4c5f8ace731f1e8bcae0c9d3fb5a5 Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Wed, 26 Jan 2022 09:23:58 +0000 Subject: [PATCH] fix: don't create new instances of builders classes (#7343) --- packages/builders/src/components/ActionRow.ts | 4 +++- packages/builders/src/components/Components.ts | 13 +++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/builders/src/components/ActionRow.ts b/packages/builders/src/components/ActionRow.ts index d3eec578b061..3da6b625f8df 100644 --- a/packages/builders/src/components/ActionRow.ts +++ b/packages/builders/src/components/ActionRow.ts @@ -3,6 +3,8 @@ import type { ButtonComponent, SelectMenuComponent } from '..'; import type { Component } from './Component'; import { createComponent } from './Components'; +export type MessageComponent = ActionRowComponent | ActionRow; + export type ActionRowComponent = ButtonComponent | SelectMenuComponent; // TODO: Add valid form component types @@ -10,7 +12,7 @@ export type ActionRowComponent = ButtonComponent | SelectMenuComponent; /** * Represents an action row component */ -export class ActionRow implements Component { +export class ActionRow implements Component { public readonly components: T[] = []; public readonly type = ComponentType.ActionRow; diff --git a/packages/builders/src/components/Components.ts b/packages/builders/src/components/Components.ts index 7060fe245903..f0cb5d95eebb 100644 --- a/packages/builders/src/components/Components.ts +++ b/packages/builders/src/components/Components.ts @@ -1,9 +1,9 @@ import { APIMessageComponent, ComponentType } from 'discord-api-types/v9'; import { ActionRow, ButtonComponent, Component, SelectMenuComponent } from '../index'; -import type { ActionRowComponent } from './ActionRow'; +import type { MessageComponent } from './ActionRow'; export interface MappedComponentTypes { - [ComponentType.ActionRow]: ActionRow; + [ComponentType.ActionRow]: ActionRow; [ComponentType.Button]: ButtonComponent; [ComponentType.SelectMenu]: SelectMenuComponent; } @@ -15,14 +15,15 @@ export interface MappedComponentTypes { export function createComponent( data: APIMessageComponent & { type: T }, ): MappedComponentTypes[T]; -export function createComponent(data: APIMessageComponent): Component { +export function createComponent(data: C): C; +export function createComponent(data: APIMessageComponent | MessageComponent): Component { switch (data.type) { case ComponentType.ActionRow: - return new ActionRow(data); + return data instanceof ActionRow ? data : new ActionRow(data); case ComponentType.Button: - return new ButtonComponent(data); + return data instanceof ButtonComponent ? data : new ButtonComponent(data); case ComponentType.SelectMenu: - return new SelectMenuComponent(data); + return data instanceof SelectMenuComponent ? data : new SelectMenuComponent(data); default: // @ts-expect-error throw new Error(`Cannot serialize component type: ${data.type as number}`);