Multi-Guild Giveaway System | Discord.JS V13 Series | #13

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to leaks code in today's episode we're creating a complete multi-guide giveaway system with the options of re-rolling pausing unpausing ending or deleting and we're also going to go through two types of data storing the first type is json and the other is mongoose now i highly recommend using external database for this case mongos that we already created in episode how to set it up and connect it with your discord bot completely for free click the card up to the right of your screen if you want to watch it if you're going to use it instead of json now before we start coding i'm going to leave you with a little preview of what the finished product will be looking like so you can decide if you want to continue watching this video or not all right so before we start out we're using our own command handler i did an episode about it click the card up to the right of your screen if you want to watch it but if you have your own that's fine all right so we're gonna place our commands folder for our command file for the giveaway system inside of the systems folder we're going to call it giveaway dot js and now here we need to extract a couple of stuff from that this code.js package so we're going to type const open up an objects then require this for the js and then we can require the command interaction and the message embeds or extract them from the package because that's all we need and then now we're going to download a package called ms so type npm ims inside of our terminal once that is done we're gonna require ms we type const ms equals require ms all right so now we're gonna start with the command structure so we're going to have a command that you can choose to start or choose the giveaway options and so let's start with the model exports so model exports and we're going to give giveaway to be the name of the command and then here we're going to set the description a complete giveaway system for example and now here we can set the permission [Music] to be administrator so only admins can use that command and now here we're gonna start with our options so the first option is gonna be a a sub command so it's gonna be a start command so we gonna name it start and then the description is gonna be a start i give away for example this all of it are optional you can change whatever you want the type of it's going to be sub commands so type sub command and if you get this errors because you forgot a comma somewhere all right so we're going to put a comma here and create our options for the start command so we type options we're going to open array and the first option is going to be the duration of the giveaway so if you want to create a giveaway that will end in 10 hours or two hours or whatever so we're going to type duration as the name of it and then the description we're going to clarify how you can input the the duration so we're going to type provide a duration for this give away and here we're going to give an example so we're going to give 1 m equals 1 minutes 1 h equals to 1 hour and 1 d equals to 1 day all right so we're going to go down one more one more line and we're going to set the type of this command or option to be string and then obviously it needs to be required otherwise we cannot run a giveaway without a duration all right so now we're going to create another option inside of the sub command and we're going to call it winners so winner count for example so name winners and description here we're going to say select the amount of winners for this giveaway so how many winners can win this giveaway and then it's going to be an integer because it's going to be a number so three four five six it's gonna be integers so type in figure and it's going to be required simple and now we're going to go down one more line for another option and it's going to be the price so the price name so what are they going to win so name price description provide the name of the prize and for the type it's going to be string and it's also going to be required and now for our last option insider let's start is the channel so it's gonna be two types of how we shoot the channel so i'm gonna show you once we do the execute function so we're gonna type name it's gonna be channel description select a channel to send that give away to let me correct give away all right and then the top of it is going to be channel we forgot to put a comma over here and with the discord is a new function you can select what type of channel it is because previously if you selected the type to be channeled you can select also voice channels or categories so here we're going to type channel types and inside of an array we're going to type guild underscore text all right but it's this is not going to be required because we can have the option if we didn't mention a channel then the channel is going to be the same channel as we did the command in all right so um now for the actions uh because we can have actions for example pause unpause and and etc we're gonna create another sub command just like this one so we're gonna go down to here and we're gonna put a comma and here we're gonna create a new command basically our sub commands so we're gonna name it to be for example actions all right the description is gonna be uh options for giveaways for example we can alright so the type is gonna be sub command and here we can have our options inside of an array so because we're going to have many options we can use the the shows type of command so we we just select the shows and then we can execute on it because all of them require a message id so it's it's better to make choices instead of separate commands so um we're going to create a first option which is options so name it's going to be options all right so description is going to be select an option that type of it is going to be string because we're going to be using the choices and it's also going to be required and don't forget your comments like i always do and here we're going to have the choices inside of generators choices we're going to create an array so the first choice is going to be end and the value of it is going to be also end so now let's put the comma at the end this line and paste this a couple more times so let's paste it for example this many times so the second option is going to be pose you can create or you can arrange them as much as you want and the third option is going to be on pause the value impose and and then we're going to have re-roll and the value is going to be reroll and last but not least we're going to have delete and the value is going to be delete it's pretty simple so far this is going to be your like giveaway command structure so so far so good and we're done with the options now so we can go down to this end of the array and we can put a comma and here we can execute and what we're gonna execute we're gonna execute the interaction and the client because we're gonna use a client property that we're going to create for the manager so here we can we can grab or grab our parameters for sorry that's an excellent for the client and the interaction so command interaction for the interaction and then client with the capital c for the client and it should also extract it from the discord.js package all right so so far we so good we're going to create a code block and then here we're going to grab the options property from the interaction because we're gonna use the options a lot so instead of typing interaction.options we can only type options so const options equal interaction so here we grab the option property from the interaction okay because we have now two sub commands we need to know which sub command that the user um shows so we're gonna type const sub equal to interaction dot get sub command let's see sorry options we're gonna tap options get sub command because you already extracted the options properties sub command and that's it so uh whenever the commands get executed um the the get command option is gonna either return with actions or start and how do we know and how do we act on them we're gonna create a switch statement but before we do that we're gonna create two embed templates so one for errors and one for successful uh commands so we're gonna type cons add or embed for example a new message embed and it's gonna be resembled by the color red and then we're gonna create another embed so success embed equals to new message embed so we don't have to keep continuing creating him with our time so that color is green to resemble that it's a success so now so far so good we created our templates so here we're gonna create um a switch statement so we're going to type switch and sub we're going to provide the sub because this is this is on what we're listening to the returns of the sub all right and now here we're going to create cases so case start we're gonna execute whatever inside of this command looks so if the case if it returned start then we can execute this whatever is inside of this code book and then we're gonna go down here and break but it also can return actions because it's another sub command so we're gonna create another case we're gonna type case actions and here we're gonna execute whatever inside of this code block and then break alright so now because if the case was actions we have a lot of choices inside of the actions so we want to know what is the shows that the user shows and whatever shows it might be we gonna basically get the uh get the shows and execute the code based on the shows so to do that we're gonna go down here to the actions and here we can grab the shows so what whatever the user show shows so to do that we're going to type const choice is equal to options dot get string sorry not sub command get string and we're gonna get it from the options string or options option so let's press the options here and now we know uh or that's how we get uh what whatever option or shows that the user selected so here we're going to create another switch statement so we're going to type switch all right and here we're going to switch between the choices so choice and now we're gonna it's the same concept so we're gonna basically case uh whatever because we have a lot of cases and pause and pause real and delete so case and for example we're gonna execute this code then we're gonna break and remember this has to be the two dots not the column all right and now we can copy paste this a couple of times so we don't have to write or spend time writing unnecessary stuff so here we're gonna type post case unpause case re-roll and case delete pretty simple so what we're doing here is if if the case is actions then we're gonna switch the cases between choices and see whatever use the user shows and act opponents and now here we can set the default uh or actually let's set the default outside of it let's set the default here so default is going to be for example let's say console.log and error and give away command all right so if somehow something breaks we're gonna just console.log this for now we're gonna change it later on all right so now that we're done with our sub command structure we're gonna need to download a package that it's called um discord giveaways i'm gonna leave the link for its documentation down in the description so let's go to the github page of it so to download it you basically copied copy this paste it in your terminal so mpmi discord giveaways and it will download and then when we run it for the first time it's going to create a new file and a json file which is the default database for this but we can change it later on to be a mongoose all right so now that we have done that um we need to create um a property for the client so if you look at at the discord or their github page you can see here that they created a giveaway or a client property but they did it inside of their index.js file which we're not going to be doing all right so because we are going to be using mongoose as our database uh this is going to be a lot of changes that we need to do so what we need to do first is create a schema for that to store the data and app to be able to access it so if you scroll down to almost the end of this um github repository you can see here mongodb and you can click on mongoose and here it will show you an example on how to create your mongo's uh schema so like we see here we have the conscious schema it was a new schema and then down here we create they created a model and then uh basically um extended that model to be the giveaway manager or to be inside of the giveaway manager like you see here giveaway manager with own database so we're going to do the exact same thing but a little bit different so if we gonna we're gonna go to our structures folder to our scanners folder if you haven't created this one you can create it um so you're gonna create a new file and here we're gonna call it giveaway dp.js all right and here we're gonna download or import or require the mongo's package so we're going to have const mongoose equals to require mongos all right so now we're going to go back to the website and as we see here we're going to copy from line 25 where they created the schema all right to line 80 where they created the the giveaway model so let's copy that go down a couple of miles here and paste it all right so let's delete this comment and now we have our schema uh finished basically this is the scammer which is gonna appear in your uh in your database all right so once that is done we're gonna go down a couple more lines and we're gonna model export the giveaway model so here we're going to have model dot export and we can export the giveaway model module basically variable right so now that we have model exported we can use it in other files and our schema has been successfully created all right so let's close out of this one and now we're gonna create a new file and we're gonna our folder and we're gonna call it systems inside of our main category so let's create a new folder and call it systems and in it we're gonna type or write giveaway yes or give away this yes you can name it whatever you want to bonus all right so in here we're going to require the package that we downloaded which is this will give away so here we go type or actually let's go to the github repository here and from 82 you can copy this line over here down to 125 all right let's go ahead and paste it here all right so here is your system um we can remove all or if you want to keep the comments to reads basically this function is called when the manager needs to get all the giveaways or which are stored in database you can keep them if you want to read them but that will make the tutorial a bit too large if you're going to explain everything that's happening all right now that we have removed all of them and we're gonna require the uh what's called the giveaway model that we exported in in our schemas this one so to do that we're gonna go down to the system or we're gonna go to the system and we're gonna type const and let's copy the variable name again equal and here we're gonna type wherever we stored that variable so dot dot or i think dot slash let's do that again dot slash no dot dot slash all right uh and it's gonna be inside of our structures and then slash schemas and slash giveaway dp and that's where we have exported or imported the giveaway model so these are defined now all right so to be able to run this system whenever we start our bots we need to model export it and we need to run it from our main folder so let's model export real quick so what we're gonna do is we're gonna model i don't know why my um my vs code is being so now we're gonna export the client and then let's create an error function here all right and we're gonna copy all that and place it here all right so um like you see here client.giveawaymanager equals manager so we created a new property to our client so now if you go to main all right let's go down one more line here and here we're gonna type or actually let's do it right here here we're gonna type require and we can acquire dot dot slash systems slash giveaway system and we're gonna pass that client to it all right and don't forget your cma semi columns guys these are important if you have an open array here that is not defined all right so once that is done we basically created our system instead of our folder uh that's called system we ran it whenever the bot started inside of our main.js or index.js whatever it might be and we created our schema so we're basically done with storing with with an external database so now we can continue on with our command so it can get finished so let's close out of all of these for now and we will start with the start command so if the command or if whatever sub command shows is stored then we gonna start with it we gonna start a giveaway all right so we're gonna go to the switch sub start let's open it up a little bit and here we're gonna grab a couple of um what's called a couple of uh objects so we're gonna type const for example g channel which means giveaway channels is equal to options dot get channel and we're gonna get it from the channel option where is it here the channel option all right let's go up here channel or we're gonna make it the interaction those channel because we didn't make this required so we're gonna basically if there isn't a channel if this doesn't return a channel then we're gonna take the interaction the channel as the giveaway channel then here we're gonna define our the racing so cons duration equals to options dot get uh what was it a thing for string string all right and it's named duration let's double check always double check guys so you don't get any errors so duration then we're gonna go down one more line and here we're gonna type const winner count equals to options dot get uh in figure i think we selected to be so let's go up here and shake so winners integer yeah just like this and then we're going to get the price so comes price equals to options dot get string string and we're gonna get it from the prize sorry prize and let's double check if we named it prize all right so here we go we grab the channel object the duration which is the time the winner count at the price now here we're gonna uh fill up the property so we're gonna type client dot and here we're gonna use the property that we created so giveaway and give a waze manager i think it was i'm pretty sure and here we're going to have don't start all right and here we're going to pass in the g channel as the channel that we're going uh deploying the giveaway to and then we can open a command uh open a block of code and now here to uh to understand it a little bit better we're gonna go we're gonna go to their website right in here and like you see here um where is it to start a giveaway all right so here we they grab the object that we just grabbed and here we can we can see client.start and let's copy this actually all right let's go for this and paste it here so like you see here we're going to change the interaction channel to be obviously the g channel all right just like this and here we have the duration duration winner count winner count prize price now it's gonna um everything is else is defaulted like you see in this picture this is how it's gonna look not that at every one but this is how the embed will look but if you wanna change the the the text that it's displaying you can use the message property which they see here if you click on this link it's going to take you to the start options and here we can edit all of this sort options um so for example let's edit the message let's find the message property so here give away messages so this is the default messages but we're going to change them when change the text so we're going to type messages we're going to open up a code block and here we're going to type give away because it's named giveaway here type string and this is the default we're going to change it so we're going to type for example let's add an emoji this one for example make it bold give away started and add the emoji again and the defaulted text which is this one is going to get changed the same thing with the giveaway ended so let's copy that go down one more line and here we can type sorry we can type or add an emoji and type give away ended all right let's make it bold [Music] all right and add our last emoji and that way the giveaway embed will get changed and then you have the win message so where is it the win message congratulations winners you won this prize the fact that i don't like with this one is its link it links the message url which i don't think is uh looks good so i'm gonna remove that so instead i'm gonna type let's go downline let's copy the property name so win message and here i'm gonna type let's copy this all right and i'm just gonna sorry i'm just gonna remove this and that's it and leave the winners inside of this brackets because they're going to get automatically changed to be the actual winners all right so now that it's done so here it says doden g data and it's going to console.log the g data we don't need that because it's just gonna load a complete big object of data we don't need it at all so what we're gonna do is we're gonna async all right so we can async and remove the g data and here we're gonna take the success embeds right and type a dot set color i mean said description um give away was su says fully started is that how youtube successful let's ask google yeah all right so uh let's go down here and here we're gonna type interaction dot reply and we're gonna reply with the success embed inside of an array and then if whenever happens so we're gonna catch because we async all right so don't catch all right and we're gonna catch the air [Music] and here we're gonna take these copy them and change the success embed to um what was it error embed i hope you guys are with me on this so you don't get confused and if you get if you need any help just join our discord and we'll gladly help you so error embeds the description and here we're gonna type let's make it with the the backtick so we can put our error object so here we're going to type an error has occurred and we're going to create a new line and pass in the error here and this should be it all right so if the the giveaway started successfully then giveaway started and if they enter or if they given errors then we're going to send an error embed saying that an error has occurred and this is going to be the error so we are basically done with our start command so let's go now to the big part which is the actions so let's go down here so what um if you go to their website again let's where is the website here so you see attention the command examples below reroll edit delete and can be executed on any server your vote is a member of if a person has the price or the message id of the giveaway so basically let's say i create a giveaway in my discord and somebody creates another giveaway in the discord and has the exact same same system then they can copy the message id of my giveaway and end it by using a command on their discord so that's bad so we're going to create something preventive to that so it's pretty simple like you say here to prevent it so constant giveaway here you can search with the uf with the price or the message id message id is much more saver so we're going to use that and then here we can we can copy this line over here by the way the website and everything is going to be linked down in the description let's paste it here for now so remove this and remove this and put it here all right so here we're going to make a a simple change so um so what we're gonna do is let's see actually we forgot one thing because we need a message id so uh under this sub command action all right under this options array right here we're going to create another option so it's going to be called let's see do i do anything oh sorry all right inside of here guys it's out of here yeah we're going to create another option uh am i doing something wrong oh yeah sorry it's out of this bracket right outside of it we're going to create a new option so name is going to be channel all right um and it's going to be description or actually message underscore id or whatever you want to call it and the description is going to be provide the message id of the give away all right and then the type is going to be string and it's going to be required set to true and that should be it all right so now we have another option to take from so let's copy this name over here and grab it so um wherever we are here so we're gonna type const message id equals options.get string we're gonna get it from this option all right so and now here we can take this message id and um remove this query and instead of the query we can add this to add or equals and it's going to be our message id so it's checking the guide guide is equal to the interaction of ld and the g message is equal to them is that three giveaway message so it's pretty simple i know it's a lot guys so we're gonna go down here and we're gonna change it a little bit so we're gonna create a block and remove this and here we're gonna type um error embed so error embed.set description and we're gonna tap unable to find give the give away with the message id and here we can pass in the message id and this guide all right then we're going to go down here and we're going to type interaction reply embeds and here we're going to pass in the error embed so pretty simple and straightforward so far and actually um let's make it a thermal so let's firmly set it through let's do that with all of the embeds that we sent because they're unnecessary for everybody to see them so let's do that real quick and this should be it alright so now that we have done this preventive method we can go down here to the end to start with the ends and we can go to their um to their documentation again and end the giveaway so we can copy this lines over here so let's copy them all right paste it here let's fix it all right and if you want to type this manually then go ahead too but it's unnecessary when you have the code written here all right so message id we don't we don't need this because we already got it here so and and here we're going to change some stuff so instead of this is going to be success embed.set description give away has been ended all right and then we're gonna interaction.reply embeds and here we're gonna provide the success embed then we're gonna make the thermal set it through all right here and instead of this is gonna be also uh where is it where is it like this one so let's copy this and paste it here fix it so if an error happens we're going to say an error has occurred then we're going to send the error embed thermal true so it's a pretty simple concept guys now this is our template that we're gonna use for everything so we can copy this all right and now we're gonna go down to the pose and instead of end here we're gonna type pose and that's and we can change the embed the description so it has been paused and the same thing let's go down to unpause so we're gonna tap on pause here instead of on pause and unpause here instead of pause same thing with the reroll here we're gonna have re roll i think it was reroll let's check it again and let's see let's see we roll yeah let's reroll all right the message id the same one um and then for the delete we're gonna go down here to delete and here we're gonna type delete and we can change it to deleted and here we can change it to reload i think this should be it if i'm not mistaken and if we see any errors any errors at all we gonna fix them in the video an important thing what you need to do is type return so return before every interaction reply that you do so let's return on everything let's see here return and also here very important return and the giveaway return return return here too and here too [Music] same here and same here let's put the space also here sorry uh see just like this so put return on every before every interaction reply and you should be good to go and this is very important so you have to do it so uh yeah so let's start the boats and let's hope it doesn't crash on us so node space dot let's see our giveaway command loaded everything and now if we go to our robo t um let's update the connection refresh we should see list code collections giveaways so it created a collection so that's pretty good so we have the giveaway system connected to our database now let's go to the discord all right let's try it out so now let's say we start a giveaway so we'll type slash giveaway start duration let's say five minutes winner a one winner prize for example hello all right press enter like you see here we got our successful embed we can dismiss it and now here like we see here it's started and now if we go to our database to our giveaway collection now we can see the data here and especially ended false that means even if we restart or stop our boat it's still gonna do the giveaway whenever the boat starts up again all right so now to prove that it works in multiple guides i'm gonna go to another guide already tested it so let's say type slash giveaway and and here we gonna have one minutes winners one prize one and like we see here we created another giveaway and i can react here and i can react here or wherever and no errors in the console now let's say i try to end this giveaway from the other server so let's copy the id of the message let's go here type slash giveaway actions let's see end let's id like you see here unable to find the giveaway with the message id this in this guide no it's in the console and everywhere give over here didn't end yet so um all right so now that the multi guide is working let's try out our other commands so slash giveaway actions uh pause and he will paste the message id like you see here the embed is post now let's unpause it so give away actions um on pause message id and like you see it's on pause now if you wanna uh let's see actions if you wanna um end it all right you can type this and it will end and i won now let's try to delete it so giveaway action delete message id and like you see here it has been deleted nodes in the console and let's refresh our database and like you see here ended true and yeah everything is good so um let's go ahead i won on this too and um yeah this is it basically guys um i hope you enjoyed the video and i hope to see you in the next one
Info
Channel: Lyxcode
Views: 1,908
Rating: undefined out of 5
Keywords:
Id: a6_-BRHe7qk
Channel Id: undefined
Length: 38min 0sec (2280 seconds)
Published: Sat Nov 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.