UNPKG

v2componentsbuilder

Version:
64 lines (63 loc) 1.75 kB
import { ComponentType, ButtonStyle } from 'discord-api-types/v10'; export class V2ButtonBuilder { constructor() { } setCustomId(custom_id) { if (custom_id.length > 100) throw Error("Max length for custom ids is 100 characters"); this.custom_id = custom_id; return this; } setLabel(label) { if (label.length > 80) throw Error("Max length for custom ids is 100 characters"); this.label = label; return this; } setEmoji(emoji) { this.emoji = emoji; return this; } setStyle(style) { this.style = style; return this; } setURL(url) { this.url = url; return this; } setDisabled(disabled) { this.disabled = disabled; return this; } setSKU(sku_id) { this.sku_id = sku_id; return this; } toJSON() { if (this.style === ButtonStyle.Link) { if (!this.url) throw new Error("Link buttons must have a URL"); return { type: ComponentType.Button, style: ButtonStyle.Link, label: this.label, emoji: this.emoji, url: this.url, disabled: this.disabled ?? false, }; } else { if (!this.custom_id) throw new Error("Non-link buttons must have a custom_id"); return { type: ComponentType.Button, style: this.style, custom_id: this.custom_id, label: this.label, emoji: this.emoji, disabled: this.disabled ?? false, }; } } }