Prefix Commands & Events Handlers | Discord.JS V13 | S2 / #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
yo what is up guys welcome to the second episode of our tutorial series on how to create your discord bot in this episode i'm gonna be showing you how to create your event and command handlers plus how to make your first command and your first event listener so to start out we need to download the package which is called fs so we go to our terminal type npm ifs and that should download the package for us now if we head over to our ndx.js file we can accumulate our collections so we're gonna need two collections one for the commands and one one for the cooldowns if we go up to the second line where we required intents from the discord package we can add a comma and require collection and now if you go down under the um config.js and require we can type client dot commands equals to new collection and now we'll create our cooldowns collection so client dot cooldowns equals to new collection once that is over we're gonna assign our prefix so we go to our config.json file and we're gonna remove this line quick and we're gonna assign assign our prefix so you go to the first line the token line and you add a comma and then you add a prefix name and you add the prefix as its value so i'm gonna have dot as my prefix you can have it as whatever you want once that is done we go back to our index and then here we can wait a little bit and create a folder so we're going to create a folder which is going to be called handlers and inside that folder we're going to create our handler files so the first one we're going to create is called events handler.js you can name it whatever you want the second file is going to be called commands and blurp.js once that is over we can require it in our ending so js file and pass in the client and discord to it so we delete this line over here and now we type we pass an array and we pass in the events handler name over here it has to be exa exact name as the handler same with the commands handler we're gonna put up another quotes and now we're gonna loop through them so we have for each and we're gonna require [Music] backticks dot slash handler slash and and then we're gonna pass in the decline and discord so we can use them inside of our handlers once that is done we're going to create our actual um categories or commands folder let's create the commands folders so commands and now we'll create the events folder and inside of them we're going to create subcategories so i'm going to create a subcategory for commands one for you leds and another one for uh moderation now we go to our events and we can create subcategories over there i'm gonna create the client events and message events you see how organized and good looking our handlers are now if you go to our handlers folder we can start writing the events handled first all right so the first thing that we need to do is um require reader reader sync from the package fs with accounts we leave this object empty for now we're gonna type require fs and inside this object we're gonna uh get that reader sync simple as that now we'll go down a couple more lines and we type model.exports going to pass in the client and discord let me create another machine and inside this object we're going to type cons event folders it was to read their thing just like this i'm gonna pass in the afterlook location of the events so dot slash events we're not typing two notes because fs the package fs works from the parent um parent file so now we're gonna close this line over here go down one more line and therefore folder of event folders and now we're gonna define the event file so cost event files equals to reader sync i'm gonna pass in dot backticks actually dot slash events slash folder and then we're gonna filter through the folder so we have filter files files dot ends with dot jx so we're only gonna pick up the files that ends with the dojs extension and now we type for cons file of event files and we're gonna define the actual event so cons event equals to require now we type backticks.slash event slash folder slash file so this is the actual uh event file and now we're gonna check if the event type is one so if event dot once we're gonna run it as a once event so we type line built once and here we're gonna pass in the event.name the args sorry just like this you're gonna create a little arrow and we're gonna type event.execute we're gonna execute the the args client and then this code simple process and now we're gonna check if it's not once so else it has to be a an own command so client.on so we cop paste this explicit copy this we pass it here and we change the ones to on and here we can close this line so the code looks nicer and we're done with our event handler now we go to our commands handler and we start writing inside of it so we're gonna do the same process you can actually copy paste this over here and just remove these lines all right so now we're going to type const command folders equals to reader sync we don't need the read we need the reader sync dot slash command the same process as the event handler so we go to one more line and we have four cons folder or command folders you can open up this object we type const we define the command files command files equals to reader sync i'm copying the the wrong thing reader thing backstage dot slash commands slash folder and now we're gonna filter through it like we did before files files dot ends with dot js and here we type for cons file of command files you can define the actual commands accounts command equals to require backticks.slash command slash folder slash file and now we're gonna uh set the command to the collection.line dot commands dot set we're gonna set the command dot name that's the command simple as that we're gonna close this line real quick so the code looks nicer and we're done here so what we're gonna do now is create our message um command detector so if you go to the message subcategory of the events we're going to create a file which is going to be called message create it's the case sensitive because this is the new message event name it was before message in discord version 12 now in 13 uh it's called message create so it's very simple and straightforward we're gonna type we're gonna actually get some parameters so we have cons client message equals to require discord.js all right and now that i'm under it we're going to require the prefix of const prefix require dot slash dot [Music] all right and now we model exports like we normally do exports and the name of the event is going to be exactly then the actual name of the event message will create this case instead of like i said and down here we can add our parameters the first parameter is going to be a client as a client the second one is going to be message as the message simple now we go down one more line and we type async execute and we can execute the message a client and discord all right now we're going to shake our first shake so we're going to check if the message doesn't start with the prefix or if the message author is a bot we're going to just ignore that message so if message dot start oh sorry guys dot start now actually content content dot starts with prefix or if the message author is spot we gonna return right and down here we're gonna define the original cons ours equals to message.content.splice we're gonna slice the prefix dot length and then we're gonna uh trim then we're gonna split uh pretty sure it's like that yeah just like that and down below we're gonna define the command name so const command name we're gonna shift arcs to arms dot shift and then we're gonna take it to be lowercase simple and now we're gonna define the actual command itself so const command equals to client dot command get gonna get the command name and then or uh we're gonna get the client we're gonna find the commands of client dot commands dot find and then we're gonna find cmd cmd analysis and cmd analysis um dot in illusory command name pretty simple and straightforward all right now we're going to check if it's not a command so if the mission mark commands my m is having trouble we're going to return so we can ignore it again now we go down and check the command permission so if recommend per mission so if the command has permissions we're going to shake the author permits so author terms message to channel dot permissions or the message the author now if author terms so there's no author terms or if there is the also doesn't have the permissions author terms dot channel no no sorry actually has [Music] sorry guys so um author perms.has command per missions over here we're gonna create these brackets we're gonna return so basically uh if there's no other perms or if the the the author of the message doesn't have the author at the command permissions we're gonna just return we're gonna ignore it what you can do is you type a message reply and send an embed saying that you don't have the permissions required and if you wanna do that let's do that real quick um so you type cons it's actually required the message embed message embed so we type const um no terms equals to a new let's see message embed we're going to set this the color to be red red then we're going to set the description to be uh you do not have the required permissions to run this command all right and then we're gonna send the message so we're gonna type message dot reply and uh the embed is going to be embeds no burns all right and now we can we can actually delete this message so we're going to type let's see so when we send this message we're gonna delete it after some time so we're gonna type dot then then set timeout we're gonna set the timeline for the delete send dot delete and we're gonna set it to be two seconds so two thousand milliseconds very simple and straightforward all right now we exit out of this uh if statement and we're gonna type type see here we're gonna type cons cooldowns so we're gonna get the cooldowns and uh collection from the from the client so client and i'm gonna check if the command doesn't have cooldown so if dimension marked the cooldown so it has command name we're gonna set a cooldown anyways to hold down um those sets we're gonna set the command name and new discord dot collection or we can actually do this we're gonna import a collection over here we can remove the discord collection simple as that uh all right and it should be eight for the um cooldowns uh for now now we go down a couple more lines and we're gonna define the date right now so bones now equals to date dot now all right now we're gonna cons timestamps equals to cooldowns dot get we're gonna get the cooldowns from the command name and then here we're gonna define the cooldown amounts accounts pull down let's see cool down sorry down amount equals to command dot pull down or so let's see yeah pull down or one second so if the command doesn't have a cool now we're gonna set it to be for one second either way so we don't clog up our boat so now we go down a couple more last we type if time stamps don't get dot has message dot message author id we're gonna define the expiration time so expiration time pressure i wrote drawn so expiration time equals to timestamps dot gets message dot author id plus the actual cooldown amount cool down amount right and now we're going to check if it expired so if or if it didn't expire so expiration time we're going to define that time left equals to expiration time minus the time right now as of the message divided by 1000 which is 1000 milliseconds and then we're going to return a message reply let's make it an embed so we type cons time left embed equals to new message we're going to set the color to be a red white and it's working off new messaging but then we're gonna set the descriptions and description please wait another time time left sorry time left we're gonna to fix more seconds to be able to run this command again [Music] simple as that and now we're gonna return it as a message reply so return message.channel.send actually let's do this right here too message. kinda look sent we're gonna remove this simple uh and we're gonna pass in that one bed to be the time time sorry time left embed pretty simple and straightforward and then we're going to delete that message so dot then the same process that we did before sent we're going to set timeout set timeout then we're going to type set delete after two seconds again all right now what we can do is actually make our our code look a bit nicer by closing the lines let's see over here down here down here down here all right now we go down one more line and we're gonna time damage dot set we're gonna set the message but also the id now and then we're gonna set timeout [Music] to be timestamps dot delete message author dot id we have delete the cooldown amount and now we're gonna actually try to run the command so try command dot execute we're gonna execute the message args command name a client and then discord uh and if an error happens we're gonna catch error and we're gonna console.log the error and then we're gonna send the message we're gonna create an embed so components header embed equals to new message embed the color of the embed is going to be red because it's an error set color red and the description is going to be put on this the description description is going to be an error happened while trying to run this command check console for more details all right and now we're gonna uh send the messages to message the channel don't send we're gonna send the embed error we're gonna close the slide close this available category under our events and we're going to create a file called ready.js we're going to simply model both exports sorry we're going to set the event name to be the actual ready event and now we're gonna execute that client and now here we're gonna type console.log that client is now ready we're gonna add an emoji by pressing windows dot let's have the green part now we're gonna set the client activity client.user.com just like that gonna be in development and then that line i mean sorry the tie is going to be watching pretty simple and straightforward and now our first event is actually ready let's remove this now we're going to create our first command so we go to the utilities and we create a file called pin.js and then here we're going to simply record the messaging that's a message that required from the discord package require discover js i began a model dot exports the name of the command is going to be ping the allied the commanders are gonna be uh latency or lag the permissions of the command is gonna be an administrator um the description is going to be description it's going to be friends that clients [Music] and the cooldown is gonna we're gonna have it to be um three five seconds we need to add a comma over here and now we're gonna execute the message orgs command name client discord and we're going to create an embed so calls response it's going to be a new message embed the color is going to be that color we're going to set it to be green green right the description is going to be the actual world paint so we can add an emoji called phone i'm pretty sure sorry so windows dot and we know what's happening um and it's going to be phone vision or ping just like that and then here we're gonna type blind continue blind.ws.p and we're gonna add an ms at the end and here we're gonna say send the magician message dot channel will send we're gonna send that embed to embeds response all right now we restart our bot to clear our console and see we got an error over here let's see what's uh the error if you go to our advanced sender here we go it needs to be event that that the exact name of the events folder so we correct this error we're gonna type node dot and like you see here the client is now ready as we said it to say here if you go to this for that now and try our pin command so dot ping it should send us the message it didn't let's check the error let's see command is not defined here we go it should be command with two m's i only wrote it with one m so here let's try again and i would type dot ping and error happened while trying to run this command check console for more details that's the error message that we created and now we're gonna go to the um here the message creates nay it should be name instead of name so we clear the console again no dots and now we type the pink man again and it should send us the message i will type latency should send us these with another like you see like we are handled now we type latency again we should send us the ping and if you type the last alliances of the pink command dot like please wait more seconds we can wait and we're gonna get this um the response error free so let's close all of these files over here and we're gonna see how clean our discord looks i mean our bot looks this is how clean the index.js file is the handler event and the command so thank you guys for watching see you in the next episode
Info
Channel: Lyxcode
Views: 5,042
Rating: undefined out of 5
Keywords: discord.js, discord.js v13, discord bot, tutorial, v13, bot turorial, how to make a discord bot, discord bots, discord bot tutorial
Id: 35Pm-X4vplM
Channel Id: undefined
Length: 25min 27sec (1527 seconds)
Published: Fri Jul 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.