THE EVENT HANDLER! | Discord JS v13 #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on my fellow web developers we are continuing our discord series episode three and in this one we're going to be working on a event handler so in the last one we created a command handler now in this one we're gonna have to kind of redo a couple of those bits but i'm gonna show you what we need to change to be able to make it smooth and to be able to to handle all the offense discord offers now discord offers a lot of different events we're only gonna be covering the two ones we've already done in our event handler but when it comes to adding more we're going to make the process so much more simpler so guys let's just let's just ping our bot make sure he's still working he's still working okay let's move into fiest code and get started so over here we have our simple bot now the issue with our current step is our ready function is all in here our client and there's gonna be loads a lot of these um on method slash once methods we need to cater for so it's not going to make much sense to put them all into one file then you have to scroll for ages up and down up and down and you know we want to make it easy for us and you guys to build bigger better discord bots so what we're going to do is we're going to create a new folder we're going to start off with a new folder we're calling it offense now inside of offense what we're going to do is we're basically just going to create two files we're going to start with one and this is going to be the ready event so you see here we have the ready event we're just gonna call it the same name hit enter and then we're gonna come in here now there's a couple of things we need to do i'm gonna show you the basic layout we need so we need a module.exports just like we did for the command and then we're going to take a name the name is going to be ready which is great and then we're going to call execute just again like we did for the previous command so in ping you can see here we have a data and a execute for this one we're just gonna have a name and an execute so a similar thing we're also gonna have for the one what for the um ready event we're gonna have once true because inside of our bot if you see here we actually only call client once whereas we call client interaction on so we only want ready to run once so we're going to add this extra parameter so we can always call that now in our execute we're gonna need two things we need client and we're gonna need our command so we're gonna have to pass through both those fee fields those features and that's that but to access a few things from our on ready function what we're gonna do is we're just gonna copy all of this we're gonna cut it we'll delete this and then what we're gonna do is go into our ready function and paste this all in and there you go you can see that's now in there but this isn't gonna work we're gonna have a little error um it's you know i mean to be fair i don't think we'll have anything because yeah we're no longer calling ready which means nothing actually runs anymore but that's fine we've got this here so we're saying goes online client id we've got this we've got a client from there and we've got our commands going to be pushed through now there's we're actually as you can see we actually call rest and we also call roots now these aren't currently imported inside of our ready function so what we need to do is you know we can go to our box because we no longer need these two here we can copy these cut them back up and then go in here and paste them in here we also are going to need to require dot ianfee and we're going to have to call config because we also use process.ian fee in here so you can see we'll just bring through our requirements everything we need and we're just bringing through the same command we used before now we don't actually have to change anything inside of here it should all just work fine but we are going to have to now go and set up something to run this inside of our bot.js kind of like how we do our commands here we're going to want to create a we're going to want to we basically want to loop through all of the events so underneath our command files i am going to call const event files is equal to and let's just do this i think that should be good so we're going to say fs uh let's break this down it makes it easier to read and then filter and that's good so that's going to get all our effects there so we're just saying read do sync so we're going to read this directory we're going to filter through all the files and basically just get all the ones with dot js and then we're going to store them in this variable there we go so that is now set we then need to go for const file of defense files so we've got to loop through all of these now we're going to get the event uh let's just remove that because that's not that's not true we need to get const event it's equal to require offense file and that's going to get us the actual file we need so it's going to root for them it's going to see ready and it's basically just going to get us to ready fm now we're going to say if event dot once so if it's a parameter if it has a param or a a value of once we're basically just going to call this we're going to say client.once and then we're going to say offense.name and then we're going to say why'd you go do an arrow function and we're going to do dot args so this is going to gaffer all of the arguments we need and then we're going to say event dot execute and then we'll pass through uh dot dot args and then also commands so args will always gather up to what we need and cause this is using klein it should also get the client for us inside of our arguments and then we're going to call commands which it's got from up here our little const commands here which is also used in the ready process passing it through the um rest so if we come back here that should now work for the event but we also need to cater for when we're not using it and basically that is it there that is the exact same command so if we need any more events or arguments we will just pass some fruit here but most of the default ones should come through from when we use client it actually it checks what the default ones need so for example ready it's like it needs to client and that's so forth so it brings it all through so we should be good um and then that is basically it for this one so if we save now and we go back here ghost is online that's a good sign now hopefully we get successfully registered commands locally and there you go successfully registered commands locally so if we go back to our discord let's just see if ping still works and there you go it does work but that's it's still not done yet so what we need let's just close this down we need to actually move interaction create into its own file so let's create a new file call it interaction create dot js and then we can basically go into already copy this first bit here paste this in and then do this break that down come down there and break that down but instead of using um client we actually just and command we actually just need interactions like or interaction like this because that's all the um all our method takes it's an interaction you can see here but you can see it also takes an asynchronous function so what we need to do is make sure this is a synchronous when we call it now if we come back here we can copy everything inside of here just cut it straight out go into our interaction create and we're just going to paste it in there save our bot.js we can just remove this whole little bit here now we don't need that and this should be our bot created so that's done in there let's just check if there's any errors which there is what is the error this command is not a function so interaction is commands not function that means this is coming through as undefined which means we have may or may not have a slight issue let's figure what that is okay guys i don't want to tell you how long it took me to find out this bug it was so simple and i thought you know it could be this it could be that turns out we just forgot to rename this to interaction create just like that hit save and now that should work now we're going to have enough at issue here but i'll show you what happens once we try and run it let's go back here let's just say ping and we get an error so let's go figure out what that error is you can see it says here uh client is not defined in client dot commands and that's because we can actually get client from interaction dot client just like that so we get the interaction we say interaction dot client dot commands dot get and then we get that uh we will basically work so we just need to set up so now if we save what's our next error so let's see ghost is online we should be able to just run ping now pong and there we go that seemed to have worked correctly and nothing seemed to have errored okay guys so that is everything we need to do for today we now have an offen handler so any offense we now create if we want to do a message offense we can check for messages status that they've reacted anything like that we can do that inside the events um page here and create a new event for it to execute which is absolutely awesome so guys thank you all for tuning into this video if you enjoyed it do not forget to leave a thumbs up smash that subscribe button and don't forget to leave any comments or feedback you have below also if you want to support the channel even further you can do so by going below this video seeing a little join by clicking it and having a look at the different options you have to become a member of the channel becoming a member channel gives you extra perks and other cool little features so guys check that out if you want to support a channel for fit i really do appreciate but for now thank you all for watching this video and peace out [Music] you
Info
Channel: Tyler Potts
Views: 1,136
Rating: undefined out of 5
Keywords: webdev, javascript, css, sass, scss, js, vuejs, vue, reactjs, website development, website design, app dev, app, app design, app development, mobile app, native, web developer, app developer, developer, programmer
Id: RbVXxVReCNc
Channel Id: undefined
Length: 10min 30sec (630 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.