Command handler change
Would it be possible (is there an example) of a command handler, that looks into a folder with all of the commands, but instead, if there are subcommands, you don't handle them with like switch case statements for example, but you have a folder, and all of the subcommands have their own modules (so you can work easier and not having to scroll through god know how long of a single module to get to a specific subcommand's code.
19 Replies
- 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!what do you mean
nevermind, i'll try to work on it, you gave me an idea
coming back to this, how would i edit the deploy-commands.js file in order to make it do the following
- read all the files inside commands/utility as, obviously, commands
- read all files in any potential subdirectories within the utility directory, and it would automatically know that if the filetype that it's reading is a directory, that directory name is the slash command name and any modules inside it are just subcommands
My current deploy-commands.js looks like this
This would not be a djs question, #other-js-ts
oop
Hmm, tbh their question was how to treat directories differently so to me it sounded non-djs
okay so what i've figured out is that the for (const folder of commandFolders) loop basically takes command files from within any folders inside of the commands folder (im assuming this is so you can label the command types? like dev commands, mod commands etc.)
now, do i need to make another for of loop inside of that to check if there is another directory, and make a slashcommandbuilder... inside of deploy-commands.js?
just want to make sure i understood you right before i go on a ghost hunt
how can i check if soemthing is a directory though? there's no isDirectory or anything similar
or none that i can see at least
Like, unless I change it from
to
I can't do it, but if I do this, will it change anything?
If you are using
statSync
you can do commandFiles.isDirectory()
to check if it is a directoryyes but then the other for loop errors
Errors in what way?
Because it isn't, with what you showed there, it is
fs.Stats
which is a class, you need to check if it's a directory inside the loopYou should still be using
readdirSync
to read all the filesoh do you want me to have both fs.readdirSync and fs.statSync in the same loop?
I don't understand what you mean in the same loop, you get all the files by doing
const commandFiles = fs.readdirSync(path)
Then loop through commandFiles and inside it use statSync and check if the file is a directory and do what you want to do with it
Basically the same code as here, but remove the filter for .js
and in your for..of loop, check if filePath
is a directory using statSync
So something like this?
Well yeah, but you can't redefine a variable, you already have a
commandFiles
name it something else, you'd check if it's a directory and handle accordinglyright, but how can i make it recognize the folder as a "command", and what should I put inside of the data field in module.exports for the subcommands?
How would I "handle" it if it is a directory
Do I just put the slashcommandbuilder in deploy-commands instead and just have the subcommands be formatted as JSON (removing the .toJSON() when pushing subcommands)
It'd be hard to determine which directory is for base command and which is for subcommands unless you add some kind of identifier in the directory name, like you can name it "subcommand" and check the directory name if it is that, and treat is as such, you can make it a json, that would hold the subcommand name, and execute function that would handle its interaction, you can load it to client like you do with slash commands, and on interaction, get the subcommand with
interaction.options.getSubcommand()
and then get it from the collection and call execute, it'll be very similar to how you handle slash commands, just the intial steps is what you need to figure out