refactor: make public builder props getters (#7422)

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
This commit is contained in:
Suneet Tipirneni
2022-02-13 07:06:11 -05:00
committed by GitHub
parent 3ae6f3c313
commit e8252ed3b9
17 changed files with 370 additions and 233 deletions

View File

@@ -1,6 +1,6 @@
import { APIActionRowComponent, ComponentType } from 'discord-api-types/v9';
import { type APIActionRowComponent, ComponentType } from 'discord-api-types/v9';
import type { ButtonComponent, SelectMenuComponent } from '..';
import type { Component } from './Component';
import { Component } from './Component';
import { createComponent } from './Components';
export type MessageComponent = ActionRowComponent | ActionRow;
@@ -8,16 +8,17 @@ export type MessageComponent = ActionRowComponent | ActionRow;
export type ActionRowComponent = ButtonComponent | SelectMenuComponent;
// TODO: Add valid form component types
/**
* Represents an action row component
*/
export class ActionRow<T extends ActionRowComponent = ActionRowComponent> implements Component {
public readonly components: T[] = [];
public readonly type = ComponentType.ActionRow;
export class ActionRow<T extends ActionRowComponent = ActionRowComponent> extends Component<
Omit<Partial<APIActionRowComponent> & { type: ComponentType.ActionRow }, 'components'>
> {
public readonly components: T[];
public constructor(data?: APIActionRowComponent & { type?: ComponentType.ActionRow }) {
this.components = (data?.components.map(createComponent) ?? []) as T[];
public constructor({ components, ...data }: Partial<APIActionRowComponent> = {}) {
super({ type: ComponentType.ActionRow, ...data });
this.components = (components?.map((c) => createComponent(c)) ?? []) as T[];
}
/**
@@ -34,14 +35,14 @@ export class ActionRow<T extends ActionRowComponent = ActionRowComponent> implem
* Sets the components in this action row
* @param components The components to set this row to
*/
public setComponents(...components: T[]) {
Reflect.set(this, 'components', [...components]);
public setComponents(components: T[]) {
this.components.splice(0, this.components.length, ...components);
return this;
}
public toJSON(): APIActionRowComponent {
return {
...this,
...this.data,
components: this.components.map((component) => component.toJSON()),
};
}