Reading reaction info

I am attempting to take an array of reactions on a given message and identify the individual users that have posted each reaction. I have run into an issue where I can only reliably console.log the bot's user ID, and the list does not contain the rest of the user IDs unless the command is run immediately following a reaction state change (someone adds one). The other issue I'm running into is that I can console.log the information, but I can't seem to access it programmatically. This line of code is turning the reaction.users circular collection into something I can read in the console.
reactions.forEach(reaction => { if (reaction.count>1) { console.log(JSON.stringify(reaction.users,null,2)); } });
reactions.forEach(reaction => { if (reaction.count>1) { console.log(JSON.stringify(reaction.users,null,2)); } });
Before that line is stringifying the array, this block is trying to read each element of the reactions.users array and console log it:
let userIds= [];
for (const user in reactions.users) {
userIds.push(user);
}
console.log(`Ids: ${userIds}`);
let userIds= [];
for (const user in reactions.users) {
userIds.push(user);
}
console.log(`Ids: ${userIds}`);
This is not working. The console log is returning a list of userIDs in the stringified log, but returning Id: for the block that is trying to get the user IDs. I am not sure what to do or try next, as I've already dug through youtube, guides, and chatGPT
12 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Mark
Mark11mo ago
what does reaction.users.cache.size return
Requiem
Requiem11mo ago
no errors?
lexitorius
lexitoriusOP11mo ago
I will check on mark’s question in a bit But yeah it’s not throwing any errors, just returning an empty list
d.js docs
d.js docs11mo ago
Documentation suggestion for @lexitorius: :method: ReactionUserManager#fetch() Fetches all the users that gave this reaction. Resolves with a collection of users, mapped by their ids.
Bloonatics
Bloonatics11mo ago
you may need to fetch the users
lexitorius
lexitoriusOP11mo ago
TypeError: Cannot read properties of undefined (reading 'cache')
at Object.execute (C:\Users\mfros\js-proj
ects\ablazedglory\commands\server-events\cv.js:45:37)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Client.<anonymous> (C:\Users\mfros\js-projects\ablazedglory\index.js:245:8)
TypeError: Cannot read properties of undefined (reading 'cache')
at Object.execute (C:\Users\mfros\js-proj
ects\ablazedglory\commands\server-events\cv.js:45:37)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Client.<anonymous> (C:\Users\mfros\js-projects\ablazedglory\index.js:245:8)
Will look into reactionusermanager might have to watch some tutorial vids tho lol
Bloonatics
Bloonatics11mo ago
show what you have written
lexitorius
lexitoriusOP11mo ago
if (victors.length===1) {
thmb = highestCountReactions.map((info) => raidMasterList.find((raid) => raid.emj===info.name)?.icon || info.name);
title = `Raid Vote: ${victors}`;
desc = `${victors} wins with ${highestCountReactions[0].count} votes!`;
} else if (victors.length>1) {
title = `Raid Vote: TIE!`;
desc = `__Raid vote ended in a tie__\n\`-\` ${victors.join('\n\`-\` ')}`
}

const eRaidVote = new EmbedBuilder()
.setTitle(`${title}`)
.setDescription(`${desc}`)
.setColor(0x00ff00)
.setThumbnail(`${thmb}`)

console.log(reaction.users.cache.size);
//reactions.forEach(reaction => { if (reaction.count>1) { console.log(JSON.stringify(reaction.users,null,2)); } });
//trgtMsg.reactions.removeAll();
await interaction.reply({embeds: [eRaidVote]});
}
}
if (victors.length===1) {
thmb = highestCountReactions.map((info) => raidMasterList.find((raid) => raid.emj===info.name)?.icon || info.name);
title = `Raid Vote: ${victors}`;
desc = `${victors} wins with ${highestCountReactions[0].count} votes!`;
} else if (victors.length>1) {
title = `Raid Vote: TIE!`;
desc = `__Raid vote ended in a tie__\n\`-\` ${victors.join('\n\`-\` ')}`
}

const eRaidVote = new EmbedBuilder()
.setTitle(`${title}`)
.setDescription(`${desc}`)
.setColor(0x00ff00)
.setThumbnail(`${thmb}`)

