Basic Command Handler | Discord.js v12 tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to another video today in this video we're going to be setting up a command handler to make everything a lot more easier to read now here i deleted the hello hi command because we're not going to use it in this video so basically what this is gonna do is it's just gonna clean up our workspace a little bit more instead of having a huge advanced code here later on in the future and it'll just be in a file and make sure to watch the video in the top right that's about to show up right now in order to not be confused so let's get into this so first we need to do is go underneath config and do const fs equals require fs fs is already built into discord.js we just gotta define it underneath cons fs we need to do client dot commands equals new discord dot collection now i've been seeing a ton of people who has been having problems with their code and most of the issue was khan's client equals new digital dot client they keep forgetting to capitalize the c so make sure the c and d is capitalized in this line right here now after that you want to go down a few lines you want to do const capital f for file equals fs.readersync capital s you want to parentheses right there inside the parentheses you want to do dot slash commands now what this is basically saying is that we're going to come over here the code is going to come over here and it's not going to find the file so it's going to send an error currently so what we need to do is we want to make a new folder inside the bot name commands so now what it's gonna do is it's gonna codes gonna come over here try to find this file which it finds it and then it's good not gonna send any errors now what we want to do is every single file in here if it's going to be a code it's going to end with dot js so we need to make it where it only reads the dot js the files that end with js files so what we need to do is do dot filter after dot slash commands out of the parentheses and then you want to do parentheses again undo file arrow function file then you want to do dot ends with parentheses dot js now that is just saying that if it ends with js like i said earlier it's only going to rate those now i want to go underneath it and you want to do before cons parentheses const file of capital f you want to do brackets and then after that you want to do const command equals require parentheses dot slash commands now this is going to go to the commands folder and get all the commands now this does not need to be the normal quotation marks it needs to be e back tick so right next to the number one you want to go to the left of it and press that and back ticks then you want to do slash dollar sign brackets capital five just normal file now after that what you need to do is you need to go down one or two lines doesn't matter one do client dot commands dot set command the activation then you want to comma command now after that we're done with right here what we need to do is we need to come down here to prefix one do let's activate equals client dot commands now the difference between constantly cons for instance up here const runs through the entire file let only runs through the paragraph that it's in so that's the difference so now what we want to do is we need to come down to our if command equals equals ping we want to do activate so this right here dot get then we're just going to leave that empty for now then we want to do execute then one do parentheses message and args so after that we're good for there right now inside our commands file folder we want to do a new file named ping.js now in here what we want to do is we need to export this so we need to do module.export equals brackets if i can even probably do it then up here we're going to do activation and then we want to do the two dots and then we want to do pink then comma then we want to do description then we want to do the two bullet points again we want to name it this is a ping command you can put whatever you want here it doesn't matter after that you want to put a comma then you want to run do execute then you want the parentheses then you want to message and args then brackets then you want right here this is very simple you can either do message dot channel dot send and pong or you can do the one that's just message dot reply pong it doesn't matter we're just going to do the channel message.channel.com now that should be it so now we're going to run our bots now the bot is online we're going to go to our testing server we're going to run ping now we got an error the error is dot execute so what we need to do is we forgot to put our command the file right here so we need to do dot ping we need to ping right there that's it then save the file and then that's it then we need to run our bot now we just come over here and do exclamation point ping and then there it is now the reason why we're doing this right now is later on in the future we're going to be getting in more advanced commands like per server commands and all that so it's going to be really helpful when we have this now if you want to make another command all you gotta do is come down here we're gonna make another command called hi like the previous video so we're gonna do else if command one two equals high to exclamation to equal signs brackets then you want to do activate dot get hi we'll make the file later then you want to execute then you want to just message in arcs then we come we can save our index we're done with that one come up to commands and you want to do high.js now we can just simply copy this and then we want to change this to height then we want to say this is a high command then right here we're just going to do message dot reply hello now that's all you gotta do to make a new command it's very simple you just gotta copy this and then put in your code right there now you want to run your bot again we're going to do hi then replies hello now that's the end of the video make sure to leave a thumbs up and subscribe to see more content like this and yeah so i'll see you guys next time bye
Info
Channel: CodeViper
Views: 20
Rating: undefined out of 5
Keywords:
Id: oYt0QKcdtHc
Channel Id: undefined
Length: 8min 44sec (524 seconds)
Published: Mon Jun 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.