简体   繁体   English

一个 ActionRow 中的多个按钮

[英]multiple buttons in one ActionRow

I'm using the discord.js library and node.js to create a Discord bot that can send a DM to a user containing buttons.我正在使用 discord.js 库和 node.js 创建一个 Discord 机器人,它可以向包含按钮的用户发送 DM。

I want to send a Discord message with multiple buttons, each with a unique customId and label.我想发送带有多个按钮的 Discord 消息,每个按钮都有一个唯一的 customId 和标签。 My current method is using an list of buttons.我目前的方法是使用按钮列表。 I use a for loop adding button objects to the list, and pass it in to the components of the .send method as a list.我使用 for 循环将按钮对象添加到列表中,并将其作为列表传递给 .send 方法的组件。

x = 5
buttons = []

for (let i = 0; i < x; i++) {
    buttons.push(new MessageActionRow().addComponents(
        new MessageButton()
            .setCustomId(i.toString())
            .setLabel(messageSplit[i])
            .setStyle('PRIMARY')
        )
    )
}

msg.reply({ embeds: [embedRecipient], components: buttons })

This works, but each button is an new ActionRow of its own and thus resulting in the buttons being on different lines.这可行,但每个按钮都是它自己的新 ActionRow,因此导致按钮位于不同的行上。 What I mean: image of buttons each on a different line我的意思是:每个按钮的图像都在不同的行上

How can I make it so the same features (like customId and Label) of the buttons retain but they are all on the same line?我怎样才能使它保留按钮的相同功能(如customId和Label)但它们都在同一行? Making them all in the same one ActionRow should solve this I don't know the code to achieve that.将它们全部放在同一个 ActionRow 中应该可以解决这个问题,我不知道实现这一点的代码。

To put all the buttons in one single MessageActionRow , you could just declare the row outside the for loop and then inside it, you can just use .addComponents() to add a button to the MessageActionRow .要将所有按钮放在一个MessageActionRow中,您可以在 for 循环之外声明该行,然后在其中声明,您可以使用.addComponents()MessageActionRow添加一个按钮。 An example is this:一个例子是这样的:

x = 5
const buttons = new MessageActionRow()

for (let i = 0; i < x; i++) {
    buttons.addComponents(
        new MessageButton()
            .setCustomId(i.toString())
            .setLabel(messageSplit[i])
            .setStyle('PRIMARY')
        )
    )
}

msg.reply({ embeds: [embedRecipient], components: [buttons] })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM