Discord.JS, 如何使用一个discord按钮允许购买各种服务器角色

Discord.JS, How to use one discord button to allow the purchase of various server roles

抱歉,标题措辞不佳,我会尽力解释。我正在使用新的 discord-buttons 模块创建一个角色商店命令,并遇到了一个问题,据我所知,我必须为每个角色创建一个按钮,以便有人购买它。搜索文档后,我仍然有点难过。这是我放在一起的一些示例代码,用于展示我正在尝试做的事情:

let embedRed = new Discord.MessageEmbed()
                
        .setTitle('Red Role')
        .setColor('#c46413')
        .addField('**Price**', '10,000', true)
        .addField('**Color Hex:**', '#ffffff',true)

 let embedBlue = new Discord.MessageEmbed() 
                  
          .setTitle('Blue')
          .setColor('#c2a289')
          .addField('**Price**', '10,000', true)
          .addField('**Color Hex:**', '#ffffff',true)

  ///Buttons
let buttonBuyRed = new MessageButton()
.setStyle('green')
.setLabel('Buy Red Role')
.setID('role_buy1')


let buttonBuyBlue = new MessageButton()
.setStyle('green')
.setLabel('Buy Blue Role')
.setID('role_buy2')

//embeded messages being sent
 message.channel.send({ buttons: [buttonBuyRed], embed: embedRed});
    message.channel.send({ buttons: [buttonBuyRed], embed: embedBlue});


//What happens if buttons are pressed
client.on('clickButton', async (role_buy1) => {
  if (button.id === 'roley_buy1') {
    button.channel.send(`${button.clicker.user.tag} bought red role`);
db.push(message.author.id, `${message.guild.roles.cache.get('role id here')}`) //role being pushed to user's inventory
  }
});

client.on('clickButton', async (role_buy2) => {
  if (button.id === 'role_buy2') {
    button.channel.send(`${button.clicker.user.tag} bought blue role`);
db.push(message.author.id, `${message.guild.roles.cache.get('role id here')}`) //role being pushed to user's inventory
  }
});

由于我希望用户能够购买大约 25 个不同的角色,因此为每个角色创建一个按钮非常麻烦,我正在寻找一种只使用一个按钮的方法“buy_role" 适用于所有可用角色的按钮。

如果我没有解释清楚,请告诉我,我们将不胜感激!

你有${message.guild...},有错就是错了,试试这个:

  if (button.id === 'roley_buy1') {
    button.channel.send(`${button.clicker.user.tag} bought red role`);
    db.push(message.author.id, `${button.guild.roles.cache.get('role id here')}`) //role 
    being pushed to user's inventory
    button.clicker.roles.add('your role id');
    // or you can find the role using
    const role = button.guild.roles.cache.find(role => role.name == 'rolename');
    button.clicker.roles.add(role);
  }
});```

所以我得出一个结论,这段代码有效,但是如果你的公会有很多角色,它会抛出一个错误“无效的表单主体”

        const rolesInGuild = message.guild.roles.cache.array(); //creating array from collection of roles in a guild
        const buttons = []; // an empty array for our buttons
        for (const role of rolesInGuild) { // creating a loop inorder to create a button for every roles in rolesInGuild Array
            const button = new MessageButton()
                .setStyle('red') // default: blurple
                .setLabel(`${role.name}`) // default: NO_LABEL_PROVIDED
                .setID(`${role.id}`);
            buttons.push(button); // button id is the same as role id so its unique!
        }
        console.log(rolesInGuild);
        console.log(buttons);
        await message.channel.send('test', { buttons: buttons }); // sending our buttons

        bot.on('clickButton', async(button) => {
            for (const btn of buttons) {
                if (btn.custom_id == button.id) {
                    const role = button.guild.roles.cache.get(btn.custom_id);
                    const member = message.guild.members.cache.get(button.clicker.user.id);
                    member.roles.add(role);
                }
            }
        });

您可以将特定角色添加到此格式的数组 rolesInGuild [{ name: 'rolename', id: 'roleid' }] 而不是公会中的每个角色(我不确定你的目标是什么)