Discord Bot-限制为一种反应

时间:2020-04-26 16:27:54

标签: node.js discord discord.js

我目前正在研究Discord机器人,并且遇到了需要限制反应的问题。我正在尝试实现以下目标-如果用户选择反应,则应将其替换为最新反应。有人可以建议如何做吗?

这是我目前的screenshot和代码。

我的代码:

client.on('raw', event => {
  console.log(event);
  const eventName = event.t;
  if(eventName === 'MESSAGE_REACTION_ADD') {
    if(event.d.message_id === '70цw3748527414706191') {
      var reactionChannel = client.channels.get(event.d.channel_id);
      if(reactionChannel.messages.has(event.d.message_id))
      return;
      else {
        reactionChannel.fetchMessage(event.d.message_id)
        .then(msg => {
          var msgReaction = msg.reactions.get(event.d.emoji.name + ":" + event.d.emoji.id);
          var user = client.users.get(event.d.user_id);
          client.emit('messageReactionAdd', msgReation, user);
        })
        .catch(err => console.log(err));
      }
    }
  }
  else if(eventName === 'MESSAGE_REACTION_ADD') {
    if(event.d.message_id === '703748527414706191') {
      var reactionChannel = client.channels.get(event.d.channel_id);
      if(reactionChannel.messages.has(event.d.message_id))
      return;
      else {
        reactionChannel.fetchMessage(event.d.message_id)
        .then(msg => {
          var msgReaction = msg.reactions.get(event.d.emoji.name + ":" + event.d.emoji.id);
          var user = client.users.get(event.d.user_id);
          client.emit('messageReactionRemove', msgReation, user);
        })
        .catch(err => console.log(err));
      }
    }
  }
});

client.on('messageReactionAdd', (messageReaction, user) => {

  var roleName = messageReaction.emoji.name;
  console.log(roleName);
  var role = messageReaction.message.guild.roles.find(role => role.name.toLowerCase() ===
  roleName.toLowerCase());

  if(role) {
    var member = messageReaction.message.guild.members.find(member => member.id === user.id);
    if(member) {
      member.addRole(role.id);
      console.log("Success");
    }
  }
});

client.on('messageReactionRemove', (messageReaction, user) => {
  var roleName = messageReaction.emoji.name;
  var role = messageReaction.message.guild.roles.find(role => role.name.toLowerCase() ===
  roleName.toLowerCase());

  if(role) {
    var member = messageReaction.message.guild.members.find(member => member.id === user.id);
    if(member) {
      member.removeRole(role.id);
      console.log("Success");
    }
  }
});

谢谢您的指导!

1 个答案:

答案 0 :(得分:0)

如果他们已经具有另一个颜色角色,则可以检查messageReactionAdd,与此同时,您将需要每个颜色角色都具有一个前缀或具有一系列存在的颜色角色。

这是一个开始

client.on('messageReactionAdd', (reaction, user) => {
    const guild = reaction.message.guild;
    const roleName = reaction.emoji.name.toLowerCase();
    console.log(roleName);
    const role = guild.roles.cache.find(r => r.name.toLowerCase() === roleName);

    if (role) {
        const member = guild.members.cache.find(m => m.id === user.id);

        //check if they already have a color role
        if (alreadyColorRole) {
            //either make it so he cant react until he unreacts to the role
            //or automatically remove the other reaction.
            reaction.remove();
        } else {
            member.addRole(role.id);
            console.log("Success");
        }
    }
});