如何使用“角色反应”进行角色检查并自动删除用户反应

时间:2020-06-21 16:02:19

标签: javascript node.js discord discord.js

嘿,到目前为止,我已经获得了角色反应,但是我想这样做,以便在按下表情符号后,代码会询问是否有人已经有了该群组,因此会添加或删除该群组,但是我也想以便自动删除与表情符号的反应,从而只有漫游器对显示表情符号的消息做出反应。

我将在附件中添加我到目前为止编写的代码,也许有些人可以帮助我。

MessageReactionAddEvent:

const BaseEvent = require('../utils/structures/BaseEvent');
require('dotenv').config();
module.exports = class MessageReactionAddEvent extends BaseEvent {
  constructor() {
    super('messageReactionAdd');
  }
  
  async run(client, reaction, user) {
    const message = reaction.message;
    const member = message.guild.members.cache.get(user.id);

    if(user.bot) return;
 
    const Valorant = message.guild.roles.cache.get(process.env.DISCORD_ROLE_VALORANT);
    const League = message.guild.roles.cache.get(process.env.DISCORD_ROLE_LEAGUE);

    if(
      ["?", "?"].includes(reaction.emoji.name)
  ) {
      switch(reaction.emoji.name) {

          case"?":
          if (message.member.roles.cache.has(process.env.DISCORD_ROLE_VALORANT)) {
            console.log('Has Valorant')
          } else {
            member.roles.add(Valorant)
            member.createDM().then( channel => {
                channel.send("Added Valorant")
            })
          }
          
          break;

          case"?":
          if (message.member.roles.cache.has(process.env.DISCORD_ROLE_LEAGUE)) {
            console.log('Has League of Legends')
          } else {
            member.roles.add(League)
            member.createDM().then( channel => {
                channel.send("Added League of Legends")
            })
          }
      }
    }
  }
}

MessageReactionRemoveEvent:

const BaseEvent = require('../utils/structures/BaseEvent');
require('dotenv').config();
module.exports = class MessageReactionRemoveEvent extends BaseEvent {
  constructor() {
    super('messageReactionRemove');
  }
  
  async run(client, reaction, user) {
    const message = reaction.message;
    const member = message.guild.members.cache.get(user.id);

    if(user.bot) return;
 
    const Valorant = message.guild.roles.cache.get(process.env.DISCORD_ROLE_VALORANT);
    const League = message.guild.roles.cache.get(process.env.DISCORD_ROLE_LEAGUE);
 
    if(
        ["?", "?"].includes(reaction.emoji.name)
    ) {
        switch(reaction.emoji.name) {
 
          case"?":
          if (message.member.roles.cache.has(process.env.DISCORD_ROLE_VALORANT)) {
            member.roles.remove(Valorant)
            member.createDM().then( channel => {
                channel.send("Added Valorant")
            })
          } else {
            console.log('Hasn´t Valorant')
          }
          
          break;

          case"?":
          if (message.member.roles.cache.has(process.env.DISCORD_ROLE_LEAGUE)) {
            member.roles.remove(League)
            member.createDM().then( channel => {
                channel.send("Added League of Legends")
            })
          } else {
            console.log('Hasn´t League of Legends')
          }
      }
    }
  }
}

RolesCommand:

const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require("discord.js")

module.exports = class RolesCommand extends BaseCommand {
  constructor() {
    super('roles', 'Team', []);
  }

  run(client, message, args) {
    try {
      message.delete()

      const embed = new Discord.MessageEmbed()
      .setTitle("Test1")
      .setDescription("Test2")
      .setColor("#FCCF00")
      .setFooter("Test3")

      message.channel.send(embed).then(async msg => {
          await msg.react("?")
          await msg.react("?")
      })

  } catch(e) {
    console.log(e)
    } 
  }
}

感谢到目前为止我所获得的所有帮助,社区已经非常帮助我-谢谢。

2 个答案:

答案 0 :(得分:2)

经过大量研究,并有一位朋友帮助了我,现在我做对了,希望我能帮助遇到同样问题的其他所有人。

具有角色反应的角色检查:

- message.member.roles.cache.has(process.env.DISCORD_ROLE_VALORANT)
+ member.roles.cache.has(process.env.DISCORD_ROLE_VALORANT)

自动删除用户反应:

const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
try {
    for (const reaction of userReactions.values()) {
        await reaction.users.remove(user.id);
    }
} catch (error) {
    console.error('Failed to remove reactions.');
}

答案 1 :(得分:0)

我想在您的reaction.remove();语句之前运行switch可以达到目的:)