[英]How to check reaction user role?
我正在尝试通过对按钮做出反应来关闭票证。 我希望只有具有特定角色的人才能对此做出反应,我的代码:
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)
```
我不确定反应是否具有成员属性,但如果有,请执行以下操作:
if(reaction.member.roles.find(r => r.name === 'role name?')) {
//returns false if member doesn’t have role
}
如果没有,则使用 guild.member(user)
if(reaction.message.guild.member(user).roles.cache.find(r => r.name === 'role name')) {
//Same thing
}
我也没有时间测试这个,告诉我它是否有效!
您无法从反应中访问成员,但是您可以访问消息然后访问公会,因此您可以从公会中获取成员。 我正在获取反应和消息,所以如果它们没有被缓存,我们可以得到它们。
我建议检查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.