-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Components.ts
31 lines (29 loc) · 1.25 KB
/
Components.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { APIMessageComponent, ComponentType } from 'discord-api-types/v9';
import { ActionRow, ButtonComponent, Component, SelectMenuComponent } from '../index';
import type { MessageComponent } from './ActionRow';
export interface MappedComponentTypes {
[ComponentType.ActionRow]: ActionRow;
[ComponentType.Button]: ButtonComponent;
[ComponentType.SelectMenu]: SelectMenuComponent;
}
/**
* Factory for creating components from API data
* @param data The api data to transform to a component class
*/
export function createComponent<T extends keyof MappedComponentTypes>(
data: APIMessageComponent & { type: T },
): MappedComponentTypes[T];
export function createComponent<C extends MessageComponent>(data: C): C;
export function createComponent(data: APIMessageComponent | MessageComponent): Component {
switch (data.type) {
case ComponentType.ActionRow:
return data instanceof ActionRow ? data : new ActionRow(data);
case ComponentType.Button:
return data instanceof ButtonComponent ? data : new ButtonComponent(data);
case ComponentType.SelectMenu:
return data instanceof SelectMenuComponent ? data : new SelectMenuComponent(data);
default:
// @ts-expect-error
throw new Error(`Cannot serialize component type: ${data.type as number}`);
}
}