get all reaction on message with specific id discord js
- User
get all reaction on message with specific id discord js
- Assistant
To get all reactions on a specific message with a specific ID, you can use the
Message.reactionsproperty. This property returns aReactionManager, which allows you to fetch all of the reactions on the message.Here's an example code snippet that demonstrates how to get all reactions on a message with a specific ID:
// assuming you have obtained the message object with the desired ID somehow, for example: const message = await message.channel.messages.fetch('123456789012345678') // fetch all reactions on the message const reactionManager = message.reactions // iterate over each reaction and log the user IDs of the users who reacted reactionManager.cache.forEach(reaction => { console.log(`Users who reacted with ${reaction.emoji.name}:`) reaction.users.cache.forEach(user => { console.log(` - ${user.id}`) }) })This code iterates over each reaction on the message, then for each reaction, it iterates over each user who reacted and logs their user ID.
- User
users id do not appear
- Assistant
If the user IDs are not appearing, it's possible that your bot does not have permission to access the user data for the reactions. To fix this, you need to enable the
GUILD_MEMBERSintent for your bot and enable privileged intents in the Discord developer portal.Here's how to do it:
- Go to the Discord Developer Portal and select your bot application.
- Go to the "Bot" tab and scroll down to the "Privileged Gateway Intents" section.
- Enable the "Server Members Intent" (GUILD_MEMBERS) toggle.
- Save changes.
Once you've enabled the GUILD_MEMBERS intent, you also need to enable it in your Discord.js client:
const { Client, Intents } = require('discord.js'); const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS], partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });Then you should be able to fetch the user IDs of the people who reacted with the updated code:
// assuming you have obtained the message object with the desired ID somehow, for example: const message = await message.channel.messages.fetch('123456789012345678') // fetch all reactions on the message const reactionManager = message.reactions // iterate over each reaction and log the user IDs of the users who reacted reactionManager.cache.forEach(async reaction => { console.log(`Users who reacted with ${reaction.emoji.name}:`) const userIDs = await reaction.users.fetch().then(users => users.map(u => u.id)) console.log(userIDs) })This code fetches the user IDs of each user who reacted using the
reaction.users.fetch()method and then logs them to the console. - User
I want to cache users with their reactions
- Assistant
To cache the users with their reactions, you can create a
Mapobject that maps user IDs to an array of reaction objects. Here's an example code snippet that demonstrates how to do this:// assuming you have obtained the message object with the desired ID somehow, for example: const message = await message.channel.messages.fetch('123456789012345678') // create a map to store the user reactions const userReactions = new Map() // fetch all reactions on the message const reactionManager = message.reactions // iterate over each reaction and add the user ID to the corresponding array in the userReactions map reactionManager.cache.forEach(async reaction => { const userIDs = await reaction.users.fetch().then(users => users.map(u => u.id)) userIDs.forEach(userID => { if (!userReactions.has(userID)) { userReactions.set(userID, [{ name: reaction.emoji.name, count: reaction.count }]) } else { const reactions = userReactions.get(userID) reactions.push({ name: reaction.emoji.name, count: reaction.count }) userReactions.set(userID, reactions) } }) }) // log the user reactions console.log("User reactions:") userReactions.forEach((reactions, userID) => { console.log(`User ${userID} reacted with:`) reactions.forEach(reaction => { console.log(` - ${reaction.name} (count: ${reaction.count})`) }) })This code creates a
Mapobject calleduserReactionsthat maps user IDs to an array of reaction objects. It then fetches all the reactions on the message and iterates over each reaction to add the corresponding user ID and reaction object to theuserReactionsmap. Finally, it logs the user reactions to the console.Note that this code only fetches the user IDs and the emoji names and counts of their reactions. If you need to fetch more information about the users or reactions, you can modify the code accordingly.