Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const discord = require ( "discord.js" ) ;
- module. exports . run = async ( bot , message , args ) => {
- // Arguments that we need later.
- var item = "" ;
- var time ;
- var winnerCount ;
- // Check if you have perms to do this command.
- if ( ! message. member . hasPermission ( "MANAGE_MESSAGES" ) ) return message. channel . send ( "Sorry you can't do this" ) ;
- //! giveaway numberWinners second itemTo Win.
- // Request the number of winners.
- winnerCount = args [ 0 ] ;
- // Time how long it must last.
- time = args [ 1 ] ;
- // What prize you can win.
- item = args. splice ( 2 , args. length ) . join ( "" ) ;
- // Delete the message that has just been created by the user.
- message. delete ( ) ;
- // Calculate expiration date.
- var date = new Date ( ) . getTime ( ) ;
- var dateTime = new Date ( date + ( time * 1000 ) ) ;
- // Create embed.
- var giveawayEmbed = new discord. RichEmbed ( )
- . setTitle ( "🎉 ** GIVEAWAY ** 🎉" )
- . setFooter ( ` Expires : $ { dateTime } ` )
- . setDescription ( item ) ;
- // Send embed and put the response on the popper.
- var embedSend = await message. channel . send ( giveawayEmbed ) ;
- embedSend. react ( "🎉" ) ;
- // Set a timeout that goes off after the number of seconds.
- setTimeout ( function ( ) {
- // Arguments that we need.
- var random = 0 ;
- var winners = [ ] ;
- var inList = false ;
- // Get the users who have responded to the giveaway.
- var peopleReacted = embedSend. reactions . get ( "🎉" ) . users . array ( ) ;
- // Here we are going to go over all the people and see if the bone is in between
- // We must remove the bot from the list and then we continue.
- for ( var i = 0 ; i <people Reacted. length ; i ++ ) {
- if ( peopleReacted [ i ] . id == bot. user . id ) {
- people Reacted. splice ( i , 1 ) ;
- continuous ;
- }
- }
- // Here we check if someone has participated.
- if ( peopleReacted. length == 0 ) {
- return message. channel . send ( "No one has won so the bot wins." ) ;
- }
- // We will temporarily check if too many people have participated in the competition.
- if ( peopleReacted. length < winnerCount ) {
- return message. channel . send ( "There are too few people who participated, that's why the bot won." ) ;
- }
- // We will create a random number for the number of winners that we have previously entered and place the user in an array.
- for ( var i = 0 ; i < winnerCount ; i ++ ) {
- inList = false ;
- // Creating a random number so that we can choose a user.
- random = Math . floor ( Math . random ( ) * people Reacted. length ) ;
- // If a winner is already in the winners list then we have to search for another winner again.
- for ( var y = 0 ; y < winners. length ; y ++ ) {
- // Check if the selected winner is already in the list.
- if ( winners [ y ] == peopleReacted [ random ] ) {
- // We put i 1 less so that we can continue in the list.
- i -;
- // We set this to true so that we know it is already in the list.
- inList = true ;
- break ;
- }
- }
- // If this is not in the list, we will add it.
- if ( ! inList ) {
- winners. push ( peopleReacted [ random ] ) ;
- }
- }
- // We will send a message for every winner.
- for ( var i = 0 ; i < winners. length ; i ++ ) {
- message. channel . send ( "Congratulations" + winners [ i ] + ` ! You have won ** $ { item } ** .` ) ;
- }
- } , 1000 * time ) ;
- }
- module. exports . help = {
- name : "giveaway" ,
- description : "Start a giveaway"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement