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

177
Views
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 answers
Answer question

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