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}`);