Limit reactions to one per message

discord.js :[email protected] Node: v20.2.0 I'm trying to make a poll bot based on reactions and I'm trying to limit reactions so that each user can only be reacted to one emoji at a time. Currently I have a system that checks if a user is already reacted and if so, removes and new reactions until they remove their original reaction but I'm wondering if a system that would remove the old reaction is instead possible? This is the current system I have for the limiting.
interaction.client.on("messageReactionAdd", async (reaction, user) => {
//Check that the reaction is not a partial
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();

/*Check if:
the reactor is not a bot,
if the reaction is in a DM,
if the message reacted to is the poll for this interaction,
if the poll has ended and return if any are true */
if (
user.bot ||
!reaction.message.guild ||
pollMessage.id != reaction.message.id ||
timedOut
)
return;

//No new reactions emojis can be added
if (!reactionEmojis.includes(reaction._emoji.toString())) {
reaction.users.remove(user.id).catch(console.error);
return;
}

const { message } = reaction;
let member = reaction.message.guild.members.cache.get(user.id);

//Limit reactions to member if need be
if (roleID != null) {
if (!member.roles.cache.has(roleID.value)) {
reaction.users.remove(user.id).catch(console.error);
return;
}
}

if (!reactionCount.get(message))
reactionCount.set(message, new Collection());
const userCount = reactionCount.get(message);
userCount.set(user, (userCount.get(user) || 0) + 1);

if (userCount.get(user) > 1) {
reaction.users.remove(user);
return;
}
const pollMessageString = await generatePollBars(
pollMessage,
votingOptions
);
pollEmbed.setDescription(pollMessageString);
await pollMessage.edit({ embeds: [pollEmbed] });
});

interaction.client.on("messageReactionRemove", async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (
user.bot ||
!reaction.message.guild ||
pollMessage.id != reaction.message.id ||
timedOut
)
return;

const { message } = reaction;
const userCount = reactionCount.get(message);
// subtract 1 from user's reaction count
try {
userCount.set(user, reactionCount.get(message).get(user) - 1);
} catch (TypeError) {}
const pollMessageString = await generatePollBars(
pollMessage,
votingOptions
);
pollEmbed.setDescription(pollMessageString);
await pollMessage.edit({ embeds: [pollEmbed] });
});
interaction.client.on("messageReactionAdd", async (reaction, user) => {
//Check that the reaction is not a partial
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();

/*Check if:
the reactor is not a bot,
if the reaction is in a DM,
if the message reacted to is the poll for this interaction,
if the poll has ended and return if any are true */
if (
user.bot ||
!reaction.message.guild ||
pollMessage.id != reaction.message.id ||
timedOut
)
return;

//No new reactions emojis can be added
if (!reactionEmojis.includes(reaction._emoji.toString())) {
reaction.users.remove(user.id).catch(console.error);
return;
}

const { message } = reaction;
let member = reaction.message.guild.members.cache.get(user.id);

//Limit reactions to member if need be
if (roleID != null) {
if (!member.roles.cache.has(roleID.value)) {
reaction.users.remove(user.id).catch(console.error);
return;
}
}

if (!reactionCount.get(message))
reactionCount.set(message, new Collection());
const userCount = reactionCount.get(message);
userCount.set(user, (userCount.get(user) || 0) + 1);

if (userCount.get(user) > 1) {
reaction.users.remove(user);
return;
}
const pollMessageString = await generatePollBars(
pollMessage,
votingOptions
);
pollEmbed.setDescription(pollMessageString);
await pollMessage.edit({ embeds: [pollEmbed] });
});

interaction.client.on("messageReactionRemove", async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (
user.bot ||
!reaction.message.guild ||
pollMessage.id != reaction.message.id ||
timedOut
)
return;

const { message } = reaction;
const userCount = reactionCount.get(message);
// subtract 1 from user's reaction count
try {
userCount.set(user, reactionCount.get(message).get(user) - 1);
} catch (TypeError) {}
const pollMessageString = await generatePollBars(
pollMessage,
votingOptions
);
pollEmbed.setDescription(pollMessageString);
await pollMessage.edit({ embeds: [pollEmbed] });
});
2 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Suit
SuitOP2y ago
Forgot to say but reactionCount is a Collection!
Want results from more Discord servers?
Add your server