EVENT & COMMAND & SLASH COMMAND HANDLER | DISCORD.JS (V13) | #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back a new video today i'm going to be showing you how to create a command handler an event handler and a slash command handler since it was requested by multiple people in the school server so without further ado let's hop into the video [Music] all right so here i am into visual study code so uh first thing i'm here that's what i did last time make sure to check out the video so you can understand what i did uh and how slash commands work so basically uh in v uh 12 i did a i did a command handler as you can see here so i'm not gonna make another one it's the same command handler the normal one but i need to do mpmi fs alright so here after i installed fs i'm gonna just go over here up and i'm gonna do const fs equal require fs all right you can go to the server to find my github if you want to so here's the command handler the normal one the message one all right so let me just put it anywhere over here all right here we go i'm gonna remove the notes all right those ones i'm gonna remove them but if you want to see them to understand how this works make sure to check out the github all right here uh let's put it like that to make more clear let's replace this like that return console.log all right and perfect so this is the command handler but we also need to do something if you didn't if you don't know that is basically putting here client client. uh commands equal a new discord collection basically creating a collection all right here so uh the collection will have the command name all right and for sure with aliases so i guess aliases was not here's i guess in the next one episode 6 and yup aliases are here basically so let me just copy this one instead all right perfect all right so i'm gonna also need to put client dot aliases equal new uh discord collection as well all right both will be collections we will need more but for now this is for the command handler so here let's put command handler with alice's alright so if you don't know how the command handler work instead of just going into the message event where is the message event is here so instead of doing like if message uh content or starts with and then put like ping and then return instead of just doing that for each single command and have your uh index just full of messages uh sorry full of script and bad looking or i think i'll just create uh or before creating here in the message event you're gonna need to put something that is uh here let me search for it those all right let me just copy them and put them here so here the prefix of the bot all right so let me put the prefix of the bo something like uh let's put it as equal because i like it all right and here i'm basically defining the orgs all right so the message array i'm just splitting the message content to cut the array and then the command is going to be array 0 and rx is going to slice it all right so here all right here it will make sure it works with its original name and the audience's name and here if commands so if it found a command and gonna run it all right so let me now create a folder named commands all right and i will have to create another folder inside of it all right so it's basically something like this uh commands and then category and then file or the command itself all right as example commands uh moderate moderation vandal jazz all right so let me actually create a folder and let's name it let's put it as like admin all right so uh or actually information all right it gives information about the bot and let's create here a ping.js all right go just reply with punk but now in here you'll need to put something all right so let's first define this chord is equal to uh call discord equal to required discord.js all right and then i'm gonna need to put module dot exports dot run equal async and then i'm gonna need to pass the parameters that i put it over here client message arcs and prefix so client message args and prefix all right and then i'm gonna just do like this and also to make sure that it only works with your prefix and not other prefixes so we're gonna put here uh after command let's put into brackets and put if message dot content and let's put an exclamation at the beginning what does that mean exclamation do it basically just says no so basically like if i have this it says the like reverse all right so like if i said yes if i said like this look let me type it yes that means yes all right but if i put like this now it means no all right that's what uh exclamation mark doing here so message.content dot starts with all right and prefix return so if it does not start with the prefix i want to return i want it to only work with the prefix all right so here let me put something like message.channel.send pong hopping uh here all right and now i need to put the name and addresses if you don't know what this is is basically putting more than one name for the command all right so i'm gonna put here module dot exports dot help all right is equal to this name i'm gonna be naming it pink aliases i'm gonna name it uh let's put like other names let's put like p only all right so here let's try it no dot as you can see down but let me also put something because i'll be making three uh handlers so i don't want them to actually get into each other's so let me here uh go here file was loaded so let's say command actually save file but let me put here command handler like that all right so i know this is from the command handler and not the event handler or the slash command handler alright so command handler file.png was loaded yes pingdodgeos it's not for the ping you're gonna reply with pong as you can see easy all right now moving on to the next one that is basically the event handler all right so uh i'm gonna need to put here client dot event or events equal new this coil collection all right and i will need to actually put one thing that you maybe don't know but actually let's put it at the end so understand how it works so now after i did that let me copy it like actually let you just copy paste this twice here and let's name it here event handler all right as you can see so let me create a folder let's name it events all right so uh now i'm gonna just put here uh events i'm gonna replace commands with events the folder name if that's easy to understand so here i'm going to just uh also let me put here command handler or command z handler with an s all right can't find any commands all right so here i want to say event handler can can't find any events for sure so uh here we're gonna need to change this to events and also i'm going to change a lot of things over here so let me just remove the whole part uh here all right so let's start by putting here something so let's first get the events the cons event get all right dot slash events and then slash and let me change the quotation as well all right here i'm gonna just put the file all right so uh now i want to put like uh let's say try catch directly so try catch better also let me put something here in the commands handler let me put like loaded command loaded commands equal to zero uh or actually not screw it so let me let me put here uh client dot events dot set uh event gut uh name all right and even got and if there is an error i want to just console the logit return and console.log it all right so basically i'm just now uh getting the event name and putting in the collection with the information stuff so let me now test the event handler all right let me create a new file in it and let me call it message.js all right and make sure to name it the like the event name i guess so uh i think it doesn't really matter you can name it something else all right so uh let's get all the events from here and put move it over there so we got here a ready event and we got interaction create and message create so let's just move all of them to there so i did the message create let me just name it again message create and let me create another one call it uh interaction and interaction create doll.js and let me just create the last one called ready.js all right so let's start by the ready since it's the easier over here all right so i'm gonna just literally just copy this all right and i'm gonna paste it in the other place here all right and let me uh let me make it clear let me just first delete this from here all right let me copy this put it into here and let me copy this and put it there message create all right so uh one thing you need to know that once i start now the bot gonna give me an error all right you probably will wonder why you must say that the client is undefined let's take a look all right uh says here we there there's another error events all right what's wrong with it oh let me take a look oh uh let me let me just remove this since it's inside one uh it's not inside any uh folder it's just inside the like it's not commands and then a category is just the folder and then files directly all right so here after i just fixed it so uh now let me uh after i i just started the bot as you can see i said in the beginning go give me client is undefined so how to fix the uh so basically to fix it you're gonna need to export the client from the index to the event header which is basically gonna uh leave us doing this uh module dot exports all right so module dot exports dot client equal client all right so now i can go to the ready event here because i have client dot on i'm gonna just need to put const client is equal to require a slash index.js or index like that and i guess two dots and then index and client and let me just paste it here and here and there all right and if i didn't know dot as you can see it says command handler all right and let me i'll also make it log the events like this for the comma for the event handler you can just do here uh let's check equal fools all right and let me put over here uh console.log let me put here event handler and let's put an s all right and let's say here file also say if check is equal to fools i want to do it all right so uh also let me put that in here oops all right so let me put here uh event uh was loaded all right and then i want to do check equal true so it doesn't just log it over and over again all right as you can see here events are loaded command is loaded now if i uh if you can see here it console.logged normally and everything is working the event handler is 100 working now moving on to the last and final handler of today's video and the most important one that you all need it that is the slash command handler so as always we'll start by putting here client dot uh slash commands equal new discord dot collection all right so uh let me just close those files all right perfect so now i will also do the same way i'm gonna copy this part i'm gonna paste it here and then let me put here slash commands all right so i guess we can now change create an or create a new file let's name it uh let's name it slash commands all right why is this looking like that all right whatever anyways so here in this one uh i'm gonna basically just change the name to thoughts so slash commands i'm gonna also put it over here and uh here so i'm gonna put here slash command or slash handler let's just call it slash md handler like that all right let me paste it here all right so uh apparently i can i can say that uh that's it here it's basically the exact same but uh i actually don't want the aliases all right i don't need the aliases and uh now i'm going to put something in the uh what is it called the interaction all right the interruption create event i'm going to need to put a lot here let's slash command commands equal client dot the uh like the thing we created the collection we created all right so slash commands wasn't capitalized uppercases or small s all right it was as big as uppercase that should be replaced here as well so here offline.slash commands dot get only got gut oh come on don't get all right and i want to put here the interaction all right i'm if you know in the last one here interaction it's called interruption but i just put enter so it's shorter so interaction dot command name and then i want to put if uh slash commands you just do slash commands dot run and i'm gonna put here the interaction all right so uh all right so uh let's now just uh they're just looking good now everything looks great so far so uh now let's create a slash command actually so let me show you how to handle it so basically i'm gonna create a new file all right let's let's name it here like like an index all right i'm gonna name it data handler all right or oh all right let's just name it data handler which we're going to be creating the command the slash commands like in the in the slash command handler we are responding to them but here we are creating them all right this might sound a bit weird to you but don't worry so i'm gonna create an uh function here all right i'm gonna create two functions let's put one equal create command all right and i want to put here client and the build id as parameters so cost data here basically it's an array all right here let me just make a bigger right here like that all right and here is my first uh let's say what what is it called i forgot the the slash man itself all right so i'm gonna put here the uh like slash clash what i'm gonna just name it echo all right we are putting here the echo command all right so the name of it is obviously echo which we already did it here as you can see that's the data so let me actually just copy it from here paste it over there here and paste it bim all right and let me create uh or let me put something actually let me get this where is it let me delete now i don't even need it here anymore let me get this i need this all right i'm gonna here put it that's my gold id by the way but i will not need it for now let me keep it here and put here the guild id so if you're gonna create one command one slash command you're gonna use commands.create if you're gonna use multiple ones you're gonna use those set all right and right now i'm gonna actually use one all right so that's it here so uh if you want to create another one i will show you right now but give me a second so here i don't need that anymore since i got now my slash command handler so let me create a new file in it and let me name it echo.js and let me put here module dot export dot run equal async interaction like that i'm gonna put module.exports.run or dot help equal name that's gonna be uh echo the name of the slash command and permission all right also i forgot to mention here if you don't want any aliases just to keep it like that empty all right and here permission i i'm making here a uh slash command handler with permissions plugin like permission handler in it so anyways i'm gonna uh reply to it so here as you can see how did i reply i'm gonna copy it and also i'm gonna all right here i'm gonna just put it context equal interaction.options.string that is the text as i said last time what does that mean all right so here i'm gonna put just those because uh slash command so if the interaction is a slash command basically i want to just do this right so now i did the handlers i did everything it should work let's check it out oh there is an editor uh i probably uh yeah i forgot to put here the category i'm gonna just put here like uh information or test all right i'm gonna move move it here it's the same as the command header it's gonna be slash commands and then a file or another folder as a category and then echo.js or like slash dot commands admin banter.js like that all right and now let's try it out all right here we see everything being loaded if i now did slash echo text hey as you can see it now works perfectly everything works just how it should be all right so now uh i'm gonna create another like another uh slash commands to understand how this works so let me change first set here like that and uh will tell you right now we're gonna need here module.exports equal create command all right so let me put here so if i want to create another command i'm going to just put here like let's say it uh a pink and gonna reply with pong all right i'm gonna just put here comma and see so and i'm gonna just continue put another one like that as you can see i just put another brackets i'm gonna just put name echo uh oops i mean ping description gonna be reply with pong all right and uh one thing now i want to go to my ready event and i want to do cost uh i want to get the function from there all right so i'm going to just put here create command the function name and then require dot dot slash uh it was where where was it it was your data all right perfect so i'm going to just put here create command i want to put here the client and the gold id so you're going to ask me right now what if i wanted to be for all the guilds not only one gold first first thing if you want to test something it should be for only one gold because if you did for multiple ones it might take up two hours to respond all right to actually work but for testing for one goal to go just work instantly uh so let me now show you if i did ping let me do it again here slash as you see ping existed right now we just created it it worked let me now try it it won't work because i didn't handle it alright so if i now went here create a new file name it ping.js and uh let me copy this put it over here change this to pink because the name is ping and uh just reply with pong all right and about the permissions all right because i didn't fix the permission part let me go to the interaction here i want to say if uh interaction dot member dot permissions dot has and let's put here uh slash commands dot help dot permissions all right what i did i just went to the command and then the help as you saw here there is help this one and then the permissions is called your permission so let me actually limit member permission like all right i'm gonna uh go just put it here and then let me put it in a array or you know let's remove it from the array let's remove it so basically if he don't have it let me put the exclamation mark i want to return all right so now all is left is uh actually this might not work i'm not sure if it's gonna work so let me remove it i don't wanna give you code that doesn't work so let me remove it if you want to check for the permission just put here if you don't have it just return all right so now i want to do something that is basically uh uh just show you how to create uh globally all right because if someone said right now i don't want to create for one girl i want from multiple ones you gonna hear create another function all right but uh like a sync function blah blah blah the same way we did it over here with every single thing but instead of having this line you're gonna change it all right let me show you uh to what so basically i'm going to just go to slash commands here let's take a look here i'm going to just replace it with this line but with set not created by the way all right that's it easy all right and let's put like global uh command all right and you're gonna just copy this paste it but the only thing you're going to change here you're going to change this to global command all right and remove the build id and here let me just copy paste this perfect that's it now it's gonna work but for sure you're gonna go to the ready event here and here you're gonna put that global command and you're gonna just put here global uh like global uh command and you're gonna put your client that's it all right but i don't really want to use the global global sorry so i can delete it but you will have to use it since your bot is public mine is just for test and let me uh let me just change everything back how it was there all right so that was it for today's video i hope you enjoyed don't forget to like subscribe and leave a comment down below what you want the next video to be that's it and ciao
Info
Channel: UltraX
Views: 8,617
Rating: undefined out of 5
Keywords:
Id: pcF1sOaHvEI
Channel Id: undefined
Length: 36min 15sec (2175 seconds)
Published: Tue Aug 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.