V12 Discord.js retrieve user.id from a message reactions

Viewed 238

i'm struggling to retrieve in V12 of discord.js the user.id and user.name for a message that they react too. The idea is a message is pinned and the user selects between 1-7 each day and it adds a point to their total. It will subtract from their total as well, if they deselect any of the reactions. But I haven't written that out yet. Currently it fails with TypeError: Cannot read property 'guild' of undefined on const Character when trying to get the user.id

config.json

{
"prefix":"!",
"channel": "xxx",
"channelID": "xxx",
}

SQLite

client.getdayCount = DB.prepare("SELECT dayCount from Table WHERE id = ?;");
client.setdayCount = DB.prepare("INSERT OR REPLACE INTO Table (id, dayCount, guild) VALUES (@id, @dayCount @guild);");

Main Code

client.on('messageReactionAdd', addRole);
client.on('messageReactionRemove', removeRole);

async function addRole(message, reaction, user) {

if (message.partial) {
   try {
       await message.fetch();
   } catch (err) {
  console.error('Error fetching message', err);
  return;
   }
}


client.channels.cache.get(config.channelID).messages.fetch({
limit: 1
}).then(messages => {
const lastMessage = messages.first()
const character = reaction.message.guild.member(user.id);
//Filter the reaction
const addition = +1;
if (lastMessage.reactions.cache.emojis === "1️⃣" || "2️⃣" || "3️⃣" || "4️⃣" || "5️⃣" || "6️⃣" || "7️⃣") {
  // Define the emoji user add
   //const character = lastMessage.guild.member(user.id);
  //reaction.message.guild.member(user.id);
  if (character) //if author has sent before
  {
      character.setdayCount += addition;
      client.setdayCount.run(character);
      return;
    
  } else //else add new author to collection
  {
    character = {
      id: user.id,
      setdayCount: addition,
      guild: reaction.guild.id,
    }

  }
} else {

};
}).catch(err => {
console.error(err)
})
}
1 Answers

You are using incorrect parameters for your function, in discord js v12 the messageReactionAdd only has two parameters, messageReaction and user. Therefore the parameter that you called reaction is actually a User object which doesn't have a property named message and causes your error.

Additionally

if (lastMessage.reactions.cache.emojis === "1️⃣" || "2️⃣" || "3️⃣" || "4️⃣" || "5️⃣" || "6️⃣" || "7️⃣") {

will always be true, I think what you intend to do is

if (["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣"].includes(lastMessage.reactions.cache.emojis)) {
Related