简体   繁体   English

如何检查反应用户角色?

[英]How to check reaction user role?

I am trying to close a ticket by reacting to a button.我正在尝试通过对按钮做出反应来关闭票证。 And i want only who has a specific role can react to it, my code:我希望只有具有特定角色的人才能对此做出反应,我的代码:

client.on("messageReactionAdd", async (reaction, user) => {
    if (user.bot) return;
    const message = reaction.message;
    if (
        ["🔒"].includes(reaction.emoji.name)
    ) {
      const Embed = new MessageEmbed()
        .setColor(Color)
        .setDescription(`Closing Ticket In 5 Sec`)
       message.channel.send(Embed);
       

        setTimeout(() => {
            message.channel.delete()
        },  5000)
    ```

I am not sure if reaction has a member property but if it does, do this:我不确定反应是否具有成员属性,但如果有,请执行以下操作:

if(reaction.member.roles.find(r => r.name === 'role name?')) {
//returns false if member doesn’t have role
}

If it doesn't, then use guild.member(user)如果没有,则使用 guild.member(user)

if(reaction.message.guild.member(user).roles.cache.find(r => r.name === 'role name')) {
//Same thing
}

Also I didn't get time to test this, tell me if it works!我也没有时间测试这个,告诉我它是否有效!

You can't access the member from the reaction, however you can access the message then the guild, so you can get the member from the guild.您无法从反应中访问成员,但是您可以访问消息然后访问公会,因此您可以从公会中获取成员。 I'm fetching the reaction and message so if they're not cached we can get them.我正在获取反应和消息,所以如果它们没有被缓存,我们可以得到它们。

I recommend checkingpartials , because currently the bot will respond only to reactions added to cached message, this means if the bot is restarted you won't receive the event from old messages since they're not cached我建议检查partials ,因为目前机器人只会响应添加到缓存消息的反应,这意味着如果重新启动机器人,您将不会收到来自旧消息的事件,因为它们没有被缓存

client.on("messageReactionAdd", async (reaction, user) => {
  if (user.bot) return;
  await reaction.fetch();
  const { message, emoji } = reaction;
  await message.fetch();
  
  if (emoji.name === "🔒") {
  if (message.guild.member(user).roles.cache.find(r => r.name === "cool role")) {
    message.channel.send("Deleting Ticket!");
    
    setTimeout(() => {
        message.channel.delete();
    }, 5000)
  }
  }
});

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

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