简体   繁体   English

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

[英]How to role check with Role Reaction & Auto remove user Reaction

Hey I've gotten my role reaction so far but would like to make it so that after pressing the emoji the code asks if someone already has the group or not and accordingly adds or removes the group but I would also like to make it so that the reaction with the emoji is removed automatically so that only the bot reacts to the message that emoji is displayed.嘿,到目前为止,我已经得到了我的角色反应,但我想这样做,以便在按下表情符号后,代码会询问某人是否已经拥有该组,并相应地添加或删除该组,但我也想这样做对表情符号的反应会自动删除,因此只有机器人对显示表情符号的消息做出反应。

I will add the code I have written so far in the attachment and maybe there are some people who can help me.我将在附件中添加我到目前为止编写的代码,也许有一些人可以帮助我。

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

Thanks for all the help I get so far the community has already helped me very well - thanks for that.感谢到目前为止我得到的所有帮助,社区已经很好地帮助了我——谢谢。

With a lot of research and a friend who helped me I have now got it right and hope I can help everyone else who has the same problem.经过大量研究和一位帮助我的朋友,我现在做对了,希望我能帮助其他有同样问题的人。

Role Check with Role Reaction:带有角色反应的角色检查:

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

Auto remove user Reaction:自动删除用户反应:

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.');
}

I suppose that running reaction.remove();我想正在运行的reaction.remove(); before your switch statements should do the trick:)在你的switch语句应该做的伎俩之前:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM