Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Command } = require('@botbind/klasa');
  2. const Discord = require ('discord.js')
  3.  
  4. module.exports = class extends Command {
  5.   constructor(...args)
  6.     {
  7.     super(...args,
  8.       {
  9.             cooldown: 300,
  10.             description: 'A simple suggestiong command, it suggests the ideas.',
  11.             args: [
  12.                 {
  13.                     key: 'question',
  14.                     prompt: 'What is the poll question?',
  15.                     type: 'string',
  16.                     validate: question => {
  17.                         if (question.length < 101 && question.length > 11) return true;
  18.                         return 'Polling questions must be between 10 and 100 characters in length.';
  19.                     }  
  20.                 },
  21.                 {
  22.                     key: 'options',
  23.                     prompt: 'What options do you want for the poll?',
  24.                     type: 'string',
  25.                     validate: options => {
  26.                         var optionsList = options.split(",");
  27.                         if (optionsList.length > 1) return true;
  28.                         return 'Polling options must be greater than one.';
  29.                     }  
  30.                 },
  31.                 {
  32.                     key: 'time',
  33.                     prompt: 'How long should the poll last in minutes?',
  34.                     type: 'integer',
  35.                     default: 0,
  36.                     validate: time => {
  37.                         if (time >= 0 && time <= 60) return true;
  38.                         return 'Polling time must be between 0 and 60.';
  39.                     }
  40.                 },
  41.             ]
  42.       });  
  43.     }
  44.    async run(message, {question, options, time}) {
  45.         var emojiList = ['1⃣','2⃣','3⃣','4⃣','5⃣','6⃣','7⃣','8⃣','9⃣','🔟'];
  46.         var optionsList = options.split(",");
  47.         var optionsText = "";
  48.         for (var i = 0; i < optionsList.length; i++) {
  49.             optionsText += emojiList[i] + " " + optionsList[i] + "\n";
  50.         }
  51.         var embed = new Discord.MessageEmbed()
  52.             .setTitle(question)
  53.             .setDescription(optionsText)
  54.             .setAuthor(message.author.username, message.author.displayAvatarURL)
  55.             .setColor(0xD53C55)
  56.             .setTimestamp();
  57.         if (time) {
  58.             embed.setFooter(`The poll has started and will last ${time} minute(s)`);
  59.         } else {
  60.             embed.setFooter(`The poll has started and has no end time`);
  61.         }
  62.         message.channel.send({embed})
  63.             .then(async function (message) {
  64.                 var reactionArray = [];
  65.                 for (var i = 0; i < optionsList.length; i++) {
  66.                     reactionArray[i] = await message.react(emojiList[i]);
  67.                 }
  68.                 if (time) {
  69.                     setTimeout(() => {
  70.                         message.channel.fetchMessage(message.id)
  71.                             .then(async function (message) {
  72.                                 var reactionCountsArray = [];
  73.                                 for (var i = 0; i < optionsList.length; i++) {
  74.                                     reactionCountsArray[i] = message.reactions.get(emojiList[i]).count-1;
  75.                                 }
  76.                                 var max = -Infinity, indexMax = [];
  77.                                 for(var i = 0; i < reactionCountsArray.length; ++i)
  78.                                     if(reactionCountsArray[i] > max) max = reactionCountsArray[i], indexMax = [i];
  79.                                     else if(reactionCountsArray[i] === max) indexMax.push(i);
  80.                                 console.log(reactionCountsArray);
  81.                                 var winnersText = "";
  82.                                 if (reactionCountsArray[indexMax[0]] == 0) {
  83.                                     winnersText = "No one voted!"
  84.                                 } else {
  85.                                     for (var i = 0; i < indexMax.length; i++) {
  86.                                         winnersText +=
  87.                                             emojiList[indexMax[i]] + " " + optionsList[indexMax[i]] +
  88.                                             " (" + reactionCountsArray[indexMax[i]] + " vote(s))\n";
  89.                                     }
  90.                                 }
  91.                                 embed.addField("**Winner(s):**", winnersText);
  92.                                 embed.setFooter(`The poll is now closed! It lasted ${time} minute(s)`);
  93.                                 embed.setTimestamp();
  94.                                
  95.                                 message.edit("", embed);
  96.                             });
  97.                     }, time * 60 * 1000);
  98.                 }
  99.             })}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement