Advertisement
AcTCrAFT2004

Giveaway

Feb 2nd, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Enmap = require('enmap');
  2. const cd = require('countdown');
  3. const moment = require('moment');
  4. const ms = require('ms');
  5. const totime = require('to-time');
  6. const dbg = new Enmap({ name: 'Giveaway' });
  7.  
  8. client.on('message',message => {
  9.   if (message.author.bot) return;
  10.   if (message.content === prefix+"help") {
  11.   message.channel.sendMessage('**:gift: - Help Menu\n--------------------------\n:tada: | ``$start`` --> To Start a Giveaway.\n:tada: | ``$groll`` --> To Choose Another Winner.\n:tada: | ``$gend`` --> To End Giveaway.\n--------------------------**');
  12. }
  13. });
  14.  
  15. /////////////////
  16. //gstart
  17. client.on("ready", async () => {
  18.   await dbg.defer;
  19.   await console.log(`Logged in as [ ${client.user.username} ]!`);
  20.   client.guilds.forEach(async g => {
  21.     g.channels
  22.       .filter(
  23.         c =>
  24.           c.type == "text" &&
  25.           c.permissionsFor(client.user.id).has("VIEW_CHANNEL")
  26.       )
  27.       .forEach(async c => {
  28.         let fetched = await c.fetchMessages();
  29.         if (fetched.size == 0) return;
  30.         let mess = await fetched.filter(
  31.           r =>
  32.             r.author.id === client.user.id &&
  33.             r.content ==
  34.               `**🎉 GIVEAWAY 🎉**`
  35.         );
  36.         if (mess.size == 0) return;
  37.         mess.forEach(m => {
  38.           if (!m) return;
  39.           if (!dbg.get(`giveaway.${g.id}.${c.id}.${m.id}.time`)) return;
  40.           let time2 = dbg.get(`giveaway.${g.id}.${c.id}.${m.id}.time`).gtime;
  41.           let text2 = dbg.get(`giveaway.${g.id}.${c.id}.${m.id}.time`).gtext;
  42.           let win2 = dbg.get(`giveaway.${g.id}.${c.id}.${m.id}.time`).gwin;
  43.           if (time2 === null || time2 === undefined) return;
  44.           let embed = new RichEmbed()
  45.             .setColor("BLUE")
  46.             .setAuthor(`${text2}`, g.iconURL)
  47.             .setDescription(
  48.               `React with 🎉 to enter!\nTime remaining: ${cd(
  49.                 new Date().getTime(),
  50.                 time2
  51.               )}`
  52.             )
  53.             .setFooter(`Ends at`, client.user.avatarURL)
  54.             .setTimestamp(time2);
  55.           let embed2 = new RichEmbed()
  56.             .setColor("RED")
  57.             .setAuthor(text2, g.iconURL)
  58.             .setFooter(`Ended at`);
  59.           let ttimer = setInterval(async () => {
  60.             if (
  61.               !m ||
  62.               m.content ==
  63.                 `🎉 **GIVEAWAY ENDED** 🎉`
  64.             )
  65.               return;
  66.             let ttt = [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10];
  67.             if (ttt.includes(moment().diff(time2, "seconds")))
  68.               return m.edit(
  69.                 `🎉 **GIVEAWAY** 🎉`,
  70.                 embed
  71.                   .setColor("#ffb800")
  72.                   .setDescription(
  73.                     `**Last chance to enter!!!**\nReact with 🎉\nTime remaining: ${cd(
  74.                       new Date().getTime(),
  75.                       time2
  76.                     )}`
  77.                   )
  78.               );
  79.             m.edit(
  80.               `🎉 **GIVEAWAY** 🎉`,
  81.               embed.setDescription(
  82.                 `React with 🎉 to enter!\nTime remaining: ${cd(
  83.                   new Date().getTime(),
  84.                   time2
  85.                 )}`
  86.               )
  87.             );
  88.             if (moment().isAfter(time2)) {
  89.               m.reactions
  90.                 .filter(a => a.emoji.name == "🎉")
  91.                 .map(r =>
  92.                   r.fetchUsers().then(u => {
  93.                     let rusers = u
  94.                       .filter(user => !user.bot)
  95.                       .random(parseInt(win2));
  96.                     m.edit(
  97.                       `${g} GIVEAWAY ENDED ${g}`,
  98.                       embed2
  99.                         .setTimestamp()
  100.                         .setDescription(`Winners:\n${rusers || "No winners"}`)
  101.                     );
  102.                     if (
  103.                       m.reactions
  104.                         .filter(a => a.emoji.name == "🎉")
  105.                         .map(reaction => reaction.count)[0] <= 1
  106.                     ) {
  107.                       return m.channel.send(`No winners :rolling_eyes:`);
  108.                     } else {
  109.                       m.channel.send(
  110.                         `Congratulations ${rusers}! You won the **${text2}**`
  111.                       );
  112.                     }
  113.                     dbg.delete(`giveaway.${g.id}.${c.id}.${m.id}.time`);
  114.                     clearInterval(ttimer);
  115.                     return;
  116.                   })
  117.                 );
  118.             }
  119.           }, 5000);
  120.         });
  121.       });
  122.   });
  123. });
  124. //client.on('error', console.error);
  125. //client.on('warn', warn => console.warn(`[WARN] - ${warn}`));
  126. process.on("unhandledRejection", (reason, promise) => {
  127.   console.log("Unhandled Rejection at:", reason.stack || reason);
  128. });
  129. client.on("message", async message => {
  130.   //let g = client.guilds
  131.   //  .get("606910399811420175")
  132.   //    .emojis.find(r => r.name === "start");
  133.   if (message.author.bot || message.channel.type == "dm") return undefined;
  134.   let args = message.content.split(" ");
  135.   let timer;
  136.   if (args[0] == `${prefix}start`) {
  137.     if (
  138.       message.member.hasPermission("MANAGE_GUILD") ||
  139.       message.member.roles.find(r => r.name == "GIVEAWAYS")
  140.     ) {
  141.       if (!args[1] || !args[2] || !args[3])
  142.         return message.channel.send(
  143.           `**Usage:** **\`${prefix}start [Time] [Winners] [Giveaway Prize]\n\`** **Example:** **\`${prefix}start 4h 1 Nitro\`**`
  144.         );
  145.       if (!message.guild.member(client.user).hasPermission("EMBED_LINKS"))
  146.         return message.channel.send(`I don't have **Embed Links** permission.`);
  147.      if (ms(args[1]) === undefined)
  148.        return message.channel.send(`Please use a proper time format.`);
  149.      if (isNaN(args[2]))
  150.        return message.channel.send(`Winners must be number!`);
  151.      if (args[2] < 1 || args[2] > 10)
  152.        return message.channel.send(`Winners must be bettwen 1 and 10.`);
  153.      let timega = ms(args[1]) / 1000;
  154.      let time = Date.now() + totime.fromSeconds(timega).ms();
  155.      if (timega < 5)
  156.        return message.channel.send(
  157.          `Giveaway time can't be less than 5 seconds.`
  158.         );
  159.       let timespan = cd(new Date().getTime(), time);
  160.       let rusers;
  161.       let embed = new RichEmbed()
  162.         .setColor("BLUE")
  163.         .setAuthor(`${args.slice(3).join(" ")}`)
  164.         .setDescription(
  165.           `React with 🎉 to enter!\nTime remaining: ${timespan}`
  166.         )
  167.         .setFooter(`Ends at`, client.user.avatarURL)
  168.         .setTimestamp(time);
  169.       let embed2 = new RichEmbed()
  170.         .setColor("RED")
  171.         .setAuthor(args.slice(3).join(" "))
  172.         .setFooter(`Ended at`);
  173.       let msg = await message.channel
  174.         .send(
  175.           `**🎉 GIVEAWAY 🎉**`,
  176.           embed
  177.         )
  178.         .catch(err => message.channel.send(`Error: \`${err}\``));
  179.       dbg.set(
  180.         `giveaway.${message.guild.id}.${message.channel.id}.${msg.id}.time`,
  181.         {
  182.           gtime: time,
  183.           gid: msg.id,
  184.           gtext: args.slice(3).join(" "),
  185.           gwin: args[2]
  186.         }
  187.       );
  188.       await msg.react("🎉");
  189.       timer = setInterval(() => {
  190.         if (
  191.           !msg ||
  192.           msg.content ==
  193.             `**🎉 GIVEAWAY ENDED 🎉**`
  194.         )
  195.           return;
  196.         let ttt = [-2, -3, -4, -5, -6, -7, -8, -9, -10];
  197.         if (ttt.includes(moment().diff(time, "seconds")))
  198.           return msg.edit(
  199.             `**🎉 GIVEAWAY 🎉**`,
  200.             embed
  201.               .setColor("#ffb800")
  202.               .setDescription(
  203.                 `**Last chance to enter!!!**\nReact with 🎉\nTime remaining: ${cd(
  204.                   new Date().getTime(),
  205.                   time
  206.                 )}`
  207.               )
  208.           );
  209.         msg.edit(
  210.           `**🎉 GIVEAWAY 🎉**`,
  211.           embed.setDescription(
  212.             `React with 🎉 to enter!\nTime remaining: ${cd(
  213.               new Date().getTime(),
  214.               time
  215.             )}`
  216.           )
  217.         );
  218.         rusers = msg.reactions
  219.           .filter(a => a.emoji.name == "🎉")
  220.           .map(reaction =>
  221.             reaction.users.filter(u => !u.bot).random(parseInt(args[2]))
  222.           )[0];
  223.         if (moment().isAfter(time)) {
  224.           msg.edit(
  225.             `** GIVEAWAY ENDED 🎉**`,
  226.             embed2
  227.               .setTimestamp()
  228.               .setDescription(`Winners:\n${rusers || "No winners"}`)
  229.           );
  230.           if (
  231.             msg.reactions
  232.               .filter(a => a.emoji.name == "🎉")
  233.               .map(reaction => reaction.count)[0] <= 1
  234.           ) {
  235.             return message.channel.send(``);
  236.           } else {
  237.             msg.channel.send(
  238.               `Congratulations ${rusers}! You won the **${args
  239.                 .slice(3)
  240.                 .join(" ")}**`
  241.             );
  242.           }
  243.           clearInterval(timer);
  244.           return;
  245.         }
  246.       }, 5000);
  247.     } else return undefined;
  248.   } else if (args[0] == `${prefix}groll`) {
  249.     if (
  250.       message.member.hasPermission("MANAGE_GUILD") ||
  251.       message.member.roles.find(r => r.name == "GIVEAWAYS")
  252.     ) {
  253.       if (!args[1])
  254.         return message.channel.send(
  255.           `**Usage:** **\`${prefix}groll [giveaway message id]\`**`
  256.         );
  257.       if (isNaN(args[1])) return message.channel.send(`Thats not a message ID`);
  258.       message.channel
  259.         .fetchMessage(args[1])
  260.         .then(async m => {
  261.           if (m.author.id != client.user.id)
  262.             return message.channel.send(`This is not a giveaway message.`);
  263.           if (!m.content.startsWith(`**🎉 GIVEAWAY**`))
  264.             return message.channel.send(`This is not a giveaway message.`);
  265.           if (
  266.             m.content !=
  267.             `**🎉 GIVEAWAY ENDED 🎉**`
  268.           )
  269.             return message.channel.send(`The giveaway is not ended.`);
  270.           if (m.reactions.size < 1)
  271.             return message.channel.send(
  272.               `I can't find reactions in this message.`
  273.            );
  274.          if (
  275.            m.reactions
  276.              .filter(a => a.emoji.name == "🎉")
  277.              .map(reaction => reaction.count)[0] <= 1
  278.          )
  279.            return message.channel.send(``);
  280.          m.reactions
  281.            .filter(a => a.emoji.name == "🎉")
  282.            .map(r =>
  283.              r.fetchUsers().then(async u => {
  284.                let rusers = u.filter(user => !user.bot).random();
  285.                await message.channel.send(`The new winner is: ${rusers}`);
  286.              })
  287.            );
  288.        })
  289.        .catch(err =>
  290.          message.channel.send(`I can't find this message in the channel.`)
  291.         );
  292.     } else return undefined;
  293.   } else if (args[0] == `${prefix}gend`) {
  294.     if (
  295.       message.member.hasPermission("MANAGE_GUILD") ||
  296.       message.member.roles.find(r => r.name == "GIVEAWAYS")
  297.     ) {
  298.       if (!args[1])
  299.         return message.channel.send(
  300.           `**Usage:** **\`${prefix}gend [giveaway message id]\`**`
  301.         );
  302.       if (isNaN(args[1])) return message.channel.send(`Thats not a message ID`);
  303.       message.channel
  304.         .fetchMessage(args[1])
  305.         .then(async m => {
  306.           if (m.author.id != client.user.id)
  307.             return message.channel.send(`This is not a giveaway message.`);
  308.           if (!m.content.startsWith(`**🎉 GIVEAWAY**`))
  309.             return message.channel.send(`This is not a giveaway message.`);
  310.           if (
  311.             m.content ==
  312.             `**🎉 GIVEAWAY ENDED 🎉**`
  313.           )
  314.             return message.channel.send(`The giveaway is ended.`);
  315.           if (m.reactions.size < 1)
  316.             return message.channel.send(
  317.               `I can't find reactions in this message.`
  318.            );
  319.          let gv = dbg.get(
  320.            `giveaway.${message.guild.id}.${message.channel.id}.${m.id}.time`
  321.          );
  322.          let rusers = m.reactions.map(r =>
  323.            r.users.filter(u => !u.bot).random(parseInt(gv.gwin))
  324.          );
  325.          let embed2 = new RichEmbed()
  326.            .setColor("RED")
  327.            .setAuthor(gv.gtext)
  328.            .setFooter(`Ended at`);
  329.          m.reactions
  330.            .filter(a => a.emoji.name == "🎉")
  331.            .map(r =>
  332.              r.fetchUsers().then(async u => {
  333.                let rusers = u
  334.                  .filter(user => !user.bot)
  335.                  .random(parseInt(gv.gwin));
  336.                m.edit(
  337.                  `**🎉 GIVEAWAY ENDED 🎉**`,
  338.                  embed2
  339.                    .setTimestamp()
  340.                    .setDescription(`Winners:\n${rusers || "No winners"}`)
  341.                );
  342.                if (
  343.                  m.reactions
  344.                    .filter(a => a.emoji.name == "🎉")
  345.                    .map(reaction => reaction.count)[0] <= 1
  346.                ) {
  347.                  return message.channel.send(`No winners :rolling_eyes:`);
  348.                } else {
  349.                  message.channel.send(
  350.                    `Congratulations ${rusers}! You won the **${gv.gtext}**`
  351.                  );
  352.                }
  353.                await dbg.delete(
  354.                  `giveaway.${message.guild.id}.${message.channel.id}.${m.id}.time`
  355.                );
  356.                return;
  357.              })
  358.            );
  359.        })
  360.        .catch(err =>
  361.          message.channel.send(`I can't find this message in the channel.`)
  362.         );
  363.     } else return undefined;
  364.   }
  365. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement