Home > Software design >  Can send several buttons that we don't know how many we have? (Discord.js v13)
Can send several buttons that we don't know how many we have? (Discord.js v13)

Time:06-08

I have an array that I want to send buttons from it I mean I don't know how many i have and I've tested this code but it sends 2 different messages with buttons :

array.flatMap(user => {
                const cs = new client.discord.MessageActionRow()
                    .addComponents(
                        new client.discord.MessageButton()
                        .setLabel(user.type)
                        .setURL(${user.id})
                        .setEmoji('979681456781635700')
                        .setStyle('LINK')
                    );
                const ch = client.channels.cache.get('998376140326256158');
               ch.send({
                    embeds: [embed],
                    components: [cs, ]
                })
            })

And this is my array :

    [
  {
    id: '08933438391',
    type: 'ejx1'
  },
  {
    id: '12361425430',
    type: 'ejx3'
  }
]

I don't know, it can be 4 id,type or 3 or whatever I don't know (not gonna be more than 10)

CodePudding user response:

The addComponents method can be used with an array. Just save your buttons inside it and then, use the method. Just keep in mind that you can only set five buttons per row.

const row = new client.discord.MessageActionRow();
let buttons = [];
for(let i = 0; i < array.length; i  ){
  buttons.push(
    new client.discord.MessageButton()
    .setLabel(array[i].type)
    .setURL(${user.id})
    .setEmoji(array[i].id)
    .setStyle('LINK')
  );
}
row.addComponents(buttons);
//Do your stuff with the row variable.
  • Related