Advanced Command Handler | Discord.js v13 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 gonna do an advanced command handler for our version 13 discord.js spot so first thing we need to do is we need to create a new folder called util this is where we're going to put all our utilities in and all that then we're going to make another folder named commands it doesn't matter how you spell commands but it's going to go like that so the first thing what we're going to be doing is i'm just going to quickly come over to my other command base i'm just going to copy all this and i'll put this in the description now what we're going to be doing is in our commands we're going to create another a file named command base dot js in this file what we're gonna do is we're gonna this is our whole entire sequence for commands so it's going to run through this every single time we do a command so the first thing we're going to do is we're going to do const prefix and brackets equals require and then since we in our config.json we put we're going to put prefix in here and name it like that put the colon and then we're going to put our prefix as dash for now now we're done with our config we can save that now what we're going to do is we're going to come back here we're going to require our config.json so two dots and slash and then config.json now after that what we're gonna do is we're gonna do cons validate permissions equals parentheses permission now we're going to do our arrow function the brackets now inside of here we're going to do const valid permissions and then we're gonna do an equal sign once i get over there and then we're just gonna do our straight bracket now this is where i'm going to be pasting all of the um discord permissions now since we did that we're going to come right here go down a bit and we're gonna do four const permission of permissions and then we're gonna do our brackets then inside of here we're gonna do if not valid permissions this dot includes and then we're going to do permission insider parentheses now we're going to go out we're going to do another bracket inside of here we're going to throw a new error just in case at least there is an error now we're going to do parentheses and then we're going to do our tick which is right to the left of the one then we're going to just put unknown permission node then we're going to do our dollar sign then our brackets we're going to put permission in here and then just so it looks better we're going to put out put quotation marks double quotation marks around it now we're gonna come down to three and then we're gonna do const all commands equals brackets after we do that we're going to do well experts in module.exports equals and we're going to do our parentheses command options now we're going to do our arrow function and we're going to do a bracket now inside of here we're going to do let's bracket inside the lit we're going to do commands comma enter for a new line and permission since we need permissions to be an array we're just going to do the straight brackets and another comma now we're going to come down to here we're going to do equals command options now after that we're going to do if not command commands and then we're just going to do a return now we're gonna get out of there we're gonna do if type of commands three equal signs then we're gonna do the single quotation over the string then we're gonna do commands equals straight brackets commands now this is basically saying for instance if we have if we have four hour fans it would be like this if we have it like where we have one so let's say it's ping or if that would means it's only a command but then if we add a comma another one like let's name it pings that means it would be a string so it's gonna do both commands now that that is done we're going to enter again now we're going to console.log do parentheses and then inside the parentheses we're just going to do registering command and then we're going to do double quotation then dollar signs and brackets inside of here we're going to do commands then we're gonna do straight brackets zero now this is just gonna say on startup we have all these commands then we're gonna go down then we're gonna do if permissions and then we're gonna do dot length then we're gonna do our brackets then inside here we're gonna do if type of permissions equal equal string do another brackets this is going to be permissions equals permissions now what this is saying is if we have multiple permissions so basically it's going to look like this permissions then let's say we need the member needs ban members so that would be just one permission now if we need two permissions let's say they also need send messages that's just gonna make it into a string and that is gonna be what it would look like now we're gonna come out of there we're going to do validate permissions then we're going to do our parentheses permissions now after that what we want to do is we want to come out of here and we're going to do for const command of commands then we're going to do our brackets we're going to do all commands then we're going to do our straight brackets command then we want to do equal sign then our bracket again after that we're going to do three dots then command options and we're going to do comma and then underneath we want to do commands then permissions now since we did that what we want to do now hold on guys i'll be right back okay guys i'm back so i did do a little mess up so we're going to copy this real quick and delete it we're going to put it right here then we're going to copy this delete it and we're going to come right here and put it right here now we're just going to quickly format it now since we did that what we need to do is go down three then we're gonna do a different export so module.exports and dot listen now this is listening for the commands we're going to do clients right here now since we do that we're going to do arrow function then brackets then we're going to do client dot on message create because that is an instead of message it's not message created in discord.js version 13. now we're going to do parentheses then we're going to do message here now we're going to do our assignment brackets now what we're going to do is we're going to do const member these are all going to be going to equal message and this is just for everything to be simpler we're going to do member content and guild we're gonna do equals message now that we did that we want to do cons prefix equals then since our prefix is up here we just want to name it prefix and since we got that done we want to do const arguments equals content dot splits then we want to do parentheses slash sp splat slash then our straight brackets plus slash then after we did that what we want to do now is we want to do constants equals message dot content dot slice then we want to do our parentheses then prefix dot length and then out of parentheses we want to do split parenthesis slash plus slash and then we want to do that right there now we want to do cons cmd equals ards dot shift then after shift we're going to do two lower case now that we did that we want to do those again then we're going to do consonant equals arguments dot shift dot to lowercase after that we want to do if name that starts with prefix and then we want to come out there we want to do our brackets again we want to do const command equals all commands then we want to do our strip brackets name dot replace then we want to do our parentheses prefix comma and then our single quotations after that what we want to do is we want to do if command now we're going to return that scene if it's not a command it's just going to return the option now we're going to do const and we're going to enter permissions dot permission error comma enter permission error equals you do not have the permission to run this command and this you can change to whatever you want this is just what i'm putting so it doesn't matter what you put here then you want to do another comma inside of this one what you want to do is you want to do required roles equals a string then min args equals zero and then max args equals null because that's what you're going to be able to set then you want to expected art then you want to do comma and then we want to do call back then after that you want just equals command now that we got that done what we want to do now is we're gonna go down to like that we're actually now what we're going to come after command press enter then we're going to for fonts permission of permissions then we're going to do our bracket and then i'm going to quickly fix that then where you want to do if member dot permissions that has and then inside our parentheses we will input permission then we want to do more brackets message dot reply permission error now since we got that we want to go down to and enter real quick after mesh.reply want to put return because that's the error message we can put if it's incorrect if they do not have the correct permissions now we want to do four cons required roll of required roles do another bracket we want to do construal equals guild dot roles dot cash dot fi then we want to put our parentheses and enter and then more parentheses roll then arrow sign roll dot name then three equal signs required rule after that we want to do if not roll then we're going to do else or statement so two straight lines member dot roles dot cash dot has roll a dot id and then we're going to do our brackets and this is going to be message dot reply you do enter you must have the double brackets double quotation money signed in our brackets real quick since this is going to have the brackets in there if we wanna have the backtick inside here we wanna put required roll then we wanna put roll to use this command now in this you can put whatever you want it doesn't matter but after this right here the end of the parentheses you want to put return and then you want to go down two press enter then you want to do if parentheses enter arguments dot length then you want to do a less than sign in min args then you want to do an or statement then enter then more parentheses you want to put max args exclamation point two enter signs null and a double end statement arguments dot length then a greater than max arms then i'm going to come right here put more parentheses after this we want to do message dot reply and then you want to do a back tick and then you want to put make sure to use the correct format of the command now this you can put whatever you want this is just saying that if you set the maximum marks to one and they put let's say you're doing a ping command you do your they will put the prefix then ping then a different word after it'll do this reply now that we got that done what we want to do is we want to come after the parenthe and put return then we want to come right here and then we'll want to do our callback going message arguments clients args and cmd now that we got that done we should be good there let me quickly go do something and i'll be right back okay sorry about that guys but now what we want to do is we want to do a load commands file so load commands dot js now inside of here we want to do const path equals require path and this is already in your discord.js install so we should be there then we want to do cons fs equals require fs now what we want to do is we want to do module.exports equals parentheses client then we want to do our arrows in both then brackets then inside of here we want to do cons base file equals now we're going to do a single quotation marks command it's the dashbase.js or whatever you called your command base if you do not decide to call it command base now we want to do cons command base equals require hold on gotta do equal signs and require parentheses in our single quotation marks dot slash then we really quickly since we're doing this we gotta put our backticks now we want to just put base file right here now we are done with that part so we're gonna do cons commands equals an array then const read commands equals dir our error function then brackets then we want to do const files equals fs.read sync and then in our parentheses we want to do path dot join and more parentheses underscore their name and then comma now we're gonna enter real quick and we're gonna do four cons file of files do brackets after that we want to do const stat equals fs dot exclamation point sorry not x measure point we want to do l l stat sync going to do our parentheses i'm going to do path.join again and then more parentheses underscore der name then dir then file now after that we're going to do if stat dot is directory do brackets again now after is directory we need to put some more quotation marks then we want to read commands then do our parentheses again then in our parentheses we want to do path.join then we're going to do instead of here we want to do path.join again after the path path.join sorry about this guys we want to do dur then file since we got that we want to do an else if statements then we want to do parentheses file explanation point two equal base file then after base file we want to add symbols and file expectation point two equal signs then we want to do our single quotation marks load commands inside dot js then we want to do more brackets again then inside these brackets we will want to do const option equals require path dot join and then parenthesis durning comma der bim file then after that we want to enter don't want to do commands dot push commands dot for some reason oh it's because we named this command so it needs to be commands then we want to do push option after that we're going to do a nif statement i'm gonna push in there then we'll do if clients then we're gonna do brackets command based command base option now since i keep forgot to spell that command base we're gonna fix it now we want to go down four brackets and we want to enter then we want to read commands then we want to do our parentheses and commands now quickly since we did that we want to put a new file in here called commands now if you do not name this file commands where all the commands will go in you got to change this to whatever you named it so now since we did that we want to do command base then we want to do dot listen then we want to do clients then undo that and we're going to return commands now since we got that done inside of our util we need to make a file named commands dot js now inside of our commands.js we need to get our prefix so we'll do const brackets prefix inside brackets and then we've got to get our config.json so dot slash config.json well we gotta require it so dot slash config.json now we gotta do module dot exports equals parentheses client aliases and call back now we need to do our arrow function and brackets cider brackets we need to do if type of aliases equal equals three equal signs then we'll do string again and do brackets then we want to do aliases equals bracket straight brackets aliases now we need to do clients dot on message not credentials or whatever that was we want to do client dot on you want to do message create and then comma inside this we want to put message then our arrow function then our brackets now inside of here what we want to do is we want to do const brackets content side brackets equals message then we want to do aliases dot four each then we want to do alias then we want to do our arrow function then after that what we want to do is we want to do cons command equals backtick prefix then another dollar sign in alias now since we did that let me quickly figure out what that is about and i will be right back okay guys sorry about that figure out what was the issue i need to quickly put in another bracket in the front of there now that we did that what we want to do is come right here when we put a bracket right there too but right here what we want to do is we want to do if content dot starts with then since we have that we want to do inside here we want to do a backtick dollar sign command then we want to do it or statements so we're gonna come out here do the double whole straight lines content and then three equal signs command then we want to do our brackets and inside our brackets we want to do console.log backtick running the command commands now this is just on startup it will say in here which commands are activated and running now we want to do callback message now since that is done what we want to do right now we want to save all these and then i'm going to quickly go take a look at something and i'll be right back okay so what we're going to be doing now is we're going to create a command real quick so we're going to create a new file inside our commands folder we're going to just name it ping.js since we're going to do a quick pink man what we're going to do is we're going to do module module.exports then we're going to do an equal signs and brackets now this is where we're going to do all our command information so commands which will be our name and do our straight brackets and we're going to do those then ping let's say you're wanting it to have two activation so we're just gonna do a comma then another single quotations then we're gonna put our second name now we're gonna put comma expected args then we're just gonna do the colon then our array and then we're just going to leave that along because that's what i really do but this needs to be two single quotation marks then we're going to do permission error now this is going to be you what you want to say if there's an error so let's say you do not have the correct permissions to run this command then we're gonna do comma and then we're gonna do min args which is the minimum arts we're just gonna put that to zero for now then max arts we're gonna put that to one then we're gonna do our callback then our parentheses then we're gonna do message arguments client args cmd then we're going to do our arrow function there now all of these were in our command base this is where we're getting them from so we can copy this if you really wanted to and put this right here now right here what we need to do we need to put our comma this is where it's going to come into place for the permissions and the required rules so permissions you do that now we're going to do our signal quotation marks and let's say we're just going to send messages and you can always copy them from the command base also now required rules 2 that is if you have a required role role in your server you can put the role name right there but since i do not have any required rules for now we're just gonna do it like that and that is all we have to do for here now we're right here we're just going to message the channel send pong now we're going to save that and then in our index we want to do cons is load commands equals require then we're going to do get our commands and then our load commands then inside our client on ready we're going to do load commands and then we're going to put client right there and that is all we have to do so we're going to quickly save this we're going to check for any errors when we start the bot so we have an error right here read commands commands commands read commands is not defined it's in our low commands 26 read commands so right here is where we act on this type it needs to be read commands instead of re-command so we're going to restart a bot in there it registered the command and it's saying that bot is ready so we're going to go to our discord we're going to do pink now we got an error so we cannot access prefix before initialization so in our command base at line 87 i'm going to go up to 87 what we want to do here is we quickly want to see it's up there yep i'm going to copy that real quick make sure i spelled it right okay i will be right back as i check out this issue and i'll see you guys in a second so what we're going to do really quick is we're going to come back to our code and up here we're gonna copy just prefix we're gonna press alright guys so i just forgot that to press record but basically what i did is in the command base i double click prefix and press f2 and named it global prefix then i went down here and put change this to global prefix due to this mistake i will be putting the code in the description just because of this mistake i made but now what i'm needing to do is i whenever i do the command ping it obviously sends that so i'm going to quickly go back and check for the error okay guys i just figured out the issue so the code was doing what was supposed to do in but right here we need to put an explanation mark saying that if it doesn't have the correct permissions then do it i'm gonna quickly restart the terminal which will restart the bot we're gonna do ping and then it works and the other command pings it still works so i hope you guys enjoyed this video i will make sure i put all the code in the description just make it easier and i'll see you guys next time bye
Info
Channel: CodeViper
Views: 58
Rating: undefined out of 5
Keywords:
Id: AwHxa0EK-WM
Channel Id: undefined
Length: 34min 42sec (2082 seconds)
Published: Thu Oct 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.