Advanced Command + Event Handler (Discord.js v14)

Sdílet
Vložit
  • čas přidán 7. 01. 2023
  • Advanced Command + Event Handler (Discord.js v14)
    IMPORTANT: In 28:49 and 30:02 change the "break" to "return".
    Reason: Using break will only half work as it won't reply if there's more than one permission. However it will still run the rest of the function which means it'll attempt to still run the command at the end of the function. Return will make sure the function stops completely. Sorry for the inconvenience.
    🌐 Important links:
    areCommandsDifferent.js code: github.com/notunderctrl/disco...
    Discord server: discord.underctrl.io
    Source code: github.com/notunderctrl/disco...
    Discord.js series playlist: • Discord.js v14 Tutorial
    Discord.js official website: discord.js.org
    Patreon: / underctrl
    Buy me a coffee (one-time donation): buymeacoffee.com/underctrl
    Thank you for watching :)

Komentáře • 116

  • @ryanhmd8305
    @ryanhmd8305 Před rokem +18

    I'm a noob and the video seemed rather complicated to me on first viewing, but thanks to the github link you provided I was able to study your code and I finally understood!
    Everything is organized with great intelligence, I've learned a lot thank you! You really have a gift for explaining things so I support you, keep it that way.

  • @GuadoDex
    @GuadoDex Před rokem +5

    I always found command handling very complicated with all that file structure and links between files.
    Your video helped me understand much easier than other tutorials I've seen, thank you very much for this very advanced file structure

  • @kenqz2623
    @kenqz2623 Před rokem +13

    Amazing tutorial , covers the best practices of folder structure , code refactoring into seperate files and the most importantly the code is up to date and it works. Really easy to follow and learn not just copy paste the code

  • @UnderCtrl
    @UnderCtrl  Před rokem +9

    New commands + event handler:
    czcams.com/video/qmuPOimscPs/video.html
    IMPORTANT: In 28:49 and 30:02 change the "break" to "return".
    Reason: Using break will only half work as it won't reply if there's more than one permission. However it will still run the rest of the function which means it'll attempt to still run the command at the end of the function. Return will make sure the function stops completely. Sorry for the inconvenience.
    This change has already been reflected on the github repository

  • @constellation007
    @constellation007 Před 7 měsíci +1

    what a comprehensive video and such a powerful command and event handler im surprised the video was only a little over 30 mins... u do such a great job at explaining everything step by step, i appreciate that. Pretty advanced tutorial, I have programming experience so I was able to follow pretty well but for some who dont have much experience, i think explaining the overall high level purpose of functions we're creating would help everyone understand and put everything together easier. Besides that great job! Keep it up!

  • @KrowplexMusicHD
    @KrowplexMusicHD Před 11 měsíci +7

    I think you were a bit too fast at times, I often had to pause and go back few seconds to finalize what I was typing.
    But overall I'm glad I finally found a video that explains this structure to me and how to do it. I'm new to nodejs/discordjs and I found this helpful and interesting.

    • @UnderCtrl
      @UnderCtrl  Před 11 měsíci +5

      Sorry about that. Unfortunately the code for this video was quite a lot which is why I had to move a little faster than usual.

  • @SheeeeshBoii
    @SheeeeshBoii Před 9 měsíci +2

    Ignore the hate, this channel has the best possible tutorials! The people hating just do NOT care about JS and skip it and come here to spread hate.

  • @DamienWillis-pu9bm
    @DamienWillis-pu9bm Před 9 měsíci

    Great content dude. I lost motivation to use JavaScript, but this rekindled that fire in me!

  • @Multi1Vids
    @Multi1Vids Před 5 měsíci +1

    took me literally 4 days to understand this code, but i finally get it and to be honest, this code has many use cases in all types of javascript programming. Very good video thanks man

  • @skyonix4922
    @skyonix4922 Před rokem +1

    The best Video i have ever watched about command handling thank you!

  • @furrypotato
    @furrypotato Před rokem +3

    A great, and very intense vid. So much packed in that it will take a few views to understand it properly.
    My only request would be a vid on making a version of this that can work on multiple servers rather than just one.

    • @UnderCtrl
      @UnderCtrl  Před rokem +3

      To register commands globally just remove the guild id from config.json

    • @furrypotato
      @furrypotato Před rokem

      @@UnderCtrl Ah ok that's remarkable simple then.
      This will form the basis of my upcoming bots so thanks again.

  • @atalayio
    @atalayio Před měsícem +1

    This is best file structure for discord.js. Nice video, helped me a lot! Thanks!

  • @GulaManYOW
    @GulaManYOW Před 9 měsíci

    I'm lost at Javascript actually, I don't know what to create and I thought why not create a Discord Bot (Game). Stumb led upon this great tutorial, and understand the logic of EventHandler and some Advanced path creating techniques, thanks man!

  • @XStarReviews
    @XStarReviews Před 6 měsíci

    I personally enjoyed this video and learned a lot from it because I took the time to research the parts I didn't understand while watching this video. Great work and appreciate all the time you put into this one!
    Additionally, for anyone that ran into this following error: There was an error: *"TypeError: Cannot read properties of undefined (reading 'fetch')"*
    Solution for me was to make sure my "testServer" value in your config.json is a string. For example, my config.json file looks like this right now (I changed the numbers so they don't match with what's in my file):
    {
    "testServer": "1234580157137123456",
    "devs": "123451850396123456"
    }

  • @omarezeldin4620
    @omarezeldin4620 Před 9 měsíci

    Thanks a lot for this series, seems that you are the only one that explains discord.js v14 so perfect. Also the Github repo is very useful. I tried to learn file handling from the documentation but it was complicated. This video got the point 🎯. Also please make a video to *host our bot online 24/7* for free. I think it also would be great to find any way other than Replit. I failed to make it work anyways. Good Luck.

    • @UnderCtrl
      @UnderCtrl  Před 9 měsíci +1

      Thanks for the kind words!
      I have a tutorial on bot hosting as well czcams.com/video/f7KjJQVcogY/video.html

    • @omarezeldin4620
      @omarezeldin4620 Před 9 měsíci

      @@UnderCtrl Thanks a lot, definitely will watch it

  • @HuebertEllowit-LOL
    @HuebertEllowit-LOL Před 4 měsíci +1

    So when i run this code everything seems to work fine, and when I add a new .js file in the misc or moderation folder and run my program, I can get it to say the new command has been registered. But when i go to discord, no new command option ever pops up. I just have ping and ban and thats it. I have been working on this for such a long time and am totally lost on why it says its registered but never becomes an option to be ran. Any help would be hugely appreciated!!!

  • @BlueShiYTplus
    @BlueShiYTplus Před rokem +2

    Help a ton!

  • @mh17239
    @mh17239 Před 11 měsíci

    Is there a reason your using the intent bit flag and not the gateway intent bit?

  • @JHSAccount-qp4dt
    @JHSAccount-qp4dt Před měsícem

    hey @UnderCtrl ,I have been following your tutorial (God bless you! thank you!) and my eventListener is not working like yours on 08:50; can you help me what possibly went wrong??

  • @mdx9461
    @mdx9461 Před 5 měsíci +1

    hey bro i've done everything in the video currectly , bot in running and its okay but the bot is not responding to the commands can you help me ?

  • @JefferyPlayz42
    @JefferyPlayz42 Před 11 měsíci +1

    This is not registering the commands golbaly on my other servers. Is this code meant to do that? If so any help would be amazing!

  • @paesafps
    @paesafps Před 11 měsíci

    so I am having an issue with this where its making all of my commands to only work in my personal server and not other servers where i put the bot, how do i fix that

  • @ayberkgezer7488
    @ayberkgezer7488 Před 10 měsíci

    hi i need delete commands how can I do? I made the deleted part true but In the console it says this: Skipping registering command "checkprice" as it's set to delete. Even though I deleted the js file, it shows up in dc

  • @xander9483
    @xander9483 Před 8 měsíci

    nice tutorial! There's a small issue with the event handler though, as it assumes you're only passing in a single argument along with client. This will give issues when your events have multiple parameters. To fix this make sure you pass in all arguments (...args) instead of just one.

  • @sharko_000
    @sharko_000 Před 5 měsíci +1

    what theme did you use?

  • @lovableyapper11
    @lovableyapper11 Před rokem +1

    how to fix this?
    There was ann error: ReferenceError: commandObject is not defined
    i checked every single files, everything works fine except for that error. Btw my bot still online.

  • @yetntplayz
    @yetntplayz Před rokem

    Hey, will this work for sub-directories and how do i make all my commands visible in every server, how do i decide which commands i want in which guild and which i want to be global? (i want all of them global tho, lmao)

  • @kwutaa5534
    @kwutaa5534 Před rokem

    I got TypeError: eventFunction is not a function and idk how to fix it, I litteraly do everything on ur vid and still not working, any help plzzzz

  • @giorgostzitziesvili7676
    @giorgostzitziesvili7676 Před 7 měsíci

    BAN command is not working, it say " Cannot read properties of undefined (reading 'has') "

  • @Johnny77718
    @Johnny77718 Před rokem

    for some reason, when I try to run it, everything works, except for ban.js and ping.js. It says Error: File C:\Users\PC\Desktop\hmm\TestBot\src\commands\moderation\ban.js must export "data".'. Can someone help me please? :(

  • @Vellutia
    @Vellutia Před rokem

    hey great vid, but I have a thought on how to create some sort of internal timeout for a command, for user level timeout (individual using same command) and server level timeout(anyone in the server using the command). or maybe a global level timeout? haha not sure what would be the usecase for this one tho

  • @TheSvess
    @TheSvess Před rokem

    how can I add subcommands using this type of command handler?

  • @Ryuji-Ly
    @Ryuji-Ly Před 9 měsíci

    Is it possible to use subcommands using this command handler?

  • @_-dimarques-_4433
    @_-dimarques-_4433 Před 5 měsíci

    Hi how are you doing? I have a question how can I make it multi server????, this was is just for the test server. TY for the incredible video

  • @henriquevmoraes
    @henriquevmoraes Před rokem

    What are all your relevant VS extentions? like when you type a function and it automatically requires the file, and the one that autocomplete things, like ";", etc...

    • @UnderCtrl
      @UnderCtrl  Před rokem +1

      The code formatter is responsible for adding the ; and it's called "Prettier".
      The auto complete/import is a built in feature in vscode.

  • @mitake-san_
    @mitake-san_ Před 5 měsíci

    I followed correctly but for the file path it returns me empty [ ]

  • @syaidinaarafhan9084
    @syaidinaarafhan9084 Před 9 měsíci

    hello can u help me why my console log not showing anything

  • @mudpill7509
    @mudpill7509 Před rokem +4

    I'm not sure if it is a Windows file permission issue or something but for whatever reason my console is writing out the arrays to be empty in VS Code. It's still recognizing that there is two elements in fileNames[] but both of those elements are empty and just write [][] to the console.
    EDIT: Must have been something on my end because I pulled your git and it worked fine. Thanks.

    • @genshinryuga
      @genshinryuga Před rokem

      Hey how did you fix that I am having the same issue

    • @muriilouwu
      @muriilouwu Před rokem +2

      ​@@genshinryuga the problem is probably caused because you missed the if, else brackets in the getAllFiles function

    • @voreal8027
      @voreal8027 Před 11 měsíci

      @@muriilouwu Yep, that was the issue for me! Thanks

  • @spotstudies
    @spotstudies Před rokem

    What does
    // devOnly: Boolean,
    // testOnly: Boolean,
    mean?

  • @devanshupadhyay2658
    @devanshupadhyay2658 Před rokem

    this is Goldddd

  • @Umarbit
    @Umarbit Před rokem +1

    Hard tutorial. Everything was going right in the beginning but as I followed along my brain got confused with so many files and folders as it was the first time I used too many files and folders. Do all professional developers use separate files for separate functions? I also faced a problem. VScode autocomplete suggestion feature was working less when working in separate files. How to fix it?

    • @enesctak6994
      @enesctak6994 Před rokem +1

      I'm not a professional, but i can say that if the commands seperated to the different files, looks better than one file. Basically you don't have to search tons of code when you want to edit something.

  • @Bolbon
    @Bolbon Před 3 měsíci

    Saved me 🎉❤😅

  • @god182br
    @god182br Před 9 měsíci

    Great content!
    Using this organization to register commands, i've done a lot things!
    btw on register commands, we have a testserver on config.json, i'm testing on multiple servers but if a server isnt on config.json testServer, the command doesnt register on differentes servers.
    Any idea about it?

    • @UnderCtrl
      @UnderCtrl  Před 9 měsíci +1

      If you remove the testServer, it should globally register the commands.

  • @Itz_Shade
    @Itz_Shade Před rokem

    Cannot access path before initialization

  • @ZSMacLean
    @ZSMacLean Před 11 měsíci +1

    Okay, so I'm at the 13:50 mark, and for some reason the app is crashing. According to debug it's in the eventHandler.js line 15 "const eventFunction = require(eventFile);" I've rechecked every file multiple times for errors and I can't seem to figure out why it's crashing. Everything up to this point was working just fine.

    • @saidrexxx
      @saidrexxx Před 11 měsíci +1

      Same error dude, Could you solve it?
      Edit: I have solved the problem, check that the codes of the "events" folder exporting the function, check the syntax of "module.exports"

    • @ZSMacLean
      @ZSMacLean Před 11 měsíci

      @@saidrexxx I ended up copying the files from github, and it at first still didn't work, then I discovered a .DS file and deleted that... seems to have fixed the problem.

  • @TwelveGaug3
    @TwelveGaug3 Před 4 měsíci

    Great vids!! I've been following the tutorials gotta say this one is packed, I was stuck a few times with some errors (I'm only just learning JavaScript) but always found something I missed that caused the error.....except now I've been stuck on a ""DiscordAPIError[10004]: Unknown Guild"" for the past few days at 24:31 the console doesn't return the guild commands it just throws that error (If I remove "testServer" it returns the commands as it should.) has anyone ran into this issue or have an idea about where I messed up??

  • @isseyyy69
    @isseyyy69 Před rokem

    your VSC theme is beautiful. name?

  • @justimagine.23
    @justimagine.23 Před 3 měsíci

    **Is there a command that I can only share pictures or videos with? Is there a coding that I can use to jump into a bot? I really need it.**
    .
    ** If so can you help? about how it's done. Thank you.**

  • @JJayradd
    @JJayradd Před 10 měsíci

    Great video but I foolishly decided to make my project a module project. This left issues with `__dirname` but I guess that is 100% my fault. I am curious why you don't use modules instead?

    • @UnderCtrl
      @UnderCtrl  Před 10 měsíci

      No specific reason tbh. I just didn't bother changing.

    • @JJayradd
      @JJayradd Před 10 měsíci

      @@UnderCtrl oh awesome. I ended up having to change things around but it's been a fun hurdle.

  • @Dexify_911
    @Dexify_911 Před rokem

    nice!

  • @henori6035
    @henori6035 Před 11 měsíci

    Can u give me all your files ALL in One all pack when u did in this video ? because my bot doesnt work with my command: ping ;> ??

  • @aritraghosh9647
    @aritraghosh9647 Před rokem

    There was an error: DiscordAPIError[30034]: Max number of daily application command creates has been reached (200) running ur code gave me this error what should I do?

    • @UnderCtrl
      @UnderCtrl  Před rokem

      There’s an error going on with the discord api at the moment. All you can do it wait until they fix it.

  • @EmmaDuchess
    @EmmaDuchess Před rokem +1

    I keep getting stuck around 5-6 minutes with an error telling me ReferenceError: getAllFiles is not defined when I'm attempting to run the node src/index.js command in the Terminal
    Edit: I've followed every single step as they come, so I'm confused why it wouldn't work.
    Edit2: I've decided to just go back to previous videos and watch every single one in order since you might've asked us to do something that I haven't done yet etc. Tho you could try to explain the solution if you know it, for others that might be on the same halt as me.

    • @mariesaoconnell7281
      @mariesaoconnell7281 Před rokem +1

      Be sure to import the function ☺
      I think he has an extension that auto imports it automatically when he references it.

    • @EmmaDuchess
      @EmmaDuchess Před 11 měsíci

      @@mariesaoconnell7281 Thx for the tip

  • @schlumpfu
    @schlumpfu Před 11 měsíci +1

    Im getting this error There was an error: DiscordAPIError[50035]: Invalid Form Body
    name[BASE_TYPE_REQUIRED]: This field is required

    • @oconr
      @oconr Před 11 měsíci +1

      I had this issue because I had a blank file somewhere in the commands directory that wasn't providing the values of name, description, options

    • @satyyaam
      @satyyaam Před 5 měsíci

      Solved 👍

  • @shapa1027
    @shapa1027 Před 8 měsíci +1

    after going through this video, I followed along step by step, but encountered an error
    I then copy and pasted from you github source and I'm still receiving this error:
    node:events:489
    throw er; // Unhandled 'error' event
    ^
    TypeError: eventFunction is not a function
    at Client. (C:\Users
    yans\Desktop\ShapaBot\src\handlers\eventHandler.js:16:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
    any help would be amazing

    • @highoncatnip_
      @highoncatnip_ Před 8 měsíci +1

      mine was working but just started throwing this error fsr
      EDIT: MAKE SURE YOU HAVE THE PROPER MODULE.EXPORTS CODE IN ANY NEW FILES YOU CREATE OR ELSE IT *WILL* THROW THIS ERROR

    • @josuehernandezmontero6679
      @josuehernandezmontero6679 Před 7 měsíci

      Have you solved the problem?

    • @ipn.d
      @ipn.d Před 22 dny

      @@highoncatnip_ TYSM i had this problem
      i wrote "module.export" instead of "module.exports" and i get the problem

  • @ArfaWang
    @ArfaWang Před 3 měsíci

    how to delete messages in channel?

  • @nicholaswalker1831
    @nicholaswalker1831 Před 11 měsíci +1

    On Client.MessageUpdate you have two arguments, oldMessage and newMessage.
    This method only supports one argument from the event, so my event function would get only client and oldMessage.
    Solved it like this:
    client.on(eventName, async (...args) => {
    for (const eventFile of eventFiles) {
    const eventFunction = require(eventFile);
    await eventFunction(client, ...args);
    }
    });

    • @jcosmick
      @jcosmick Před 8 měsíci

      thanks you are a king

  • @SreekalaS-cj9bs
    @SreekalaS-cj9bs Před rokem +1

    broo i cant create a folder in events? it is showing events/ready

    • @mariesaoconnell7281
      @mariesaoconnell7281 Před rokem

      VS Code is weird, so if the only thing inside of 'events' is 'ready' then it auto shows the content of 'ready'. You should be able to right click 'events' and create another folder.
      If not it may be easier to use your console. 'cd' into 'events' and use the command 'mkdir '

    • @Sh4ky
      @Sh4ky Před 11 měsíci

      bro Im literally having the exact same issue, I think it's messing with the filepath I made in eventHandler

  • @leonfedorenko5231
    @leonfedorenko5231 Před 2 měsíci

    How to fix: ',' expenced

  • @daisukasa
    @daisukasa Před 7 měsíci

    hello, i really like your videos but im stuck on an error when i try to "node src/bot.js" it says "ReferenceError: Cannot access 'files' before initialization" i still dont understand what do i do wrong im still looking out, i would like to fix it if you could help me

    • @daisukasa
      @daisukasa Před 7 měsíci

      its the 05:46 part

    • @daisukasa
      @daisukasa Před 5 měsíci

      i managed to solve the problem myself

    • @daisukasa
      @daisukasa Před 5 měsíci

      im stuck again...

    • @daisukasa
      @daisukasa Před 5 měsíci

      i cant register my commands it gives error

  • @rashmysajin4520
    @rashmysajin4520 Před rokem

    Whats your discordjs upload schedule

    • @UnderCtrl
      @UnderCtrl  Před rokem

      Hi, currently very unorganised since I’m adjusting between my college and even the JavaScript course I’m working on. I’ll most likely set one up by next week. You can keep an eye out on my discord.
      discord.underctrl.io

  • @nicooop656
    @nicooop656 Před rokem

    Why am I getting an TypeError: eventFunction is not a function even if I followed the vid correctly?

    • @UnderCtrl
      @UnderCtrl  Před rokem +3

      It’s because one of your event files is not exporting a function by default.

    • @xavia7933
      @xavia7933 Před rokem

      @@UnderCtrl is there a solution?

  • @ahmedsn914
    @ahmedsn914 Před rokem +1

    Please explain basics like
    Const channel = interaction.guild.roles.cach.get("")
    Like these basics
    Please explain them

    • @petergg9096
      @petergg9096 Před rokem

      What do you want to know?

    • @Sh4ky
      @Sh4ky Před 11 měsíci

      Just use GPT bro. Also, I recommend commenting out every piece of code you do so you can quickly understand it. This is from GPT:
      const channel = interaction.guild.roles.cache.get("");
      Let's break it down:
      interaction refers to the interaction object, which represents an interaction with a Discord message or component.
      guild is a property of the interaction object that refers to the guild (server) where the interaction occurred.
      roles is a property of the guild object that represents the collection of roles in the guild.
      cache is a property of the roles object that contains a cached collection of roles for efficient access.
      get() is a method provided by the cache property to retrieve a role from the cache based on its ID.
      In this specific code snippet, interaction.guild.roles.cache.get("") attempts to retrieve a role from the cache based on an empty string as the role ID. However, since an empty string is not a valid role ID, it will likely return undefined or an error.
      To utilize this code correctly, you need to provide a valid role ID inside the get() method to retrieve the corresponding role from the cache.

  • @ongidc
    @ongidc Před 17 dny +1

    19:50

  • @ssamyyy
    @ssamyyy Před 9 měsíci

    watchted till starting of ping.js at 14:38

  • @remaxio
    @remaxio Před 10 měsíci

    bro i repeat the video 3 times and i still have problems understanding it hhh

  • @gyrthez246
    @gyrthez246 Před 5 měsíci +1

    I understood the video was going to be quicker, but I think it was way too rushed to be able to understand anything. I don't feel that I actually learned anything during this tutorial other than copy pasting the code you wrote, which could have been done from github in a few minutes tops anyways.
    There was no real explanation of what a lot of these components do or why we're setting it up this way.

    • @UnderCtrl
      @UnderCtrl  Před 5 měsíci +1

      Fair enough. I watched this video again and I agree with you 100%. I think I was trying to hard to make the video as short as possible. I’ll most likely be doing an updated version of this. Thanks a lot for the feedback 🙏

  • @LordClassyMoose
    @LordClassyMoose Před 9 měsíci

    great step-by-step tutorial, horrible explanation, had to use chatGPT to fix the errors.

    • @UnderCtrl
      @UnderCtrl  Před 9 měsíci

      Sorry to hear that. I did mention the video was going to be fast paced to save time. Thanks for the feedback however. I'm planning on a newer version of this video.

  • @raphz6256
    @raphz6256 Před 10 měsíci

    Not gonna lie, this was one of the worst tutorials I've seen in a while. Your other tutorials have been great, but this one doesn't explain anything and it cuts parts of you pasting code without explaining what changed.

    • @UnderCtrl
      @UnderCtrl  Před 10 měsíci

      Fair enough. As I mentioned at the start of the video there was a ton to cover so I hoped viewers at least had some basic understanding of JavaScript (which is a requirement for this series) so that I don't have to explain every little detail.

  • @KrowplexMusicHD
    @KrowplexMusicHD Před 11 měsíci +1

    I'm trying to work with the "threadUpdate" event which has 2 arguments instead of one. The Old Thread and the New Thread. I found a work around by saying if its the "threadUpdate" event, pass 2 arguments. Is there a way to make it work no matter how many arguments? I tried with an array but I kept getting an iterator error.
    module.exports = async (client, oldThread, newThread) => {
    try {
    if (oldThread.parent.type === ChannelType.GuildForum) {
    console.log("Old Thread Updated!");
    }
    if (newThread.parent.type === ChannelType.GuildForum) {
    console.log("New Thread Updated!");
    }
    } catch (error) {
    console.log(`There was an error running this command: ${error}`)
    }
    };

    • @UnderCtrl
      @UnderCtrl  Před 11 měsíci

      Hey please join my server and I’ll help from there. discord.underctrl.io

  • @ThotsAndPrayers
    @ThotsAndPrayers Před 8 měsíci

    if youre not using typescript in 2023 what are you even doing? also this file structure is so needlessly over-complicated.

    • @UnderCtrl
      @UnderCtrl  Před 8 měsíci +4

      This file structure is one of the most common ones when it comes to Discord.js bots. And I did introduce TypeScript but most of my viewers just prefer regular JS.

    • @Konataware
      @Konataware Před 6 měsíci

      that aged like milk