How To Make A Discord Bot - Cooldowns (2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back to another discord.js tutorial today we're going to be doing command cooldowns now before we get started i do want to give my discord server a quick shout out this is a discord server for the discord.js coding community so you guys want to code your bot and are having trouble go ahead and join this server go all the way down here in the coding section and there are some help sections here as well as a new channel called command showcase which are the code helper role the people that actually have the role for the code helper are able to basically send some creative commands here and this is all the code that you can freely use as much as you want so we have something like a weather command we have an embed builder we have avatar reload so these are like free commands code that you guys can steal come here join and yeah let's go ahead and get started so for cooldowns all we're going to need is actually the message events.js as well as a command so we can test this on and how we're gonna do this is actually with maps if you guys don't know what maps are i'm gonna leave two links in the description the first one is gonna be an mdn web docs uh description of maps and also i'm gonna leave the discord collections guide if you guys wanna learn about discord collections which is basically just a native map with more features to it but we're gonna be using both of these in this tutorial i'm not gonna explain what they do but go ahead and learn from these websites all right so the first thing to do up here above the module exports is actually create a new map and this is going to hold all of our cooldowns so const cooldowns equals to new map and how this map is going to work i'm just going to explain quickly so i have to explain while i'm actually writing the code this is going to hold the key and a value and the key is actually going to be the command name so whatever name of the command so something like mcu server that we're going to be using for this tutorial and this is actually going to be a new discord collection here new discord collection and this new map here is going to hold the timestamp as well as the cool down amount or the cool down time so that is how we're going to be doing this so once we created this map come all the way down here i do want to talk about this in a bit but i restructured my code a little bit from the last time i did this message event so i just did a try and catch usually before i had if the command does not exist then do this but i restructured it to do a try and catch so we're going to try to execute the actual command if not we're going to send a reply saying there's an error trying to execute this you know command and then we'll actually console console.log in the air um so you don't have to have this but i just did it for a little more secure error checking so here what we're going to do is create an if statement to check if this cooldown right here already has the command name as one of the keys so if it doesn't so cooldowns if it doesn't have it that has command that name what we want to do is basically add that command name as a key so cooldowns dot set we're going to set a command that name and then we're also going to set that new disk collection so new discord that collection and then we are going to be done with this if statement the next thing we're going to do is get the current time which is the time that the certain you know the whoever user send it that current time and there's actually a way to do this with javascript it's just date dot now so const current time is equal to date that now following this we're going to create timestamps const timestamps is equal to that cooldowns.getcommand.name and then we're going to create the actual cool down amount how long it's going to be on cool down that command so cons cool down um let's do mounts equals command that cool down and then we're going to do is actually multiply it by 1000 so we get milliseconds and actually we should actually test this by adding the cooldown we'll not test it but show you guys what this is so we're going to do something called command.cooldown so when you want to have a cooldown on a command simply come to the command and then right below the aliases or the name we simply just want to add another key called cool down and then we wanna do the cooldowns in seconds so let's say this command is gonna have five actually ten seconds of cooldown and that's how simple you have to do in every single command file it's just add this key here for cool down and then the seconds again if you're gonna do a whole minute you do 60 seconds i'm just gonna do 10 seconds for this command and that is how we're getting this cool down amount and now we're multiplying it by a thousand so we get the milliseconds all right so now we're going to check that if the time step actually has the message that author that id right now this code won't work um because we're only checking for something later on we're going to be adding that check so the timestamps then we're going to message that author that id and then oh that's what i want to do open this if statement up i'm gonna do cons expiration time which is just gonna be when we're actually able to use that command equals to timestamps dot get message that author that id and we are going to add cool down time cooldown uh amount sorry one more if statement if current time is less than the expiration time we want to simply const time left check the actual time left of this which is a simple expression which is expiration time minus current time and then we are going to divide it by a thousand and then return the actual matches message saying hey this is how much you have left for your command so we're gonna do a message.reply we are going to then do a backtick so we can actually use a template literal then do please wait money sign brackets time left so time underscore left to fix and then we're going to fix it to the first one so we get the first decimal point of the time and then more seconds before using and then we can do something like command name so command name so we're going to open this up with the money sign brackets and then do command that name and then that should actually be it all the way at the end of the if statement we actually want to add the off the message.author.id and the current time to this time stamps right here so let's do timestamps that's set message that author that id and then we also want to do current time so that is it let me just explain a little bit of what's going on we're creating a map here for the cool nouns we're checking if that map has a command that name as a key if it doesn't we're going to add that key and also create a new discord.collection we're getting the current time that the member sent the command then we're creating a timestamp which is equal to the cooldown um cooldowns that gets the command name which is actually just getting this from up here and then we are doing the cooldown amount so this is getting it from the command file then if the timestamps actually has the message.author that id which we actually add all the way down here then what it's actually forgot to add something out here but i'll just do it after i explained and then we are going to get the expiration time which is actually when they're able to use the command which is just getting the timestamps and then just adding the cooldown amounts and then if the current time is less than the expiration time we want to check the time left by doing this calculation and then we want to send this to the user saying you know this is how long you have until you can use the command and then that's it forgot to say we do want to do a set timeout down here and we want to do this because we want to get rid of everything once the time is over so set timeout it's going to be timestamps delete message that author that id and for the amount of time which is just the cool down time so after the cooldown is over make sure you delete it um so we're not stuck in that little um cooldown so let's go ahead and save everything no space dot go into our discord server we did the cooldown for the mc server command which has an alias of mc it's 10 seconds so what happened here there was an error timestamps is not a function did i do this wrong sorry yes that has and because of this mistake i'm gonna have the code down below in the description it's a function supposedly that has we actually check that there is a message that our third id has a key here um so sorry about that let's go ahead and run that one more time make sure you check the description for that code if i messed you guys up emcee all right ten seconds we have seven seconds left six seconds left five seconds left three seconds let's just wait three seconds and let's see it should be over now there we go also as easy as this to actually add time let's do two minutes clear now space that and come in here i can only use this commands or this command after two seconds and you guys can go ahead and convert this into minutes and hours you can also add permissions but i'm gonna let you guys do that so i don't want this video to get that complicated so that's actually pretty much it it's just this code right here and then adding the map up here and then of course adding the key in the module exports and you should have a cooldown for your command with that being said i hope you guys enjoyed this video if you did make sure to subscribe i do want to give a quick shout out to my patreons the patreons that you're currently seeing in the screen right now if you guys want to support me in patreon there's going to be a link in the description of that as well as well as a link in description to my discord server and yeah guys i will see you guys later on my next video peace
Info
Channel: CodeLyon
Views: 23,509
Rating: undefined out of 5
Keywords: how to make a discord bot, how to make a discord bot 2020, how to make a discord bot online, how to make a discord bot online 24/7, how to make a discord bot visual studio code, make a discord bot, how to make a cool discord bot, how to code a discord bot, how to create a discord bot, discord bot tutorial, code discord bot, code your own discord bot, make your own discord bot, discord cooldown command, discord js cooldown, discord js cooldown command, discord.js v12 cooldown
Id: WGTZgZVVclw
Channel Id: undefined
Length: 10min 41sec (641 seconds)
Published: Sat Feb 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.