### Initialize a Discord.js Components v2 ContainerBuilder Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Every Components v2 message starts with a `ContainerBuilder`. This builder allows setting global properties like accent color and spoiler status for the entire message, acting as the foundational canvas. ```js const container = new ContainerBuilder() .setAccentColor(0x5865F2) .setSpoiler(true); ``` -------------------------------- ### Create a Discord Dashboard Layout with Components v2 Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Example of building a dashboard-like UI using `ContainerBuilder` to set an accent color, and then populating it with `TextDisplayBuilder` for titles and stats, `SeparatorBuilder` for visual breaks, and `SectionBuilder` with `ButtonBuilder` for interactive elements like a refresh button. ```js const dashboard = new ContainerBuilder() .setAccentColor(0x2F3136) .addTextDisplayComponents( new TextDisplayBuilder().setContent("# Server Dashboard") ) .addSeparatorComponents( new SeparatorBuilder().setSpacing(SeparatorSpacingSize.Small) ) .addSectionComponents( new SectionBuilder() .addTextDisplayComponents( new TextDisplayBuilder().setContent("**Members Online**: 1,234"), new TextDisplayBuilder().setContent("**Messages Today**: 5,678") ) .setButtonAccessory( new ButtonBuilder() .setCustomId('refresh_stats') .setLabel('Refresh') .setStyle(ButtonStyle.Secondary) ) ); ``` -------------------------------- ### Design a Product Showcase UI with Discord Components v2 Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Illustrates how to construct a product showcase interface using `ContainerBuilder`. It combines `TextDisplayBuilder` for product name and price, `MediaGalleryBuilder` for multiple images, `SectionBuilder` for feature lists, and `ActionRowBuilder` with `ButtonBuilder` for purchase and 'learn more' actions. ```js const product = new ContainerBuilder() .addTextDisplayComponents( new TextDisplayBuilder().setContent("## Premium Discord Bot\n**$19.99/month**") ) .addMediaGalleryComponents( new MediaGalleryBuilder() .addItems( new MediaGalleryItemBuilder() .setURL('https://example.com/preview1.png') .setDescription('Main interface'), new MediaGalleryItemBuilder() .setURL('https://example.com/preview2.png') .setDescription('Configuration panel') ) ) .addSectionComponents( new SectionBuilder() .addTextDisplayComponents( new TextDisplayBuilder().setContent("āœ… Advanced moderation\nāœ… Custom commands\nāœ… 24/7 support") ) ) .addActionRowComponents( new ActionRowBuilder() .addComponents( new ButtonBuilder() .setCustomId('purchase') .setLabel('Buy Now') .setStyle(ButtonStyle.Success), new ButtonBuilder() .setCustomId('learn_more') .setLabel('Learn More') .setStyle(ButtonStyle.Primary) ) ); ``` -------------------------------- ### Display Multiple Media Items with Discord.js MediaGalleryBuilder Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md The `MediaGalleryBuilder` allows displaying up to 10 images, videos, or GIFs within a message, replacing traditional embed images. Each item can have its own description, providing rich visual content. ```js new MediaGalleryBuilder() .addItems( new MediaGalleryItemBuilder() .setURL('https://example.com/image1.png') .setDescription('Product showcase'), new MediaGalleryItemBuilder() .setURL('https://example.com/image2.jpg') .setDescription('Different angle') ); ``` -------------------------------- ### Organize Content Blocks with Discord.js SectionBuilder Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md The `SectionBuilder` groups related content, allowing up to three text displays and an optional button or thumbnail accessory. It helps create a clean, organized layout with a clear left-to-right information flow. ```js new SectionBuilder() .addTextDisplayComponents( new TextDisplayBuilder().setContent("Primary information"), new TextDisplayBuilder().setContent("Secondary details"), new TextDisplayBuilder().setContent("Additional context") ) .setButtonAccessory( new ButtonBuilder() .setCustomId('section_action') .setLabel('Take Action') .setStyle(ButtonStyle.Primary) ); ``` -------------------------------- ### Implement Vertical Flow Layout with Discord Components v2 Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Demonstrates how to arrange components vertically within a container using a sequence of `addTextDisplayComponents`, `addSeparatorComponents`, `addSectionComponents`, `addMediaGalleryComponents`, and `addActionRowComponents` to create a top-to-bottom content flow. ```js container .addTextDisplayComponents(header) .addSeparatorComponents(spacer) .addSectionComponents(mainContent) .addMediaGalleryComponents(images) .addActionRowComponents(actions); ``` -------------------------------- ### Map Discord Embed Features to Components v2 Equivalents Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Provides a direct mapping of common Discord Embed features to their corresponding components and builders in Discord Components v2, aiding developers in migrating existing embed-based UIs to the more flexible Components v2 system. ```APIDOC Embed Feature | Components v2 Equivalent --------------|------------------------- `setTitle()` | `TextDisplayBuilder` with `# Header` `setDescription()` | `TextDisplayBuilder` with content `addFields()` | Multiple `SectionBuilder` components `setImage()` | `MediaGalleryBuilder` `setThumbnail()` | `ThumbnailBuilder` in section accessory `setColor()` | `ContainerBuilder.setAccentColor()` ``` -------------------------------- ### Organize Discord Components v2 UI Building Blocks in JavaScript Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Presents a `ComponentsV2Builder` static class with helper methods for creating common Discord Components v2 elements like headers, info sections, and spacers. This promotes code reusability and a structured approach to UI construction. ```js class ComponentsV2Builder { static createHeader(title, subtitle) { return new TextDisplayBuilder() .setContent(`# ${title}\n${subtitle || ''}`); } static createInfoSection(title, content, action) { const section = new SectionBuilder() .addTextDisplayComponents( new TextDisplayBuilder().setContent(`**${title}**\n${content}`) ); if (action) { section.setButtonAccessory(action); } return section; } static createSpacer(size = SeparatorSpacingSize.Small, divider = false) { return new SeparatorBuilder() .setSpacing(size) .setDivider(divider); } } ``` -------------------------------- ### Display File Attachments Inline with Discord.js FileBuilder Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md The `FileBuilder` enables displaying file attachments as inline previews directly within the message layout, rather than as separate attachments below the message, integrating them seamlessly into the content. ```js new FileBuilder() .setURL('attachment://document.pdf') .setSpoiler(false); ``` -------------------------------- ### Manage Component State in JavaScript Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Provides functions `saveComponentState` and `loadComponentState` to store and retrieve UI component states using a `Map`. This allows preserving the state of interactive components across user interactions based on message IDs. ```js const componentStates = new Map(); function saveComponentState(messageId, state) { componentStates.set(messageId, state); } function loadComponentState(messageId) { return componentStates.get(messageId) || {}; } ``` -------------------------------- ### Implement Abstraction Layer for Discord Message Building in JavaScript Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Demonstrates a `MessageBuilder` class that uses an abstraction layer to switch between Components v2 and traditional message building. This design pattern allows for easier adaptation to future API changes or feature rollouts. ```js class MessageBuilder { constructor() { this.useComponentsV2 = true; } createMessage(data) { if (this.useComponentsV2) { return this.buildCV2Message(data); } else { return this.buildTraditionalMessage(data); } } buildCV2Message(data) { // Components v2 implementation } buildTraditionalMessage(data) { // Fallback implementation } } ``` -------------------------------- ### Handle Button Interactions for Discord Components v2 Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Demonstrates how to process interactions from Discord Components v2 buttons using the standard `client.on('interactionCreate')` event listener. It shows how to check for button interactions and use `interaction.customId` to differentiate between various button actions, such as replying or showing a modal. ```js client.on('interactionCreate', async interaction => { if (!interaction.isButton()) return; switch(interaction.customId) { case 'refresh_stats': await interaction.reply({ content: 'Stats refreshed!', ephemeral: true }); break; case 'purchase': await interaction.showModal(purchaseModal); break; } }); ``` -------------------------------- ### Add Interactive Components with Discord.js ActionRowBuilder Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md The `ActionRowBuilder` is essential for adding interactive elements like buttons and select menus to Components v2 messages. It allows developers to create dynamic and engaging user interfaces directly within Discord. ```js new ActionRowBuilder() .addComponents( new ButtonBuilder() .setCustomId('primary_action') .setLabel('Confirm') .setStyle(ButtonStyle.Success), new SelectMenuBuilder() .setCustomId('options_select') .setPlaceholder('Choose an option') .addOptions([ { label: 'Option 1', value: 'opt1' }, { label: 'Option 2', value: 'opt2' } ]) ); ``` -------------------------------- ### JavaScript Configuration for Discord Components Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md This JavaScript snippet defines a `componentConfig` object for Discord UI elements, including predefined color palettes and spacing options. It also provides a `buildConfiguredContainer` function that utilizes this configuration to instantiate a `ContainerBuilder`, dynamically setting its accent color based on a provided theme or defaulting to the primary color. ```js const componentConfig = { colors: { primary: 0x5865F2, success: 0x57F287, warning: 0xFEE75C, danger: 0xED4245 }, spacing: { small: SeparatorSpacingSize.Small, large: SeparatorSpacingSize.Large } }; function buildConfiguredContainer(config) { return new ContainerBuilder() .setAccentColor(componentConfig.colors[config.theme || 'primary']); } ``` -------------------------------- ### Validate Discord Components v2 Container in JavaScript Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Includes functions `validateContainer` and `countComponents` to enforce the Discord component limit (40 components). `validateContainer` throws an error if the limit is exceeded, aiding in debugging and adherence to Discord's API constraints. ```js function validateContainer(container) { const componentCount = countComponents(container); if (componentCount > 40) { throw new Error(`Component limit exceeded: ${componentCount}/40`); } return container; } function countComponents(container) { let count = 1; // Container itself container.components?.forEach(component => { count++; if (component.components) { count += component.components.length; } }); return count; } ``` -------------------------------- ### Create Rich Text Content with Discord.js TextDisplayBuilder Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md The `TextDisplayBuilder` replaces traditional embed titles and descriptions, supporting full Markdown formatting and Discord mentions for rich content display within Components v2 messages. It offers flexibility without per-field character limits. ```js new TextDisplayBuilder() .setContent("# Main Heading\n**Bold text** and _italics_\nMentions work: <@123456789>\nChannels too: <#987654321>"); ``` -------------------------------- ### Create Paginated Content Container in JavaScript Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Implements a `PaginatedContainer` class to manage and display paginated data. It constructs a Discord Components v2 container with navigation buttons for 'Previous' and 'Next' pages, dynamically updating the displayed content based on the current page and page size. ```js class PaginatedContainer { constructor(data, pageSize = 5) { this.data = data; this.pageSize = pageSize; this.currentPage = 0; } buildPage() { const start = this.currentPage * this.pageSize; const pageData = this.data.slice(start, start + this.pageSize); const container = new ContainerBuilder() .addTextDisplayComponents( new TextDisplayBuilder() .setContent(`# Results (${start + 1}-${start + pageData.length} of ${this.data.length})`) ); pageData.forEach(item => { container.addSectionComponents( new SectionBuilder() .addTextDisplayComponents( new TextDisplayBuilder().setContent(`**${item.title}**\n${item.description}`) ) ); }); const buttons = []; if (this.currentPage > 0) { buttons.push( new ButtonBuilder() .setCustomId('prev_page') .setLabel('Previous') .setStyle(ButtonStyle.Secondary) ); } if ((this.currentPage + 1) * this.pageSize < this.data.length) { buttons.push( new ButtonBuilder() .setCustomId('next_page') .setLabel('Next') .setStyle(ButtonStyle.Secondary) ); } if (buttons.length > 0) { container.addActionRowComponents( new ActionRowBuilder().addComponents(...buttons) ); } return container; } } ``` -------------------------------- ### Update Discord Message Container Dynamically in JavaScript Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Defines an asynchronous function `updateContainer` that rebuilds a Discord Components v2 container with new data and updates an existing interaction message. It ensures the `MessageFlags.IsComponentsV2` flag is set for proper rendering. ```js async function updateContainer(interaction, newData) { const updatedContainer = buildContainerFromData(newData); await interaction.update({ components: [updatedContainer], flags: [MessageFlags.IsComponentsV2] }); } ``` -------------------------------- ### Add Visual Spacing and Dividers with Discord.js SeparatorBuilder Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md The `SeparatorBuilder` controls spacing and adds visual breaks within Components v2 messages. It can create subtle gaps or strong horizontal lines for better content separation and improved readability. ```js new SeparatorBuilder() .setSpacing(SeparatorSpacingSize.Large) .setDivider(true); ``` -------------------------------- ### Group Related Information with Sectioned Content in Discord Components v2 Source: https://github.com/ankush26030/discord-components-v2-ui/blob/main/README.md Shows how to create sectioned content using `addSectionComponents` and `SectionBuilder`. This pattern allows grouping related text displays and attaching accessories like thumbnails or buttons to individual content blocks, separated by components like `addSeparatorComponents`. ```js container .addSectionComponents( new SectionBuilder() .addTextDisplayComponents(title, description) .setThumbnailAccessory(preview) ) .addSeparatorComponents(divider) .addSectionComponents( new SectionBuilder() .addTextDisplayComponents(details) .setButtonAccessory(actionButton) ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.