Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda