如何检查反应用户角色?

时间:2021-06-16 02:18:53

标签: javascript node.js discord discord.js

我试图通过对按钮做出反应来关闭工单。而且我希望只有具有特定角色的人才能对此做出反应,我的代码:

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

2 个答案:

答案 0 :(得分:1)

我不确定反应是否有成员属性,但如果有,请执行以下操作:

.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

如果没有,则使用 guild.member(user)

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

我也没有时间测试这个,告诉我它是否有效!

答案 1 :(得分:1)

您不能从反应中访问成员,但是您可以访问消息然后访问公会,因此您可以从公会中获取成员。 我正在获取反应和消息,所以如果它们没有被缓存,我们可以得到它们。

我建议检查 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)
  }
  }
});