javascript 如何跟踪选择菜单discord.js?

2skhul33  于 2023-01-01  发布在  Java
关注(0)|答案(2)|浏览(171)

我正在尝试创建一个代码,它将创建所需数量的菜单选择,然后在单击后响应它们。

for(i = 0; i < arr.length; i++) {
 select.addOptions([
          {
            label: `${i}`,
            value: `value${i}`,
            description: `${i} role` }
          ])
}

这里arr.length-需要的选择数。我只是不知道如何在这个选择菜单上得到这个值。我很高兴甚至有一个提示!谢谢

cczfrluj

cczfrluj1#

select.addOptions()接受一个数组选项,因此可以这样做:

let selectarr = [];
for(i = 0; i < arr.length; i++) {
    selectarr.push({
        label: `{i}`,
        description: `value${i}`,
        value: `${i} role`,
    });
}

select.addOptions(selectarr)

评论:

//Create select menu const select = new ActionRowBuilder().addComponents( new StringSelectMenuBuilder() .setCustomId("your id") 
.setPlaceholder("Your palceholder") 
.addOptions(selectarr) ); 
//Reply 
const sendResult = interaction.channel.send({ content: "Your text", embeds: [if you have an embed], components: [select], });
r8uurelv

r8uurelv2#

我找到了解决方案,但我认为有一个更合理的解决方案来解决我的问题,我给予你我的版本

const select = new StringSelectMenuBuilder()
    .setCustomId(`newTest`)
    .setMaxValues(1)
    .setPlaceholder(`Placeholder`)
const clearArr = []
for(i = 0; i < arr.length; i++) {
        clearArr.push(i)
        select.addOptions([
          {
            label: `${i}`,
            value: `value${i}`,
            description: `${i} role`,
          }
          ])
    }

    const row = new ActionRowBuilder()
    .addComponents(buttons)

    let sendmsg = await message.channel.send(); // your message
    const collector = sendmsg.createMessageComponentCollector({
      componentType: ComponentType.StringSelect,
    });
    collector.on("collect", async (collected) => {
    const value = collected.values[0];
    for(const check of mas) {
      if(value == `value${check}`) {
        message.channel.send(`hi`)
      }
    }

    });

感谢每一个想帮忙的人:)

相关问题