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

178
Visualizações
Giveaway winner is coming out as undefined
    const ms = require('ms');
    const { MessageEmbed } = require('discord.js')


    module.exports = {
    name: 'giveaway',
    description: 'start a giveaway',
    async execute(client, message, cmd, args, Discord){
    let author = message.author.username;
    var time;



    time = args[0];
    let title = args.join(" ").slice(3);
    if(!message.member.permissions.has(['ADMINISTRATOR'])){
     message.channel.send(`Sorry **${author}** But you don't have enough permissions to use this command!`)
    } else {
    if(message.member.permissions.has(['ADMINISTRATOR'])){
    let giveawayEmbed = new MessageEmbed()
    .setColor('BLACK')
    .setThumbnail(`${message.author.displayAvatarURL()}`)
    .setTitle(`${author}'s Giveaway 🎉`)
    .setDescription(`**${title}**`)
    .addField(`Duration :`, ms(ms(time), {
      long: true
    }), true)
    .setFooter("React to this message with 🎉 to participate !")

  var giveawaySent =  await message.channel.send({ embeds: [giveawayEmbed] });
      giveawaySent.react('🎉');

console.log(giveawaySent.id)


setTimeout(async function(){

  const react = await giveawaySent.reactions.cache.find(r => r.emoji.name === '🎉').users.fetch();
  let index = Math.map(Math.random() * react.length);
  let winner = react[index];


  console.log(react)
  console.log(index)
  console.log(winner)

  let winnerEmbed = new MessageEmbed()
  .setColor('GREEN')
  .setDescription(`Congratulations **${winner}** you won **${title}**`)
  message.reply({ embeds: [winnerEmbed] })
}, ms(time))



  }

    }
  }
}

I'm trying to code a giveaway module however the winner result is coming out as undefined and I can't seem to figure out why, If you guys can help me out be greatly appreciated

These were logged by the console if it helps any index = NaN winner = undefined

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

    const ms = require('ms');
const { MessageEmbed } = require('discord.js')


module.exports = {
  name: 'giveaway',
  description: 'start a giveaway',
 async execute(client, message, cmd, args, Discord){
   let author = message.author.username;
   var time;



   time = args[0];
  let title = args.join(" ").slice(3);
   if(!message.member.permissions.has(['ADMINISTRATOR'])){
     message.channel.send(`Sorry **${author}** But you don't have enough permissions to use this command!`)
   } else {
  if(message.member.permissions.has(['ADMINISTRATOR'])){
    let giveawayEmbed = new MessageEmbed()
    .setColor('BLACK')
    .setThumbnail(`${message.author.displayAvatarURL()}`)
    .setTitle(`${author}'s Giveaway 🎉`)
    .setDescription(`**${title}**`)
    .addField(`Duration :`, ms(ms(time), {
      long: true
    }), true)
    .setFooter("React to this message with 🎉 to participate !")

  var giveawaySent =  await message.channel.send({ embeds: [giveawayEmbed] });
      giveawaySent.react('🎉');

console.log(giveawaySent.id)


setTimeout(async function(){

  const react = await giveawaySent.reactions.cache.find(r => r.emoji.name === '🎉').users.fetch();
  const reactArray = react.map(c => c)
  let index = Math.floor(Math.random() * reactArray.length);
  let winner = reactArray[index];

  console.log(reactArray)
//  console.log(react)
  console.log(index)
  console.log(winner)

  let winnerEmbed = new MessageEmbed()
  .setColor('GREEN')
  .setDescription(`Congratulations **${winner}** you won **${title}**`)
  message.reply({ embeds: [winnerEmbed] })
}, ms(time))



  }

    }
  }
}

Seems to have resolved the issue, Added const reactArray = react.map(c => c)

Changed let index = Math.floor(Math.random() * react.length); to let index = Math.floor(Math.random() * reactArray.length); which made const react = await giveawaySent.reactions.cache.find(r => r.emoji.name === '🎉').users.fetch(); into an array of user id's

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