Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Issues with async if statement calculating final value using promises from Discord.JS library api

I am having trouble I believe that is due to async. Here is the code:

const calculateMessageKarma = async (reaction) => {
  if (reaction.partial) {await reaction.fetch();}
  // Get The Member That Gave Karma & Verify Author = Karma Giver
  let mK = 0; let tMK = 0;
  //Upvotes
  if(await reaction.message.reactions.resolve(config.reactions.UPVOTE)){
    let uvotes = await reaction.message.reactions.resolve(config.reactions.UPVOTE).users.fetch()
    await uvotes.forEach(async vote => {
      let member = await reaction.client.guilds.cache.find((guild) => guild.id === config.guilds.zentrading).members.fetch(vote.id).then((value) => { return value; });
      if(vote.id === reaction.message.author.id) return; // Dont count reactions to own messages
      if (member.roles.highest.id == config.roles.guildlead.id) { mK += 5}
      else if (member.roles.highest.id == config.roles.guildmod.id) { mK += 4;}
      else if (member.roles.highest.id == config.roles.arahant.id) {mK += 3}
      else if (member.roles.highest.id == config.roles.non_returner.id) {mK += 2}
      else if (member.roles.highest.id == config.roles.once_returner.id) { mK += 1}
      else if (member.roles.highest.id == config.roles.non_returner.id) {mK += 1}
      else {mK += 0}
    })
  }
  //Downvotes
  if(await reaction.message.reactions.resolve(config.reactions.DOWNVOTE)){
    let dvotes = await reaction.message.reactions.resolve(config.reactions.DOWNVOTE).users.fetch()
    await dvotes.forEach(async vote => {
      let member = await reaction.client.guilds.cache.find((guild) => guild.id === config.guilds.zentrading).members.fetch(vote.id).then((value) => { return value; });
      if(vote.id === reaction.message.author.id) return; // Dont count reactions to own messages
      if (member.roles.highest.id == config.roles.guildlead.id) { mK += -5}
      else if (member.roles.highest.id == config.roles.guildmod.id) { mK += -4;}
      else if (member.roles.highest.id == config.roles.arahant.id) {mK += -3}
      else if (member.roles.highest.id == config.roles.non_returner.id) {mK += -2}
      else if (member.roles.highest.id == config.roles.once_returner.id) { mK += -1}
      else if (member.roles.highest.id == config.roles.non_returner.id) {mK += -1}
      else {mK += 0}
    })
  }
  if (mK > 0) { tMK = 2 + Math.floor(mK / 10) * 1;} 
  else if (mK < 0){ tMK = -1 + Math.floor(mK / 5) * -1;} 
  else { tMK = 0;}
  return tMK;
}

This is meant to count the number of two different reactions and assign weight of these reactions based on the members role and then take the value mK and calculate a score for the message based on a formula. The code works fine for config.reactions.UPVOTE but the config.reactions.DOWNVOTE if statement is almost being ignored. I have debugged and determined that the if statements are being evaluated but its not returning the proper calculation.

As well I know the ID's are correct as when messageReactionAdded and messageReactionRemove is called, it is checking the reaction before calling this function.

Node Version: 16.4.0

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I was able to find a solution by using the following:

As part of the documentation: await reaction.message.reactions.resolve(config.reactions.DOWNVOTE).users.fetch()

Resolves with a collection of users, mapped by their ids.

enter image description here

Therefore we need to use .values() to return a map that we can iterate using something like this:

let votes = await reaction.message.reactions.resolve(config.reactions.DOWNVOTE).users.fetch().then(users => {return users.values()})
    for(vote of votes){

This for loop now properly returns the values necessary to calculate mK.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!