Hello I'm working on managment of role by reacting to a message and I don't want the roles to be cumulative. To make it cleaner, I want to remove the reactions of the user to other emojis. But when I want to get users that had reacted with a certain emoji it is empty. It's the same way when I get all reactions of the user on this message.
How to reproduce to bug:
client.on("messageReactionAdd", async (reaction, user) => {
const userReactions = reaction.message.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
console.log(userReactions);
});
Then to react to any message on a server that your bot is in.
For me, it prints : _Collection [Map] {}_
I tried with many other ways (and many time with dirty code 馃槩) but I all the time git the same result...
I think its obviously a bug and I didn't find anyone that talked about it before.
Further details:
Can confirm this.
My code:
client.on("messageReactionAdd", async (reaction, user) => {
console.log(reaction);
console.log(user);
});
Both console logs don't log anything when a user who is not currently cached in the client adds a reaction to a message.
For example, when using the following Discord gateway intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MESSAGE_REACTIONS', 'DIRECT_MESSAGES', 'DIRECT_MESSAGE_REACTIONS'] I get nothing from the messageReactionAdd event if an uncached user reacts to a message that is cached. However, if I remove the gateway intents completely from my client options I get both the user and reaction collection.
Reproduction steps:
messageReactionAdd and log to console data you get from it. ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MESSAGE_REACTIONS', 'DIRECT_MESSAGES', 'DIRECT_MESSAGE_REACTIONS'].I can not exactly reproduce that, @talle117.
If an uncached user adds a reaction, the event will only emit if the USER PartialType is enabled. (Regardless of intents)
See the relevant lines of code here:
https://github.com/discordjs/discord.js/blob/d827544fbd12e827fb4b6ff99d8894ecd79ede02/src/client/actions/MessageReactionAdd.js#L18-L19
Further down the call chain a partial check will be made if the user is not cached:
https://github.com/discordjs/discord.js/blob/d827544fbd12e827fb4b6ff99d8894ecd79ede02/src/client/actions/Action.js#L27-L31
In your cases there is no cached user and no partial type for users enabled, thus the function returns undefined and no event is being emitted.
You probably are not encountering this without intents because you then will have the PRESENCES implicitly enabled and receive initial online members causing the user to be cached.
Please specify actual versions in the template, latest is pretty much useless as it will change over time, @Zargith.
Also if you check I have also tested the issue on latest master, commit hash: actually fill in the commit hash you tested this on.
Again, latest is useless here for the same reason.
To your issue:
If you fetch a message, you will not get any users that added any reactions (read: MessageReaction#users will be empty).
You can manually fetch those in chunks of up to 100 by using MessageReactionUserManager#fetch.
If you have a lot of users using this feature, I'd strongly recommend to do that.
If you however only sparingly have users use this feature, you might be able to get away without caching all of them and simply directly using MessageReactionUserManager#remove on all other reactions. Note that this will likely produce a bunch of 4xx (I assume 404) responses. (Too many of those will get you an automated 1h ban, hence my suggestion above if this is not a sparingly used thing)
Most helpful comment
I can not exactly reproduce that, @talle117.
If an uncached user adds a reaction, the event will only emit if the
USERPartialTypeis enabled. (Regardless of intents)See the relevant lines of code here:
https://github.com/discordjs/discord.js/blob/d827544fbd12e827fb4b6ff99d8894ecd79ede02/src/client/actions/MessageReactionAdd.js#L18-L19
Further down the call chain a partial check will be made if the user is not cached:
https://github.com/discordjs/discord.js/blob/d827544fbd12e827fb4b6ff99d8894ecd79ede02/src/client/actions/Action.js#L27-L31
In your cases there is no cached user and no partial type for users enabled, thus the function returns
undefinedand no event is being emitted.You probably are not encountering this without intents because you then will have the
PRESENCESimplicitly enabled and receive initial online members causing the user to be cached.