首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Discord.JS,如何使用一个不一致的按钮来允许购买各种服务器角色

Discord.JS,如何使用一个不一致的按钮来允许购买各种服务器角色
EN

Stack Overflow用户
提问于 2021-06-01 03:14:17
回答 2查看 993关注 0票数 0

很抱歉标题写得很糟糕,我会尽我所能解释的。我正在使用新的discord button模块创建一个角色商店命令,遇到了一个问题,据我所知,我必须为每个单独的角色创建一个按钮,以便有人购买它。在搜索了文档之后,我仍然有点困惑。下面是我放在一起的一些示例代码,用来展示我想要做的事情:

代码语言:javascript
代码运行次数:0
运行
复制
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“按钮的方法,该按钮适用于所有可用角色。

如果我没有解释清楚,请让我知道,任何帮助都很感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-01 04:47:42

所以我得出了一个结论,这个代码可以工作,但是如果你的行会有很多角色,它会抛出一个错误"Invalid form body“

代码语言:javascript
代码运行次数:0
运行
复制
        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:'rolename',id:'roleid‘}格式的数组角色中,而不是添加到公会中的每个角色(我不确定您的目标是什么)

票数 1
EN

Stack Overflow用户

发布于 2021-06-01 03:55:36

你有${message.guild...},如果你有一个错误,这是错误的,所以试试这个:

代码语言:javascript
代码运行次数:0
运行
复制
  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);
  }
});```
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67779187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档