Building Discord Components V2 Messages Cleanly in discord.js
Discord's Components V2 update finally lets bots put text, images, thumbnails, dividers, buttons, and...
Discord's Components V2 update finally lets bots put text, images, thumbnails, dividers, buttons, and dropdowns together inside a single clean card. It looks much better than old embeds, especially for dashboards, shop menus, and help panels.
The catch is how much code it takes to write by hand in discord.js.
Just to send a basic welcome card with two lines of text, a user avatar on the side, a divider line, and two buttons, you end up stacking six or seven different classes inside each other:
import {
ContainerBuilder,
SectionBuilder,
TextDisplayBuilder,
ThumbnailBuilder,
SeparatorBuilder,
SeparatorSpacingSize,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
MessageFlags,
} from "discord.js";
const container = new ContainerBuilder()
.setAccentColor(0x5865f2)
.addSectionComponents(
new SectionBuilder()
.addTextDisplayComponents(
new TextDisplayBuilder().setContent("## Welcome to the server"),
new TextDisplayBuilder().setContent("Glad to have you here.")
)
.setThumbnailAccessory(
new ThumbnailBuilder().setURL(interaction.user.displayAvatarURL())
)
)
.addSeparatorComponents(
new SeparatorBuilder().setDivider(true).setSpacing(SeparatorSpacingSize.Small)
)
.addActionRowComponents(
new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("rules")
.setLabel("Read Rules")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setURL("https://discord.com")
.setLabel("Website")
.setStyle(ButtonStyle.Link)
)
);
await interaction.reply({
flags: MessageFlags.IsComponentsV2 | MessageFlags.Ephemeral,
components: [container],
});
Once your bot grows past a few commands, maintaining layouts like that gets tiring fast.
To make my own bot code easier to read and maintain, I built discord-container-kit and just released v1.1.0.
Installation
npm install discord-container-kit
Writing the same card in a few lines
Here is that exact same welcome card using discord-container-kit:
import { Container, Button } from "discord-container-kit";
const card = new Container()
.accent("blurple")
.section(
["## Welcome to the server", "Glad to have you here."],
interaction.user.displayAvatarURL()
)
.divider()
.buttons(
Button.primary("rules", "Read Rules", "📜"),
Button.link("https://discord.com", "Website")
);
await interaction.reply(card.toReply({ ephemeral: true }));
You do not have to manually wrap buttons or dropdowns inside rows, calculate message flags, or look up hex codes for standard colors like "blurple", "emerald", "gold", or "red". Custom and animated Discord emojis work directly as strings too (like "<a:party:123456789012345678>").
Full panels with dropdowns, images, and files
Every Components V2 item is built right onto Container, including all five select menu types (string, user, role, channel, and mentionable), image galleries, and file attachments:
import { Container, Button, ChannelKind } from "discord-container-kit";
const panel = new Container()
.setColor("emerald")
.heading("Server Setup", 1)
.section(
["### Logging", "Pick where moderation logs should go."],
interaction.guild.iconURL()
)
.divider()
.channelSelect("log_channel", {
placeholder: "Choose a text channel...",
channelTypes: [ChannelKind.GuildText],
})
.stringSelect(
"log_events",
[
{ label: "Moderation", value: "mod", emoji: "🛡️" },
{ label: "Member Joins", value: "joins", emoji: "👋" },
],
{ placeholder: "Select events to track..." }
)
.buttons(
Button.success("save_setup", "Save"),
Button.secondary("cancel_setup", "Cancel")
)
.footer("You can change these settings anytime.");
await interaction.reply(panel.toReply({ ephemeral: true }));
Quick status cards
For everyday confirmation or error replies, there are ready-made presets so you do not have to build a new card from scratch every time:
await interaction.reply(
Container.success("Saved", "Your settings have been updated.").toReply({ ephemeral: true })
);
await interaction.reply(
Container.error("Missing Access", "You need administrator permissions for this.").toReply({
ephemeral: true,
})
);
Moving old Embeds to Components V2
If you already have a big bot full of EmbedBuilder messages and do not want to rewrite dozens of files by hand, Container.fromEmbed() converts an existing embed into a V2 container in one step:
const card = Container.fromEmbed(existingEmbed).buttons(
Button.primary("refresh", "Refresh")
);
await channel.send(card.toMessage());
Multi-page menus
For leaderboards, help commands, or shop pages, paginate() sets up Previous, Next, and Jump buttons automatically and restricts button clicks to the user who ran the command:
import { Container, paginate } from "discord-container-kit";
const pages = [
new Container().accent("gold").heading("Leaderboard - Page 1").lines("1. Demon", "2. Alex"),
new Container().accent("silver").heading("Leaderboard - Page 2").lines("3. Sam", "4. Kai"),
];
await paginate(interaction, pages, {
showJumpButtons: true,
time: 120_000,
});
New Discord Modals (with file uploads and checkboxes)
v1.1.0 also includes builders for Discord's newer pop-up modal inputs, including file uploads, radio choices, and checkboxes:
import { Modal } from "discord-container-kit";
const modal = new Modal("bug_report", "Report an Issue")
.shortInput("title", "Summary", { placeholder: "What went wrong?" })
.paragraphInput("details", "Steps to reproduce")
.fileUpload("screenshots", "Screenshots", { maxValues: 3, required: false })
.radioGroup("urgency", "How severe is it?", [
{ label: "Minor visual issue", value: "low", default: true },
{ label: "Command is broken", value: "high" },
])
.checkbox("dm_opt_in", "Allow staff to message me with updates", { default: true });
await interaction.showModal(modal.toJSON());
Links
If you are building with Components V2 and want cleaner command files, give it a try and let me know what you think or what helpers you would like to see next.
- NPM: https://www.npmjs.com/package/discord-container-kit
- GitHub & Docs: https://github.com/demondevx/discord-container-kit
Sumber artikel open-source oleh DEMON di DEV Community — Baca artikel asli. Topik:
#discord#javascript#typescript#opensource

Komentar