Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda