Created
April 17, 2024 03:26
-
-
Save harishkotra/09ee64d76b3e60d0982d91d78c62c695 to your computer and use it in GitHub Desktop.
✅ Discord.js v14.14.1: Remove Role on Reaction Removal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Easily remove roles from Discord users when they remove specific reactions using discord.js v14.14.1. (discord.js 14+) | |
*/ | |
const { Client, Events, GatewayIntentBits, Partials } = require('discord.js'); | |
const client = new Client({ | |
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions], | |
partials: [Partials.Message, Partials.Channel, Partials.Reaction], | |
}); | |
client.on(Events.MessageReactionRemove, async (reaction, user) => { | |
// **Error Handling:** Check for partial reactions | |
if (reaction.partial) { | |
try { | |
await reaction.fetch(); | |
} catch (error) { | |
console.error('Error fetching message:', error); | |
return; // Prevent potential errors by stopping execution | |
} | |
} | |
// **Configuration:** Replace these placeholders with your actual values | |
const channelID = 'YOUR_CHANNEL_ID'; // Channel ID where the reaction is added | |
const targetMessageID = 'YOUR_MESSAGE_ID'; // Message ID where the reaction is added | |
const reactionEmoji = 'YOUR_REACTION_EMOJI'; // Name of the emoji to remove role on | |
const roleID = 'YOUR_ROLE_ID'; // Role ID to be removed | |
// **Conditional Check:** Ensure correct channel, message, and emoji | |
if ( | |
reaction.message.channelId === channelID && | |
reaction.message.id === targetMessageID && | |
reaction.emoji.name === reactionEmoji | |
) { | |
const guildMember = reaction.message.guild.members.cache.get(user.id); | |
const role = reaction.message.guild.roles.cache.get(roleID); | |
if (guildMember.roles.cache.find(role => role.id === roleID)) { | |
// **Remove Role:** User has the role, so remove it | |
await guildMember.roles.remove(role); | |
console.log(`${user.username} removed role ${role.name}`); | |
} else { | |
// User doesn't have the role (informative message optional) | |
console.log(`${user.username} doesn't have role ${role.name}`); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment