discord.js 使用角色查找赠品中的领取时间

discord.js find claim time in giveaways using roles

我最近将此添加到我的机器人中,它会检查角色并在发送用户声明时间的频道中发送消息。

module.exports = {
  name: 'giveaway',
  description: ':tada: the new winner is',
  execute(message, args){

    let winner = message.mentions.members.first();
    
    const ouser = message.mentions.users.first();

    var time = 10000;
    var support = 0;
    var donate = 0;
    var boost = 0;
   
    const allowedRole = winner.roles.cache.find(r => r.name === '・Supporter') || winner.roles.cache.find(r => r.name === 'Nitro・Donator') || winner.roles.cache.find(r => r.name === '・Booster') 
    
    if (!allowedRole) {
      message.channel.send(`Congratulations **${ouser.username}**! You have ${time / 1000} seconds to DM the host!`)
          .then(message => {
      setTimeout(function() {
        message.channel.send(`${time / 1000} seconds up!`)
      }, time)
    })
    return;
    }

    switch (allowedRole.name) {
      
      case '・Supporter':
      support = 3000;

      break;

      case 'Nitro・Donator':
      donate = 5000;

      break;

      case '・Booster':
      boost = 5000;
    }

    var newTime = (time + support + donate + boost));
    
    const user = message.mentions.users.first();
    
    message.channel.send(`Congratulations **${user.username}**! You have ${newTime / 1000} seconds to DM the host!`)
    .then(message => {
      setTimeout(function() {
        message.channel.send(`${newTime / 1000} seconds up!`)
      }, newTime)
    })

  }
}

由于 + 不工作,我不确定如何为总索赔时间添加时间。我尝试使用 time - (- support) - (-donate) - (-boost)) 但它只显示 13 秒(支持角色)。有什么解决办法吗?

问题出在这一行

const allowedRole = winner.roles.cache.find(r => r.name === '・Supporter') || winner.roles.cache.find(r => r.name === 'Nitro・Donator') || winner.roles.cache.find(r => r.name === '・Booster')

allowedRole只能设置一个角色,但一个用户可以有多个角色。

你可以这样做

module.exports = {
  name: "giveaway",
  description: ":tada: the new winner is",
  execute(message, args) {
    const winner = message.mentions.members.first();

    let DefaultTime = 10;
    let support = 0;
    let donate = 0;
    let boost = 0;

    //get all the roles of the winner in an array
    const userRoles = winner.roles.cache.map((r) => r.name);

    //Check if the user have the allowed roles and set time according to that (There might be a better way to do this instead of using if statements for each of them seperately)
    if (userRoles.includes("・Supporter")) {
      support = 3;
    }

    if (userRoles.includes("Nitro・Donator")) {
      donate = 5;
    }
    
    if (userRoles.includes("・Booster")) {
      boost = 5;
    }

    const TotalTime = DefaultTime + support + donate + boost;

    message.channel
      .send(
        `Congratulations **${winner.user.username}**! You have ${TotalTime} seconds to DM the host!`
      )
      .then((message) => {
        setTimeout(function () {
          message.channel.send(`${TotalTime} seconds up!`);
        }, TotalTime * 1000);
      });
  },
};