v2componentsbuilder
Version:
A discord.js v2components builder
43 lines (42 loc) • 1.29 kB
JavaScript
import { ComponentType } from 'discord-api-types/v10';
const ACCESSORY_TYPES = [
ComponentType.Button,
ComponentType.StringSelect,
ComponentType.UserSelect,
ComponentType.RoleSelect,
ComponentType.MentionableSelect,
ComponentType.ChannelSelect,
ComponentType.Thumbnail,
];
export class V2SectionBuilder {
constructor() {
this.type = ComponentType.Section;
}
setId(id) {
this.id = id;
return this;
}
setComponents(components) {
if (components.length < 1 || components.length > 3)
throw new Error('Sections must contain between 1 and 3 text display components.');
this.components = components.map(c => c.toJSON());
return this;
}
setAccessory(component) {
component = component.toJSON();
if (!ACCESSORY_TYPES.includes(component.type)) {
throw new Error(`Invalid accessory type ${component.type}. ` +
`Must be one of: ${ACCESSORY_TYPES.join(', ')}.`);
}
this.accessory = component;
return this;
}
toJSON() {
return {
id: this.id ?? undefined,
type: ComponentType.Section,
components: this.components,
accessory: this.accessory,
};
}
}