Advertisement
Guest User

giveaway.js

a guest
Jan 20th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { RichEmbed } = require("discord.js");
  2. const { stripIndents } = require("common-tags");
  3.  
  4. module.exports = {
  5.   name: "giveaway",
  6.   category: "fun",
  7.   description: "Makes a new giveaway for an alloted time.",
  8.   usage:
  9.     "n.giveaway <time> [form of time] [quantity] [amount of winners possible] [gift]",
  10.   run: async (client, message, args) => {
  11.     if (message.deletable) message.delete();
  12.     const errorEmbed = new RichEmbed();
  13.     const successEmbed = new RichEmbed();
  14.     let time = args[0];
  15.     let formTime = args[1];
  16.     let quantity = args[2];
  17.     let amountWinners = args[3];
  18.     let gift = args.splice(4, args.length).join(" ");
  19.     let eventsRole = message.guild.roles.find("id", "665705887196774450");
  20.     const giveawayChannel = message.guild.channels.find(
  21.       channel => channel.id === `665714033982308377`
  22.     );
  23.     if (!message.member.roles.has(eventsRole.id)) {
  24.       return message.channel
  25.         .send(
  26.           errorEmbed
  27.             .setColor("#FFE9CE")
  28.             .setDescription(
  29.               `❌ **You do not have permission to use this command!**`
  30.             )
  31.         )
  32.         .then(m => m.delete(5000));
  33.     }
  34.     if (!time) {
  35.       return message.channel
  36.         .send(
  37.           errorEmbed
  38.             .setColor("#FFE9CE")
  39.             .setDescription(`❌ **You must specify a time for the giveaway!**`)
  40.         )
  41.         .then(m => m.delete(5000));
  42.     }
  43.     if (!formTime) {
  44.       return message.channel
  45.         .send(
  46.           errorEmbed
  47.             .setColor("#FFE9CE")
  48.             .setDescription(
  49.               `❌ **You must specify a form of time for the giveaway!**`
  50.             )
  51.         )
  52.         .then(m => m.delete(5000));
  53.     }
  54.     if (!quantity) {
  55.       return message.channel
  56.         .send(
  57.           errorEmbed
  58.             .setColor("#FFE9CE")
  59.             .setDescription(
  60.               `❌ **You must specify how much you are giving away!**`
  61.             )
  62.         )
  63.         .then(m => m.delete(5000));
  64.     }
  65.     if (!amountWinners) {
  66.       return message.channel
  67.         .send(
  68.           errorEmbed
  69.             .setColor("#FFE9CE")
  70.             .setDescription(
  71.               `❌ **You must specify how many winners you want for the giveaway!**`
  72.             )
  73.         )
  74.         .then(m => m.delete(5000));
  75.     }
  76.     if (!gift) {
  77.       return message.channel
  78.         .send(
  79.           errorEmbed
  80.             .setColor("#FFE9CE")
  81.             .setDescription(`❌ **You must specify what you are giving away!**`)
  82.         )
  83.         .then(m => m.delete(5000));
  84.     }
  85.     if (formTime === "second") {
  86.       let newTime = Date.now() + parseInt(time) * 1000;
  87.     } else if (formTime === "minute") {
  88.       let newTime = Date.now() + parseInt(time) * 1000 * 60;
  89.     } else if (formTime === "hour") {
  90.       let newTime = Date.now() + parseInt(time) * 1000 * 60 * 60;
  91.     } else if (formTime === "day") {
  92.       let newTime = Date.now() + parseInt(time) * 1000 * 60 * 60 * 24;
  93.     } else {
  94.       return message.channel
  95.         .send(
  96.           errorEmbed
  97.             .setColor("#FFE9CE")
  98.             .setDescription(
  99.               `❌ **You must specify a valid form of time for the giveaway!**`
  100.             )
  101.         )
  102.         .then(m => m.delete(5000));
  103.     }
  104.     message.channel.send(
  105.       successEmbed
  106.         .setColor("#FFE9CE")
  107.         .setTimestamp()
  108.         .setDescription(`✅ **Successfully created giveaway!**`)
  109.     );
  110.     const giveawayEmbed = new RichEmbed()
  111.       .setTitle(`Giveaway`)
  112.       .setColor("#FFE9CE")
  113.       .setTimestamp()
  114.       .setFooter(`You must react to enter!`)
  115.       .addField(
  116.         `**${gift}** (x${quantity})`,
  117.         stripIndents`**Host:** ${message.author}
  118.         **Time of Giveaway:** ${time} ${formTime}(s)`
  119.       );
  120.     let azukiFiesty = await client.emojis.find(
  121.       emoji => emoji.name === "azukifiesty"
  122.     );
  123.     var sendGiveaway = await giveawayChannel.send(giveawayEmbed);
  124.     sendGiveaway.react(azukiFiesty);
  125.     if (Date.now() > time) {
  126.       var random = 0;
  127.       var winners = [];
  128.       var inList = false;
  129.       var participants = sendGiveaway.reactions
  130.         .get(azukiFiesty.id)
  131.         .users.fetch();
  132.       for (var i = 0; i < participants.length; i++) {
  133.         if (participants[i].id == client.user.id) {
  134.           participants.splice(i, 1);
  135.           continue;
  136.         }
  137.       }
  138.       if (participants.length == 0) {
  139.         const noWinnersEmbed = new RichEmbed()
  140.           .setTitle("No Winners")
  141.           .setTimestamp()
  142.           .setColor("#FFE9CE")
  143.           .setDescription(
  144.             `**There were no participants, so no one won the giveaway.** ˃̣̣̥⌓˂̣̣̥`
  145.           );
  146.         return giveawayChannel.send(noWinnersEmbed);
  147.       }
  148.       if (participants.length < amountWinners) {
  149.         const noWinnersEmbed = new RichEmbed()
  150.           .setTitle("No Winners")
  151.           .setTimestamp()
  152.           .setColor("#FFE9CE")
  153.           .setDescription(
  154.             `**There were not enough participants, so no one won the giveaway.** ˃̣̣̥⌓˂̣̣̥`
  155.           );
  156.         return giveawayChannel.send(noWinnersEmbed);
  157.       }
  158.       for (var i = 0; i < amountWinners; i++) {
  159.         inList = false;
  160.         random = Math.floor(Math.random() * participants.length);
  161.         for (var y = 0; y < winners.length; y++) {
  162.           if (winners[y] == participants[random]) {
  163.             i--;
  164.             inList = true;
  165.             break;
  166.           }
  167.         }
  168.         if (!inList) {
  169.           winners.push(participants[random]);
  170.         }
  171.         if (Date.now() > time) {
  172.           const logEmbed = new RichEmbed()
  173.             .setColor("#FFE9CE")
  174.             .setTimestamp()
  175.             .setFooter(
  176.               `To claim your prize, you must DM the host of this giveaway!`
  177.             )
  178.             .setTitle("Winner")
  179.             .setDescription(`**Congratulations! You have won ${gift}!**`);
  180.           giveawayChannel.send(winners, logEmbed);
  181.         }
  182.       }
  183.     }
  184.   }
  185. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement