Create button dynamically?

I have some data I need to fetch from my database, and then iterate over that data and want to create buttons for each row of data. Is this possible? The documentation is a little hazy and I haven't seen any examples of a similar case.
4 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
duck
duck2y ago
you can pass whatever variables to whatever ButtonBuilder methods you want
Helix
Helix2y ago
I guess what I'm saying, is would something like this actually work?
const rosters = await Roster.query().select(["id", "name", "url"]);



const row = new ActionRowBuilder().addComponents(
...rosters.map((roster) =>
new ButtonBuilder()
.setCustomId(roster.id)
.setLabel(roster.name)
.setStyle(ButtonStyle.Success)
)
);
const rosters = await Roster.query().select(["id", "name", "url"]);



const row = new ActionRowBuilder().addComponents(
...rosters.map((roster) =>
new ButtonBuilder()
.setCustomId(roster.id)
.setLabel(roster.name)
.setStyle(ButtonStyle.Success)
)
);
I do I need to pass it as an array of plain objects to the addComponents function?
duck
duck2y ago
what you have is fine, assuming you don't add more than 5 buttons to the single row