Advertisement
DudeThatsErin

Untitled

Feb 19th, 2021
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Start of the file
  2.  
  3. module.exports = {
  4.     name: 'randomwinner',
  5.     description: "This is a random winner command. Pull a random winner from the reactions!",
  6.  
  7.     execute(message, args, Discord, client) {
  8.         //This command only to be used with role named 'Moderator'
  9.         if (message.member.roles.cache.some(role => role.name === 'Management')) {
  10.             if (!args.length) {
  11.                 return message.channel.send(`You didn't provide any arguments, ${message.author}!`);
  12.            }
  13.            numWinners = args[0];
  14.            //console.log('This is the number of Winners:');
  15.            //console.log(numWinners);
  16.            //console.log('------------------------------');
  17.            //console.log('This is the emojicode pre-fix : ' + args[1])
  18.            var emojiCode = args[1].split(':')[2];
  19.            //console.log('This is the emoji code after the first split : ' + emojiCode);
  20.            emojiCode = emojiCode.replace('>', '');
  21.            //console.log('This is the emoji code for the emoji you will track');
  22.            //console.log(emojiCode);
  23.  
  24.            message.channel.messages.fetch().then(async messages => {
  25.                for (const msg of messages.array()) {
  26.                    resultIndex = msg.reactions.cache.map((value, index) => index);
  27.                    resultCount = msg.reactions.cache.map((value, index) => value.count);
  28.                    var selIndex = -1;
  29.                    var numVotes = 0;
  30.                    for (var i = 0; i < resultIndex.length; i++) {
  31.                        if (resultIndex[i] == emojiCode) {
  32.                            selIndex = i;
  33.                            break;
  34.                        }
  35.                    }
  36.                    if (selIndex != -1) {
  37.                        numVotes = resultCount[selIndex];
  38.                        //console.log("number of reactions: " + numVotes);
  39.                        msg.reactions.resolve(emojiCode).users.fetch().then(x => {
  40.                            userList = x.map((value, index) => value);
  41.                            //console.log(userList[0].username);
  42.                            //console.log('----------');
  43.                            //console.log(userList.length);
  44.                            //console.log(userList)
  45.  
  46.                            const newWinnerEmbed = new Discord.MessageEmbed()
  47.                                .setColor('#304281')
  48.                                .setTitle("Contest Entries")
  49.                                .setDescription('These are the winners of the contest! Congratulations!')
  50.                                .setFooter('Work for this command has been done by [EB]EternalFragment#5420 and AmeiLotus')
  51.  
  52.                            var d = [];
  53.                            for (z = 0; z < numWinners; z++) {
  54.                                generateUniqueRandom(numWinners);
  55.                            }
  56.                            //console.log('Unique random numbers:' , haveIt);
  57.  
  58.                            for (z = 0; z < numWinners; z++) {
  59.                                newWinnerEmbed.addFields({ name: "Winner #" + (z + 1) + " - ", value: '<@' + userList[haveIt[z]] + '>' })
  60.                            }
  61.  
  62.                            message.channel.send(newWinnerEmbed);
  63.  
  64.                            //resets the haveIt array for new commands.
  65.                            haveIt = [];
  66.                        });
  67.                    }
  68.  
  69.                }
  70.            }); //finish messages fetch .then
  71.        } else {
  72.            message.channel.send("You don't have the role");
  73.        }
  74.  
  75.    } //end of execute function
  76. }// end of module.export
  77.  
  78. // START UNIQUE NUMBER GENERATOR.
  79. // MAKE SURE TO CLEAR IT AFTER YOUR COMMAND IS OVER USING `haveIt = [];`
  80. let haveIt = [];
  81.  
  82. function generateUniqueRandom(maxNr) {
  83.    //Generate random number
  84.    let random = (Math.random() * (maxNr - 1)).toFixed();
  85.  
  86.    //Coerce to number by boxing
  87.    random = Number(random);
  88.  
  89.    if (!haveIt.includes(random)) {
  90.        haveIt.push(random);
  91.        return random;
  92.    } else {
  93.        if (haveIt.length < maxNr) {
  94.            //Recursively generate number
  95.            return generateUniqueRandom(maxNr);
  96.        } else {
  97.            console.log('No more numbers available.')
  98.            return false;
  99.        }
  100.    }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement