Code Your Own Discord Bot - Event Handler & Command Handler V2 (2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys welcome back to another discord.js tutorial today since it's 2021 i want to kind of redo the command handler and also do an event handler this has been a very much requested video so let's go ahead and get started however before we truly get started i got a present a gift that i want to talk to you guys about all right guys so benq was actually very nice to send me one of their screen bar plus lamps if you're like me and you like coating a nighttime this lamp is going to help you a ton it's actually a lamp that you don't have to necessarily put in your desk it's actually on top of your monitor this lamp will completely illuminate your entire desk without having a reflection on your monitor if you actually see the pictures that i took there's no reflection on the monitor at all however the lamp will lit your whole entire workspace once again without having the need of having an additional lamp somewhere i'm telling you guys as a quarter myself and all of the soon-to-be coders are watching this youtube video make sure to invest on a good light source and the screen bar plus is the best option out there if you guys want to go ahead and purchase one of these amazing lamps there's going to be a link in the description go ahead purchase the lamp let's go ahead and head straight into the video so with that being said let's go ahead and get started with the event handler and the command handler v2 this is right here our current command handler this right here where we use a discord collection and then we kind of loop through every file and find the file that ends with js and then basically handle all the command files through this but you want to have a separate folder for the command handler we also want to have a separate folder for the event handler and handle all of the different events like this on separate files so it's a little bit easier to read and to handle your project so to do this i'm actually going to go ahead and delete all of this but i'm just going to comment them out usually you want to delete it but just in case i need some reference to this i'm just going to comment them out and actually i'm going to comment this out as well you still need the client that commands equals new discord collection and actually we're going to create a new collection called client.events uh it's equal to a new discord dot collection and this is how we are creating our discord collections so how we're going to structure this is in here in our project we're going to add a new folder and we're going to call this handlers inside the handlers we're going to have two files our first file is going to be the command handler.s and the second file is going to be the event handler so event underscore handler.js so these files are what's going to handle over events and all of our commands next in here what we're going to do is go ahead and create like a list and inside this list we are going to put the name of these files right here so command underscore handler then a comma and then add the events underscore handler and for each of these files we want to loop through them and basically require them into this file so handler um so for each handler and then here we're gonna do an arrow function and we're just gonna do require open this up with a backtick and then dot handlers and then money sign brackets handler this is this exact variable that we created here and finally we're just going to pass in clients as well as discord now the reason we're passing into discord is because some of the commands might not might need discord for example like embeds so we're passing discord just in case of that all right so that's actually it for main.js and if you actually go ahead and delete all this you're gonna see that our main.js is very clean it's only like 16 pages but there's some spaces here so it could be like 14 lines not pages 14 lines for our main file which looks very nice let's go ahead and head to our command handler and the first thing to do is require fs if you don't have fs go ahead and open up your terminal and do npm install fs just like that and that'll install fs you should have this installed if you've been following the tutorials and we're just going to simply require require fs just like that and we're going to do the typical module that exports and we're going to work in our module.exports for this entire file so we're going to get that client we're also going to get that discord that we passed in through um this right here and we're going to do that with an arrow function and here we are going to create a variable that is going to loop through every file of our commands folder that we have here if you don't have a commands folder you could add that as well you should add that but we already had this from previous tutorials so there it is just keep all the commands in the commands folder and we're gonna do cons we're gonna name this command underscore files equals fs that read their sync and in here we're going to just go ahead and go into our commands folder and we're going to filter every file that we see in that commands folder to find the ones that are a javascript file so file that ends with and then dot js so if you accidentally put like an image inside of this commands folder then it's not going to go ahead and grab that file then we're going to add that for loop and we're going to do const file actually if you go ahead and go back into your main file you see that we basically had this before but we're adding this into a new file which is much safer and definitely um better than having everything in the main.js so for each file of of command underscore files fonts command is called require and in here we're going to do a backtick two dots and go into the commands folder do money sign brackets file and let's just do an if statement so if command the name if the command has a name which is if we actually go into a command this name right here if command and lame then we're gonna do client that commands that set command that name command just like that and else we're just going to continue so this is already our command handler uh it's basically this we just added some other new features to it um because instead of doing this whole entire if else thing down here in the client.on message that we used to have we're kind of doing that here inside of a command handler however if you actually see this itself how you do messages with discord.js it's an event it's the event message so let's work on the event handler and it's pretty much the same we're actually going to go ahead and copy require fs you have to require fs to modules that exports we're going to pretty much copy everything that within the command handler with a little bit of different things that we have to add so passing discord passing clients do an arrow function and here const events underscore files actually no first we actually want to create since we have a different folders let's actually go ahead and create the folder first i'm kind of speeding a little bit so we're going to create the folder events this is going to hold over events make sure that you are in your main folder not inside a subfolder and inside the events there's actually two type of events we have the client events which is like the client already so when your bot is on that is a client event and then we have the guild events like the messages and everything happens with the guild aka your discord server so we're gonna create two more folders first one's gonna be guild and second one is gonna be clients um let me actually do this make sure they're all inside of the events folder not inside each other so it's clients and why does this look like this i do not like this let me figure out why it's looking like this all right so i figured it out it was actually putting the client folder inside of the guild folder for some reason so make sure you don't do that there's two folders inside of the events don't put them you know inside each other so for our event handler actually i keep messing up this is a file i don't want a file i want a folder so give me a folder called client there we go so again back to our event handler we want to create a function that's going to load every directory because right now we are going to load the events we want to also load client and guild so to do this we're going to do const load uh the directory so there and we're going to create a variable called der and we're going to do another function open this up in here uh const events underscore files equals fs that read their sync read their sync and we're gonna do the pretty much exactly the same thing we had in the command handler which is open this up with a single quotation marks dot events and actually make sure this is a backtick i always forget you have to put a backtick so backtick events and money side brackets and passing those directories uh make sure this is there's not there then filter and we're going to filter everything to get the js files just in case you mess up and put something like an image you don't want to go ahead and loop through that image you want to look through everything that is a javascript file and we're going to do the typical for loop so for cons file of event files create a variable you open this up create a variable const event equals require we're going to open this up with the backtick uh we're going to do two spaces two dots sorry backslash go into the events and then go into each directory so money sign brackets directories and we're going to go into a specific file that is a javascript file file advanced event underscore name now we're getting the name of the events equals file that splits and we're going to do a single quotations with a dot and we're going to get the first one of the list so it's gonna be uh zero just like that that client that on we're gonna pass in the event's name we are going to bind everything that we need so bind the first one is going to be no the second one is going to be discord and the third one is clients and this seems to be an error up here what is the error oh this has to be equals just like that and finally the same thing that we did in our main.js file which is we kind of loop through everything like this come in here all the way at the end and we pretty much want to do the exact same thing for these folders so the first folder was called client and the second folder was called guild and for each we want to e and pass that variable into this function that we created so load directories e so that's pretty much it that's our command handler and that is our event handler now we want to go into the events and let's create some events so the first event we have here in our old file is the ready which is when the bot is online we want to send a message to the console if we actually go ahead and run this clear it's not going to send a message because there is no event that's you know telling us to contour that log when the bot is online so to do this this is actually a client event so we're going to create a new file and the event itself was called ready so it's going to be ready not js and ready.js all you can do really is just do module that exports so module that exports um and this is just going to be a simple console.log so console.log and you can type whatever you want so code lion bots is online like that and actually if you go ahead and run it it should work so notice space dot we got an error filter ends with this is where is this the event handler it actually ends with not end with thank you for debugging this for me or telling me the air there we go codeline bots is on line perfect so we have that event already running through our event handler let's do the message event which is pretty much the most important event handler so we're going to do or sorry the event which is the message event in our events folder inside of our guild folder go ahead and create a new file called message.js and these names of the files are exactly the names of the events so client.message this event called message which is how we used to handle our messages it's just going to be a file called message.js so in here we want to do a module exports dot exports equals we're going to pass in discord discord i'm gonna pass in clients as well as message now the reason we're passing this discord is because sometimes a command might need discord so something like an embed is gonna need a discord um so that's why we're passing in discord if you are more proficient with javascript you could do this in a different way and not passing discord through every file but for this tutorial that is what we're going to do so khan's prefix this is kind of how we usually do all of our commands here and actually let's go ahead and delete the prefix here um we don't no longer need it in our main file we need it here in the message events so cons prefix you can put the prefix that you want so we're going to do pretty much the same things we did episode two of the series which is just if the message content um that starts with the prefix if it doesn't start with the prefix or if the author of the message was the bot itself but we're just gonna simply return we don't want the bot to send its own commands and we also want to start with the prefix advanced args equals message.content dot slice that prefix that length and if you really wanted to you can easily just copy this from your old main file we're pretty much doing this if you really look at the code so prefix the length then we're gonna split i'm gonna do slash space plus sign slash cons tmd equals rx dot shift shift um and we're going to shift to lower case to lower case hopefully i'm typing everything correctly if i'm not that's going to be a pain to come back here and fix some errors cons command equals client not commands that gets cmd and finally if command exists we want to execute it so command dot execute client message args and discord if the command itself requires it so we're pretty much done however if you had previous commands and you're kind of just rebuilding it with this event handler i'm showing you go into each command and you're gonna see that we used to have something like this which is the execute and we're only passing message and args make sure you pass in clients before you might not use it but you have to pass it in and if you want to pass in discord you can just add it at the end if you require so if we do for a clear command we have to do that i think i already did it for our play command we did right here client message args let's do the leave did not do it or leave so i'm gonna have to add a client in here so that's pretty much it everything should work so let me go ahead and run it and see if we get any errors which we shouldn't going to our discord server let's go ahead and play which is one of our commands and it works any errors no errors so we did it that's how this command is or this command handler and this event handler works if we actually go ahead i'm going to open this up in this right here this is how our project is structured we now have a handlers folder with our command handler and event handler can i increase this so it's bigger i cannot that sucks we have an events folder for our guild and client events and our client events we have the ready and our guild events we have the message and for example if you come here in our old main file we had another event called guild member ad and this is a message when a member joins then it's going to send a message to a specific channel you can create this inside of the guild right here and create a file called the guild member ad and that's how it should do it make sure the caps are you know the same and that's pretty much it so now we have a pretty good structured project and if you are deploying a bot or you're going to organize your bot better definitely do it this way don't do it the old way where we had everything in one file like again let me go ahead and show you our main file is literally this big that's it everything else happens through all of these files and all these files are pretty small eventually they're probably going to get bigger if you have a you know bigger discord mods but that's it guys that's all for this video thank you all so much for watching if you enjoyed the discord.js tutorials make sure to subscribe to this channel and i'll see you guys later on my next video peace
Info
Channel: CodeLyon
Views: 66,830
Rating: undefined out of 5
Keywords: code your own discord bot, code your own discord bot - basics (2020), code your own discord bot 2020, code your own discord bot mac, how to code your own discord bot 2020, make your own discord bot, make your own discord bot 2020, discord bot command handler, discord bot event handler, discord bot events, discord js event handler, discord js tutorial, discord.js command handler, discord.js event handler, discord.js events, discord.js handler, command handler discord.js v12
Id: Sihf7B8D4Y8
Channel Id: undefined
Length: 18min 59sec (1139 seconds)
Published: Thu Jan 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.