Reputation: 41
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)
```
Upvotes: 1
Views: 103
Reputation: 9041
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)
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!
Upvotes: 1
Reputation: 142
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 checking partials, 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
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)
}
}
});
Upvotes: 1