How to properly structure commands (directories)

Hi! I just started making my first ever discord bot with discord.js. I also dont have much prior TS/JS experience (I am using TS) but do have a couple years of programming history, so im not a noob! I am a pretty big fan of having alot of files with little content in them. Preferably, I would want to have one command per file. This works out pretty well, until I want to make a command group, which by design kind of gets ugly to pick apart. Would anyone happen to have a link to a sample repository or some other form of guidance how to set this up the proper way? Im sure theres a bunch of magic that can be done with the module exports and transpiling and whatnot. Appreciate any help on this!
9 Replies
d.js toolkit
d.js toolkit5mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
d.js docs
d.js docs5mo ago
:guide: Creating Your Bot: Command handling read more :guide: Creating Your Bot: Event handling read more
Kenny
KennyOP5mo ago
As far as I can tell, this doesnt account for splitting apart subcommands, does it?
Amgelo
Amgelo5mo ago
you could make subcommand functions in different files and call them from the parent command unless you want to do it automatically?
Kenny
KennyOP5mo ago
Yeah, I guess that would be a step in the right direction that is simple enough to do. I guess I could also declare the sub command builder in the "subfiles" and then just plug it into the main command builder Doing that automatically would be pretty nice, but I would be surprised if there was tools for that!
Amgelo
Amgelo5mo ago
there isn't something in djs itself, since it's not a framework, it lets you organize your code however you'd like there are multiple command frameworks people have made though
Kenny
KennyOP5mo ago
I think ill just go with manually putting the stuff in the sub files and then plugging it all into the parent command in something like an index.ts for each command group folder I guess That will keep it fairly simple and pretty much achieve my organization goals I was just checking to see whether there was a more suitable, immediate solution
Amgelo
Amgelo5mo ago
you could edit the command handler to also load subcommands and then call the appropriate one if the interaction has a subcommand, or you can use a framework that'd manage everything for you, but you'd probably need to redo a lot of code
Kenny
KennyOP5mo ago
Ive literally got like 2.5 commands atm, so editing stuff is no big deal really I get what you mean though, yeah Means every subcommand file would need to have its own "execute" method then, just like the command group files do and the interactionCreate event, I would need to take apart the interaction, see if theres a subcommand being called, then route to that subcommand file and call execute there Sounds pretty doable.

Did you find this page helpful?