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

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

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 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