discord-container-builder
Version:
A simplified, developer-friendly API for Discord.js v2 Components that reduces boilerplate and improves code readability.
634 lines (625 loc) • 19.3 kB
JavaScript
import { TextDisplayBuilder as TextDisplayBuilder$1, SeparatorBuilder as SeparatorBuilder$1, SeparatorSpacingSize, ButtonBuilder as ButtonBuilder$1, ButtonStyle, StringSelectMenuBuilder as StringSelectMenuBuilder$1, StringSelectMenuOptionBuilder, ActionRowBuilder as ActionRowBuilder$1, ContainerBuilder as ContainerBuilder$1 } from 'discord.js';
export { ButtonStyle, MessageFlags, SeparatorSpacingSize } from 'discord.js';
/**
* Simplified TextDisplayBuilder that provides a cleaner API for creating text display components
*/
class TextDisplayBuilder {
constructor() {
this.textDisplay = new TextDisplayBuilder$1();
}
/**
* Set the content of the text display
* @param content - The text content to display
* @returns The TextDisplayBuilder instance for chaining
*/
setContent(content) {
this.textDisplay.setContent(content);
return this;
}
/**
* Set bold text content
* @param content - The text content to display in bold
* @returns The TextDisplayBuilder instance for chaining
*/
setBoldContent(content) {
return this.setContent(`**${content}**`);
}
/**
* Set italic text content
* @param content - The text content to display in italics
* @returns The TextDisplayBuilder instance for chaining
*/
setItalicContent(content) {
return this.setContent(`*${content}*`);
}
/**
* Set code text content
* @param content - The text content to display as code
* @returns The TextDisplayBuilder instance for chaining
*/
setCodeContent(content) {
return this.setContent(`\`${content}\``);
}
/**
* Set multiline content with automatic formatting
* @param lines - Array of text lines
* @returns The TextDisplayBuilder instance for chaining
*/
setLines(lines) {
return this.setContent(lines.join('\n'));
}
/**
* Add an emoji prefix to the content
* @param emoji - The emoji to prefix
* @param content - The text content
* @returns The TextDisplayBuilder instance for chaining
*/
setEmojiContent(emoji, content) {
return this.setContent(`${emoji} ${content}`);
}
/**
* Build the final Discord.js TextDisplayBuilder
* @returns The Discord.js TextDisplayBuilder instance
*/
build() {
return this.textDisplay;
}
/**
* Get the JSON representation of the text display
* @returns The JSON data
*/
toJSON() {
return this.textDisplay.toJSON();
}
}
/**
* Simplified SeparatorBuilder that provides a cleaner API for creating separator components
*/
class SeparatorBuilder {
constructor() {
this.separator = new SeparatorBuilder$1();
}
/**
* Set the spacing of the separator
* @param spacing - The spacing size
* @returns The SeparatorBuilder instance for chaining
*/
setSpacing(spacing) {
this.separator.setSpacing(spacing);
return this;
}
/**
* Set whether to show a divider line
* @param divider - Whether to show a divider
* @returns The SeparatorBuilder instance for chaining
*/
setDivider(divider) {
this.separator.setDivider(divider);
return this;
}
/**
* Set small spacing
* @returns The SeparatorBuilder instance for chaining
*/
setSmallSpacing() {
return this.setSpacing(SeparatorSpacingSize.Small);
}
/**
* Set medium spacing (alias for large spacing since Medium doesn't exist)
* @returns The SeparatorBuilder instance for chaining
*/
setMediumSpacing() {
return this.setSpacing(SeparatorSpacingSize.Large);
}
/**
* Set large spacing
* @returns The SeparatorBuilder instance for chaining
*/
setLargeSpacing() {
return this.setSpacing(SeparatorSpacingSize.Large);
}
/**
* Create a divider with small spacing
* @returns The SeparatorBuilder instance for chaining
*/
asDivider() {
return this.setDivider(true).setSmallSpacing();
}
/**
* Create a spacer without divider
* @param size - The spacing size (defaults to large)
* @returns The SeparatorBuilder instance for chaining
*/
asSpacer(size = SeparatorSpacingSize.Large) {
return this.setDivider(false).setSpacing(size);
}
/**
* Build the final Discord.js SeparatorBuilder
* @returns The Discord.js SeparatorBuilder instance
*/
build() {
return this.separator;
}
/**
* Get the JSON representation of the separator
* @returns The JSON data
*/
toJSON() {
return this.separator.toJSON();
}
}
/**
* Simplified ButtonBuilder that provides a cleaner API for creating button components
*/
class ButtonBuilder {
constructor() {
this.button = new ButtonBuilder$1();
}
/**
* Set the custom ID of the button
* @param customId - The custom ID
* @returns The ButtonBuilder instance for chaining
*/
setCustomId(customId) {
this.button.setCustomId(customId);
return this;
}
/**
* Set the label of the button
* @param label - The button label
* @returns The ButtonBuilder instance for chaining
*/
setLabel(label) {
this.button.setLabel(label);
return this;
}
/**
* Set the style of the button
* @param style - The button style
* @returns The ButtonBuilder instance for chaining
*/
setStyle(style) {
this.button.setStyle(style);
return this;
}
/**
* Set the emoji of the button
* @param emoji - The emoji
* @returns The ButtonBuilder instance for chaining
*/
setEmoji(emoji) {
this.button.setEmoji(emoji);
return this;
}
/**
* Set the URL for link buttons
* @param url - The URL
* @returns The ButtonBuilder instance for chaining
*/
setURL(url) {
this.button.setURL(url);
return this;
}
/**
* Set whether the button is disabled
* @param disabled - Whether the button is disabled
* @returns The ButtonBuilder instance for chaining
*/
setDisabled(disabled = true) {
this.button.setDisabled(disabled);
return this;
}
/**
* Create a primary button
* @param customId - The custom ID
* @param label - The button label
* @returns The ButtonBuilder instance for chaining
*/
asPrimary(customId, label) {
return this.setCustomId(customId).setLabel(label).setStyle(ButtonStyle.Primary);
}
/**
* Create a secondary button
* @param customId - The custom ID
* @param label - The button label
* @returns The ButtonBuilder instance for chaining
*/
asSecondary(customId, label) {
return this.setCustomId(customId).setLabel(label).setStyle(ButtonStyle.Secondary);
}
/**
* Create a success button
* @param customId - The custom ID
* @param label - The button label
* @returns The ButtonBuilder instance for chaining
*/
asSuccess(customId, label) {
return this.setCustomId(customId).setLabel(label).setStyle(ButtonStyle.Success);
}
/**
* Create a danger button
* @param customId - The custom ID
* @param label - The button label
* @returns The ButtonBuilder instance for chaining
*/
asDanger(customId, label) {
return this.setCustomId(customId).setLabel(label).setStyle(ButtonStyle.Danger);
}
/**
* Create a link button
* @param url - The URL
* @param label - The button label
* @returns The ButtonBuilder instance for chaining
*/
asLink(url, label) {
return this.setURL(url).setLabel(label).setStyle(ButtonStyle.Link);
}
/**
* Build the final Discord.js ButtonBuilder
* @returns The Discord.js ButtonBuilder instance
*/
build() {
return this.button;
}
/**
* Get the JSON representation of the button
* @returns The JSON data
*/
toJSON() {
return this.button.toJSON();
}
}
/**
* Simplified StringSelectMenuBuilder that provides a cleaner API for creating select menu components
*/
class StringSelectMenuBuilder {
constructor() {
this.selectMenu = new StringSelectMenuBuilder$1();
}
/**
* Set the custom ID of the select menu
* @param customId - The custom ID
* @returns The StringSelectMenuBuilder instance for chaining
*/
setCustomId(customId) {
this.selectMenu.setCustomId(customId);
return this;
}
/**
* Set the placeholder text
* @param placeholder - The placeholder text
* @returns The StringSelectMenuBuilder instance for chaining
*/
setPlaceholder(placeholder) {
this.selectMenu.setPlaceholder(placeholder);
return this;
}
/**
* Set the minimum number of values that can be selected
* @param min - The minimum values
* @returns The StringSelectMenuBuilder instance for chaining
*/
setMinValues(min) {
this.selectMenu.setMinValues(min);
return this;
}
/**
* Set the maximum number of values that can be selected
* @param max - The maximum values
* @returns The StringSelectMenuBuilder instance for chaining
*/
setMaxValues(max) {
this.selectMenu.setMaxValues(max);
return this;
}
/**
* Set whether the select menu is disabled
* @param disabled - Whether the select menu is disabled
* @returns The StringSelectMenuBuilder instance for chaining
*/
setDisabled(disabled = true) {
this.selectMenu.setDisabled(disabled);
return this;
}
/**
* Add options to the select menu
* @param options - Array of option objects or StringSelectMenuOptionBuilder instances
* @returns The StringSelectMenuBuilder instance for chaining
*/
addOptions(options) {
const discordOptions = options.map(option => {
if (option instanceof StringSelectMenuOptionBuilder) {
return option;
}
const builder = new StringSelectMenuOptionBuilder()
.setLabel(option.label)
.setValue(option.value);
if (option.description)
builder.setDescription(option.description);
if (option.emoji)
builder.setEmoji(option.emoji);
if (option.default)
builder.setDefault(option.default);
return builder;
});
this.selectMenu.addOptions(...discordOptions);
return this;
}
/**
* Add a single option with simplified API
* @param label - The option label
* @param value - The option value
* @param description - Optional description
* @param emoji - Optional emoji
* @returns The StringSelectMenuBuilder instance for chaining
*/
addOption(label, value, description, emoji) {
const option = { label, value };
if (description !== undefined)
option.description = description;
if (emoji !== undefined)
option.emoji = emoji;
return this.addOptions([option]);
}
/**
* Set options (replaces existing options)
* @param options - Array of option objects
* @returns The StringSelectMenuBuilder instance for chaining
*/
setOptions(options) {
this.selectMenu.setOptions([]);
return this.addOptions(options);
}
/**
* Build the final Discord.js StringSelectMenuBuilder
* @returns The Discord.js StringSelectMenuBuilder instance
*/
build() {
return this.selectMenu;
}
/**
* Get the JSON representation of the select menu
* @returns The JSON data
*/
toJSON() {
return this.selectMenu.toJSON();
}
}
/**
* Simplified ActionRowBuilder that provides a cleaner API for creating action row components
*/
class ActionRowBuilder {
constructor() {
this.actionRow = new ActionRowBuilder$1();
}
/**
* Add components to the action row
* @param components - Button or SelectMenu components
* @returns The ActionRowBuilder instance for chaining
*/
addComponents(...components) {
const discordComponents = components.map(component => {
if (component instanceof ButtonBuilder || component instanceof StringSelectMenuBuilder) {
return component.build();
}
return component;
});
this.actionRow.addComponents(...discordComponents);
return this;
}
/**
* Add a button with simplified API
* @param callback - Function to configure the button
* @returns The ActionRowBuilder instance for chaining
*/
addButton(callback) {
const button = new ButtonBuilder();
callback(button);
return this.addComponents(button);
}
/**
* Add multiple buttons with simplified API
* @param configs - Array of button configurations
* @returns The ActionRowBuilder instance for chaining
*/
addButtons(configs) {
configs.forEach(config => this.addButton(config));
return this;
}
/**
* Add a select menu with simplified API
* @param callback - Function to configure the select menu
* @returns The ActionRowBuilder instance for chaining
*/
addSelectMenu(callback) {
const menu = new StringSelectMenuBuilder();
callback(menu);
return this.addComponents(menu);
}
/**
* Build the final Discord.js ActionRowBuilder
* @returns The Discord.js ActionRowBuilder instance
*/
build() {
return this.actionRow;
}
/**
* Get the JSON representation of the action row
* @returns The JSON data
*/
toJSON() {
return this.actionRow.toJSON();
}
}
/**
* Simplified ContainerBuilder that provides a cleaner API for creating Discord.js v2 Container components
*/
class ContainerBuilder {
constructor() {
this.container = new ContainerBuilder$1();
}
/**
* Add text display components to the container
* @param components - TextDisplayBuilder instances or raw Discord TextDisplayBuilders
* @returns The ContainerBuilder instance for chaining
*/
addTextDisplayComponents(...components) {
const discordComponents = components.map(component => component instanceof TextDisplayBuilder ? component.build() : component);
this.container.addTextDisplayComponents(...discordComponents);
return this;
}
/**
* Add separator components to the container
* @param components - SeparatorBuilder instances or raw Discord SeparatorBuilders
* @returns The ContainerBuilder instance for chaining
*/
addSeparatorComponents(...components) {
const discordComponents = components.map(component => component instanceof SeparatorBuilder ? component.build() : component);
this.container.addSeparatorComponents(...discordComponents);
return this;
}
/**
* Add action row components to the container
* @param components - ActionRowBuilder instances or raw Discord ActionRowBuilders
* @returns The ContainerBuilder instance for chaining
*/
addActionRowComponents(...components) {
const discordComponents = components.map(component => component instanceof ActionRowBuilder ? component.build() : component);
// Type assertion needed because our ActionRowBuilder is designed for message components
this.container.addActionRowComponents(...discordComponents);
return this;
}
/**
* Add a text display with simplified API
* @param content - The text content to display
* @returns The ContainerBuilder instance for chaining
*/
addText(content) {
return this.addTextDisplayComponents(new TextDisplayBuilder().setContent(content));
}
/**
* Add a separator with simplified API
* @param options - Optional separator configuration
* @returns The ContainerBuilder instance for chaining
*/
addSeparator(options) {
const separator = new SeparatorBuilder();
if (options?.spacing)
separator.setSpacing(options.spacing);
if (options?.divider !== undefined)
separator.setDivider(options.divider);
return this.addSeparatorComponents(separator);
}
/**
* Add an action row with simplified API
* @param callback - Function to configure the action row
* @returns The ContainerBuilder instance for chaining
*/
addActionRow(callback) {
const row = new ActionRowBuilder();
callback(row);
return this.addActionRowComponents(row);
}
/**
* Build the final Discord.js ContainerBuilder
* @returns The Discord.js ContainerBuilder instance
*/
build() {
return this.container;
}
/**
* Get the JSON representation of the container
* @returns The JSON data
*/
toJSON() {
return this.container.toJSON();
}
}
/**
* Common button styles for easy access
*/
const BUTTON_STYLES = {
PRIMARY: ButtonStyle.Primary,
SECONDARY: ButtonStyle.Secondary,
SUCCESS: ButtonStyle.Success,
DANGER: ButtonStyle.Danger,
LINK: ButtonStyle.Link,
};
/**
* Common separator spacing sizes for easy access
*/
const SPACING_SIZES = {
SMALL: SeparatorSpacingSize.Small,
LARGE: SeparatorSpacingSize.Large,
};
/**
* Common emoji shortcuts
*/
const EMOJIS = {
SUCCESS: '✅',
ERROR: '❌',
WARNING: '⚠️',
INFO: 'ℹ️',
LOADING: '⏳',
STAR: '⭐',
HEART: '❤️',
THUMBS_UP: '👍',
THUMBS_DOWN: '👎',
FIRE: '🔥',
ROCKET: '🚀',
MONEY: '💰',
CROWN: '👑',
GEAR: '⚙️',
BOOK: '📚',
MAIL: '📧',
PHONE: '📞',
HOME: '🏠',
USER: '👤',
USERS: '👥',
LOCK: '🔒',
UNLOCK: '🔓',
KEY: '🔑',
SEARCH: '🔍',
CALENDAR: '📅',
CLOCK: '🕐',
CHART: '📊',
GRAPH: '📈',
FOLDER: '📁',
FILE: '📄',
LINK: '🔗',
IMAGE: '🖼️',
VIDEO: '🎥',
MUSIC: '🎵',
GAME: '🎮',
TROPHY: '🏆',
MEDAL: '🏅',
FLAG: '🚩',
BELL: '🔔',
MUTE: '🔇',
VOLUME: '🔊',
PLUS: '➕',
MINUS: '➖',
MULTIPLY: '✖️',
DIVIDE: '➗',
EQUALS: '🟰',
QUESTION: '❓',
EXCLAMATION: '❗',
ARROW_LEFT: '⬅️',
ARROW_RIGHT: '➡️',
ARROW_UP: '⬆️',
ARROW_DOWN: '⬇️',
};
/**
* Validation constants
*/
const VALIDATION = {
MAX_COMPONENTS_PER_ACTION_ROW: 5,
MAX_ACTION_ROWS_PER_MESSAGE: 5,
MAX_BUTTON_LABEL_LENGTH: 80,
MAX_SELECT_OPTION_LABEL_LENGTH: 100,
MAX_SELECT_OPTION_DESCRIPTION_LENGTH: 100,
MAX_SELECT_OPTIONS: 25,
MAX_CUSTOM_ID_LENGTH: 100,
};
export { ActionRowBuilder, BUTTON_STYLES, ButtonBuilder, ContainerBuilder, EMOJIS, SPACING_SIZES, SeparatorBuilder, StringSelectMenuBuilder, TextDisplayBuilder, VALIDATION };
//# sourceMappingURL=index.esm.js.map