【问题标题】:discord.js find claim time in giveaways using rolesdiscord.js 使用角色在赠品中查找声明时间
【发布时间】:2021-08-07 08:53:11
【问题描述】:

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

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 秒(支持者角色)。有什么解决办法吗?

【问题讨论】:

  • 您是否尝试根据用户的角色设置赠品持续时间?就像如果用户既是“支持者”又是“助推器”,您希望持续时间为 18 sec
  • 是的,机器人计算所有角色,所以“支持”和“助推器”=18 s 是的,它应该加起来时间

标签: node.js discord.js roles


【解决方案1】:

问题出在这一行

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);
      });
  },
};

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 2021-04-18
    • 1970-01-01
    • 2020-12-30
    • 2018-02-10
    • 2021-03-10
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多