console.log(reaction.users.cache.size);
//reactions.forEach(reaction => { if (reaction.count>1) { console.log(JSON.stringify(reaction.users,null,2)); } });
//trgtMsg.reactions.removeAll();
await interaction.reply({embeds: [eRaidVote]});
}
}
There's more above but not super related reactions is an array of the reaction objects
Bloonatics
Bloonatics11mo ago
the part that you define reactions
lexitorius
lexitoriusOP11mo ago
data: new SlashCommandBuilder().setName('cv').setDescription('closes a raid vote').addStringOption(
option => option.setName('id').setDescription('id of vote message').setRequired(true)
),
async execute(interaction) {
const guild = await interaction.client.guilds.fetch(guildId);
const channel = await interaction.channel;
const trgtId = await interaction.options.get('id').value;
const trgtMsg = await channel.messages.fetch(trgtId);
let thmb = 'https://cdn.discordapp.com/emojis/1070246146938773525.webp?size=128&quality=lossless';
let title,desc;
let reactions = trgtMsg.reactions.cache;
let reactionInfoArray = [];

reactions.forEach((reaction) => {
if (reaction.count > 1) {
const reactionInfo = {name: reaction.emoji.name, count: reaction.count - 1};
reactionInfoArray.push(reactionInfo);
}
})
const highestCountReactions = reactionInfoArray.filter((info) => info.count === Math.max(...reactionInfoArray.map((info) => info.count)));
const victors = highestCountReactions.map((info) => raidMasterList.find((raid) => raid.emj===info.name)?.name || info.name);

if (victors.length===1) {
thmb = highestCountReactions.map((info) => raidMasterList.find((raid) => raid.emj===info.name)?.icon || info.name);
title = `Raid Vote: ${victors}`;
desc = `${victors} wins with ${highestCountReactions[0].count} votes!`;
} else if (victors.length>1) {
title = `Raid Vote: TIE!`;
desc = `__Raid vote ended in a tie__\n\`-\` ${victors.join('\n\`-\` ')}`
}

const eRaidVote = new EmbedBuilder()
.setTitle(`${title}`)
.setDescription(`${desc}`)
.setColor(0x00ff00)
.setThumbnail(`${thmb}`)

console.log(reaction.users.cache.size);
//reactions.forEach(reaction => { if (reaction.count>1) { console.log(JSON.stringify(reaction.users,null,2)); } });
//trgtMsg.reactions.removeAll();
await interaction.reply({embeds: [eRaidVote]});
}
}
data: new SlashCommandBuilder().setName('cv').setDescription('closes a raid vote').addStringOption(
option => option.setName('id').setDescription('id of vote message').setRequired(true)
),
async execute(interaction) {
const guild = await interaction.client.guilds.fetch(guildId);
const channel = await interaction.channel;
const trgtId = await interaction.options.get('id').value;
const trgtMsg = await channel.messages.fetch(trgtId);
let thmb = 'https://cdn.discordapp.com/emojis/1070246146938773525.webp?size=128&quality=lossless';
let title,desc;
let reactions = trgtMsg.reactions.cache;
let reactionInfoArray = [];

reactions.forEach((reaction) => {
if (reaction.count > 1) {
const reactionInfo = {name: reaction.emoji.name, count: reaction.count - 1};
reactionInfoArray.push(reactionInfo);
}
})
const highestCountReactions = reactionInfoArray.filter((info) => info.count === Math.max(...reactionInfoArray.map((info) => info.count)));
const victors = highestCountReactions.map((info) => raidMasterList.find((raid) => raid.emj===info.name)?.name || info.name);

if (victors.length===1) {
thmb = highestCountReactions.map((info) => raidMasterList.find((raid) => raid.emj===info.name)?.icon || info.name);
title = `Raid Vote: ${victors}`;
desc = `${victors} wins with ${highestCountReactions[0].count} votes!`;
} else if (victors.length>1) {
title = `Raid Vote: TIE!`;
desc = `__Raid vote ended in a tie__\n\`-\` ${victors.join('\n\`-\` ')}`
}

const eRaidVote = new EmbedBuilder()
.setTitle(`${title}`)
.setDescription(`${desc}`)
.setColor(0x00ff00)
.setThumbnail(`${thmb}`)

console.log(reaction.users.cache.size);
//reactions.forEach(reaction => { if (reaction.count>1) { console.log(JSON.stringify(reaction.users,null,2)); } });
//trgtMsg.reactions.removeAll();
await interaction.reply({embeds: [eRaidVote]});
}
}
Bloonatics
Bloonatics11mo ago
this won't work if you're fetching older messages (not cached), you need to fetch the users https://discord.com/channels/222078108977594368/1210680446740992121/1210790135990460426
Want results from more Discord servers?
Add your server