Loại component builder
Khởi tạo ComponentBuilder với cấu hình component ban đầu (tuỳ chọn).
Loại component builder
Optionalcomponents: ComponentInput[]Danh sách cấu hình component (ComponentInput[])
Constructor này cho phép bạn khởi tạo builder kèm dữ liệu ngay lập tức mà không cần gọi thủ công các method như:
Nếu components được cung cấp:
parse() để:
ActionRowBuilderthis.rowscomponents có tồn tạiparse(components)parse chịu trách nhiệm xử lý logic buildconst builder = new ComponentBuilder([
{
type: "button",
options: [
{ customId: "btn_1", label: "Click", style: 1 }
]
}
]);
Thêm một hoặc nhiều Button vào builder.
Một hoặc danh sách cấu hình button
Instance hiện tại (chainable)
Đây là method shortcut giúp bạn thêm button nhanh chóng
mà không cần viết đầy đủ cấu trúc { type: "button" }.
Method này sẽ tự động:
{ type: "button", options: [...] }ActionRowButtonInput[]){ type: "button", options: data }
parse() để build componentActionRow theo logic của parseparse)Thêm một hoặc nhiều ActionRow mới từ cấu hình component.
Một hoặc danh sách cấu hình component (ComponentInput)
Instance hiện tại (chainable)
Đây là method “low-level” cho phép bạn thêm trực tiếp các cấu hình
component dưới dạng ComponentInput mà không cần dùng các shortcut như:
ComponentInput[])parse() để:
ActionRowBuilderthis.rowscomponents có thể tạo ra một hoặc nhiều ActionRow
(tùy vào logic bên trong parse)parse)addButton, addSelectMenuThêm một hoặc nhiều Select Menu vào builder.
Một hoặc danh sách cấu hình Select Menu
Instance hiện tại (chainable)
Method này hỗ trợ tất cả các loại Select Menu của Discord:
stringmenu)usermenu)rolemenu)channelmenu)mentionmenu)SelectMenu[])parse() để chuyển config → ActionRowBuilderActionRow được xử lý bên trong parseoptions (phụ thuộc parse)builder.addSelectMenu({
type: "stringmenu",
options: {
customId: "menu",
options: [
{ label: "A", value: "a" }
]
}
});
Build toàn bộ ActionRow để sử dụng trong Discord API.
Mảng ActionRowBuilder đã được cấu hình
Đây là bước cuối cùng trong quá trình sử dụng builder.
Method này sẽ trả về toàn bộ các ActionRow đã được thêm trước đó
để có thể truyền trực tiếp vào các method của Discord như:
message.replyinteraction.replymessage.editthis.rowsbuild()buildTạo Interaction Collector từ source (message hoặc interaction).
Loại component (button, select menu, ...)
Xác định interaction có nằm trong guild hay không
Cấu hình collector và nguồn khởi tạo
onCollectorCallback sẽ nhận object:
interaction: Interaction gốccustomId: ID của componentuserId: ID của uservalues: Giá trị (chỉ có với select menu)message: Message chứa componentcollector: Instance collectorstop(): Dừng collectoronEndCallback sẽ nhận object:
message: Message chứa collectorreason: Lý do kết thúctotal: Tổng số interaction đã collectedit(): Helper để edit messageconst collector = await builder.createCollector({
source: message,
time: 60000,
onCollector: async (ctx) => {
console.log(ctx.customId);
}
});
// Sử dụng với interaction
const collector = await builder.createCollector({
source: interaction,
time: 30000,
onCollector: async ({ interaction, stop }) => {
await interaction.reply("Clicked!");
stop();
},
onEnd: ({ total, reason }) => {
console.log(`Collected: ${total}, reason: ${reason}`);
}
});
Disable toàn bộ component trong tất cả ActionRow.
Instance hiện tại (chainable)
Method này sẽ duyệt qua toàn bộ các ActionRow và:
setDisableddisabled = trueActionRow trong this.rowssetDisabledsetDisabled(true) nếu tồn tạiThay thế một hoặc nhiều ActionRow tại vị trí index bằng config mới.
Vị trí bắt đầu thay thế trong danh sách ActionRow
Danh sách cấu hình component để tạo row mới
Instance hiện tại (chainable)
Method này cho phép bạn thay thế động một hoặc nhiều ActionRow
mà không cần rebuild toàn bộ builder.
rows hiện tạithis.rows để reuse logic addRowaddRow(components) để build các row mớinewRows)rows ban đầusplice để thay thế tại vị trí indexnewRows.length)rows sẽ bị thay đổi kích thướcthis.rowscomponents sẽ được build lại thông qua addRowbuilder.replace(0, [
{
type: "button",
options: [
{
customId: "test",
label: "Test",
style: "Danger"
}
]
}
]);
// Replace nhiều row
builder.replace(0, [
{ type: "button", options: [...] },
{ type: "stringmenu", options: {...} }
]);
Thay thế một ActionRow tại vị trí index bằng một config duy nhất.
Vị trí cần thay thế trong danh sách ActionRow
Cấu hình component để tạo row mới
Instance hiện tại (chainable)
Đây là method tiện ích (helper) của replace, dùng khi bạn chỉ cần thay thế một row duy nhất thay vì nhiều row.
Internally:
replacecomponent → ComponentInput[]component duy nhất[component]index được xử lý bên trong replacebuilder.replaceOne(0, {
type: "button",
options: [
{
customId: "confirm",
label: "Confirm",
style: "Success"
}
]
});
Thay thế một đoạn ActionRow liên tiếp bằng các component mới.
Vị trí bắt đầu thay thế
Số lượng ActionRow cần xóa
Danh sách cấu hình component để tạo row mới
Instance hiện tại (chainable)
Method này hoạt động tương tự Array.prototype.splice,
cho phép bạn:
start và deleteCountnewRows thông qua addRowsplice(start, deleteCount, ...newRows) để thay thếrowsthis.rowscomponents sẽ được build lại thông qua addRow// Replace 2 rows từ index 0
builder.replaceRange(0, 2, [
{ type: "button", options: [...] },
{ type: "stringmenu", options: {...} }
]);
Chuyển toàn bộ ActionRowBuilder sang định dạng JSON theo Discord API.
Mảng APIActionRowComponent ở dạng raw JSON
Method này sẽ gọi .toJSON() trên từng ActionRowBuilder
để chuyển đổi toàn bộ component sang format mà Discord API yêu cầu.
this.rowsrow.toJSON() trên từng rowbuild() → trả về builder object (dùng trực tiếp với discord.js)toJSON() → trả về raw JSON (Discord API format)
Builder hỗ trợ tạo và quản lý Discord Message Components (Buttons, Select Menus...) thông qua
ActionRowBuilder.Class này giúp bạn:
Remarks
Discord giới hạn:
Class này tự động chia button thành nhiều row khi > 5.
Example
Tạo button đơn giản
Example
Tạo Select Menu
Example
Tạo Interaction Collector
Example
Disable toàn bộ components