I want to add on reaction emote remove to this code client.on(Events.MessageReactionAdd, async (reaction,user) =>{
if (reaction.partial) {
try{
await reaction.fetch();
if (reaction.message.channel.id !== channel_Id) return;
if (user.bot) return;
console.log(reaction);
const roleName = emojisList[reaction.emoji.name];
if (!roleName) return;
code
// get the role object from the guild
const guild = reaction.message.guild;
const role = guild.roles.cache.find(r => r.name === roleName);
if (!role) return;
// add the role to the user/member
const member = await guild.members.fetch(user.id);
await member.roles.add(role);
console.log(`Added ${roleName} role to ${member.user.tag}`);
To add the functionality to remove a reaction emote, you can modify the code like this:
code
User
I want to add when I remove my emote from message this code role is removed too client.on(Events.MessageReactionAdd, async (reaction,user) =>{
if (reaction.partial) {
try{
await reaction.fetch();
if (reaction.message.channel.id !== channel_Id) return;
if (user.bot) return;
console.log(reaction);
const roleName = emojisList[reaction.emoji.name];
if (!roleName) return;
// get the role object from the guild
const guild = reaction.message.guild;
const role = guild.roles.cache.find(r => r.name === roleName);
if (!role) return;
// add the role to the user/member
const member = await guild.members.fetch(user.id);
await member.roles.add(role);
console.log(Added roleNamero{member.user.tag});
}
catch(error) {
console.error(‘Error assigning role:’, error);
Assistant
To remove the role when the user removes their own reaction emote, you can modify the code like this:
code
User
check if the reaction on message is removed
Assistant
To check if a reaction on a message is removed, you can add an Events.MessageReactionRemove event listener in your code. Here's an example:
code
User
client.on(Events.MessageReactionRemove, async (reaction,user) =>{
if (reaction.partial) {
try{
console.log('working test');
await reaction.fetch();
if (reaction.message.channel.id !== channel_Id) return;
if (user.bot) return;
const roleName = emojisList[reaction.emoji.name];
if (!roleName) return;
// get the role object from the guild
const guild = reaction.message.guild;
const role = guild.roles.cache.find(r => r.name === roleName);
if (!role) return;
// remove the role from the user/member
const member = await guild.members.fetch(user.id);
await member.roles.remove(role);
console.log(Removed ${roleName} role from ${member.user.tag});
}
catch(error) {
console.error('Error removing role:', error);
}
}
});
why this code not working
Assistant
The console.log statement in your code should work properly, so if you're seeing the "working test" message printed in the console when a reaction is removed, that's a good sign. However, I see that you have an indentation error in your code.
In the try block, the line with console.log('working test'); and all the lines that follow it should be indented by one level less. So it should look like this:
code
client.on(Events.MessageReactionAdd, async (reaction,user) =>{
if (reaction.partial) {
try{
await reaction.fetch();
if (reaction.message.channel.id !== channel_Id) return;
if (user.bot) return;
console.log(reaction);
const roleName = emojisList[reaction.emoji.name];
if (!roleName) return;
// get the role object from the guild
const guild = reaction.message.guild;
const role = guild.roles.cache.find(r => r.name === roleName);
if (!role) return;
// add or remove the role to/from the user/member
const member = await guild.members.fetch(user.id);
if (reaction._emoji.name === '❌') {
await member.roles.remove(role);
console.log(`Removed ${roleName} role from ${member.user.tag}`);
} else {
await member.roles.add(role);
console.log(`Added ${roleName} role to ${member.user.tag}`);
}
}
catch(error) {
console.error('Error assigning/removing role:', error);
}
}
});
In this modified code, we use an if/else statement to check if the reaction emote is the "❌" emote. If it is, we remove the corresponding role from the user/member. Otherwise, we add the role as before. We also slightly updated the error message for when there is an error assigning or removing a role.
l
e
t
o
}
}});
client.on(Events.MessageReactionAdd, async (reaction,user) =>{
if (reaction.partial) {
try{
await reaction.fetch();
if (reaction.message.channel.id !== channel_Id) return;
if (user.bot) return;
console.log(reaction);
const roleName = emojisList[reaction.emoji.name];
if (!roleName) return;
// get the role object from the guild
const guild = reaction.message.guild;
const role = guild.roles.cache.find(r => r.name === roleName);
if (!role) return;
// add or remove the role to/from the user/member
const member = await guild.members.fetch(user.id);
if (reaction._emoji.name === '❌') {
await member.roles.remove(role);
console.log(`Removed ${roleName} role from ${member.user.tag}`);
} else {
await member.roles.add(role);
console.log(`Added ${roleName} role to ${member.user.tag}`);
}
}
catch(error) {
console.error('Error assigning/removing role:', error);
}
} else {
if (reaction.message.channel.id !== channel_Id) return;
if (user.bot) return;
const roleName = emojisList[reaction.emoji.name];
if (!roleName) return;
// get the role object from the guild
const guild = reaction.message.guild;
const role = guild.roles.cache.find(r => r.name === roleName);
if (!role) return;
// remove the role from the user/member
const member = await guild.members.fetch(user.id);
await member.roles.remove(role);
console.log(`Removed ${roleName} role from ${member.user.tag} due to reaction removal`);
}
});
In this modified code, we first check if the reaction is partial (i.e. we need to fetch it) and handle the addition of the role as before. However, we also added an else block to handle the case where the user removes their own reaction emote. In this case, we remove the corresponding role from the user. We also added a log statement to indicate that the role was removed due to reaction removal.
client.on(Events.MessageReactionAdd, async (reaction,user) =>{
if (reaction.partial) {
try{
await reaction.fetch();
if (reaction.message.channel.id !== channel_Id) return;
if (user.bot) return;
console.log(reaction);
const roleName = emojisList[reaction.emoji.name];
if (!roleName) return;
// get the role object from the guild
const guild = reaction.message.guild;
const role = guild.roles.cache.find(r => r.name === roleName);
if (!role) return;
// add the role to the user/member
const member = await guild.members.fetch(user.id);
await member.roles.add(role);
console.log(`Added ${roleName} role to ${member.user.tag}`);
}
catch(error) {
console.error('Error assigning role:', error);
}
}
});
client.on(Events.MessageReactionRemove, async (reaction,user) =>{
if (reaction.partial) {
try{
await reaction.fetch();
if (reaction.message.channel.id !== channel_Id) return;
if (user.bot) return;
console.log(reaction);
const roleName = emojisList[reaction.emoji.name];
if (!roleName) return;
// get the role object from the guild
const guild = reaction.message.guild;
const role = guild.roles.cache.find(r => r.name === roleName);
if (!role) return;
// remove the role from the user/member
const member = await guild.members.fetch(user.id);
await member.roles.remove(role);
console.log(`Removed ${roleName} role from ${member.user.tag}`);
}
catch(error) {
console.error('Error removing role:', error);
}
}
});
In this modified code, we added another event listener for Events.MessageReactionRemove that handles the case where a user removes their own reaction emote. It is similar to the Events.MessageReactionAdd listener but instead removes the corresponding role from the user.
Note that the reaction.partial check is done just as a safety measure, but it may not always be necessary.
client.on(Events.MessageReactionRemove, async (reaction,user) =>{
if (reaction.partial) {
try{
console.log('working test');
await reaction.fetch();
if (reaction.message.channel.id !== channel_Id) return;
if (user.bot) return;
const roleName = emojisList[reaction.emoji.name];
if (!roleName) return;
// get the role object from the guild
const guild = reaction.message.guild;
const role = guild.roles.cache.find(r => r.name === roleName);
if (!role) return;
// remove the role from the user/member
const member = await guild.members.fetch(user.id);
await member.roles.remove(role);
console.log(`Removed ${roleName} role from ${member.user.tag}`);
}
catch(error) {
console.error('Error removing role:', error);
}
}
});
Make sure you also have the channel_Id and emojisList defined somewhere in your code before this listener function.
If the code still doesn't work after fixing the indentation error and making sure that the necessary variables are defined, let me know what errors or issues you're encountering, and I can try to help you further.