Handle Subcommands in SubcommandGroups

Hello, I'm working with SubcommandGroups atm and I'm trying to figure out how I can handle SubcommandGoups aswell. The issue is that I'm loading my commands and Subcommands into collections as every bot tutorial tells you to. Now I have client.slashCommands and client.subcommands as collections for my commands. Now, how do I properly manage subcommandgroups in there as well so that it's still clear to what subcommand they belong? Does it make sense to store them as an object? Like
client.commands = {}
// Loading slash commands
fs.readdirSync("./slashCommands")
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading slash command ${file}...`);
let pull = require(`./slashCommands/${file}`);
client.commands[pull.name] = pull;
console.log("Loaded!");
});



// Loading subcommands
fs.readdirSync("./slashCommands/subCommands")
.forEach(dir => {
fs.readdirSync(`./slashCommands/subCommands/${dir}/`)
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading ${dir} subcommand ${file}...`);
let pull = require(`./slashCommands/subCommands/${dir}/${file}`);
client.commands[dir][pull.name] = pull;
console.log("Loaded!");
});
});
client.commands = {}
// Loading slash commands
fs.readdirSync("./slashCommands")
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading slash command ${file}...`);
let pull = require(`./slashCommands/${file}`);
client.commands[pull.name] = pull;
console.log("Loaded!");
});



// Loading subcommands
fs.readdirSync("./slashCommands/subCommands")
.forEach(dir => {
fs.readdirSync(`./slashCommands/subCommands/${dir}/`)
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading ${dir} subcommand ${file}...`);
let pull = require(`./slashCommands/subCommands/${dir}/${file}`);
client.commands[dir][pull.name] = pull;
console.log("Loaded!");
});
});
4 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ncls.
ncls.OP3y ago
Yeah, I watched a video and he did it like that. That's why I changed it in the code I put there but I don't know if it's good. I generally have the feeling that especially this part of my code is a huge pile of sh!t That's everything I found. Every tutorial I watched or read that I can remember had it that way. I don't really know how to do it better, that's why I came here Do you have some resources that tell me how to that best? But the tutorial stored them in different collections. I'm constantly trying to come up with something but it always results in my wanting to nest collections and struggling on how to refer from the "lower level" to the "higher level"... I don't know what different layout I should use. I looked up a couple more tutorials now and all of them create different collections for their main commands and subcommands
d.js docs
d.js docs3y ago
guide Slash Commands: Subcommands read more
ncls.
ncls.OP3y ago
This is a crappy solution I came up with now. Does it look somewhat acceptable?
client.commands = {};

fs.readdirSync("./slashCommands")
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading slash command ${file}...`);
let pull = require(`./slashCommands/${file}`);
client.commands[pull.name] = pull;
console.log("Loaded!");
});

fs.readdirSync("./slashCommands")
.forEach(dir => {
if (fs.lstatSync(`./slashCommands/${dir}`).isDirectory()) {
fs.readdirSync(`./slashCommands/${dir}`)
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading ${dir} subcommand ${file}...`);
let pull = require(`./slashCommands/${dir}/${file}`);
client.commands[dir][pull.name] = pull;
console.log("Loaded!");
});
}
});

fs.readdirSync("./slashCommands")
.forEach(dir => {
if (fs.lstatSync(`./slashCommands/${dir}`).isDirectory()) {
fs.readdirSync(`./slashCommands/${dir}`)
.forEach(subDir => {
if (fs.lstatSync(`./slashCommands/${dir}/${subDir}`).isDirectory()) {
client.commands[dir][subDir] = {};
fs.readdirSync(`./slashCommands/${dir}/${subDir}`)
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading ${dir} subcommandGroup ${subDir} subcommand ${file}...`);
let pull = require(`./slashCommands/${dir}/${subDir}/${file}`);
client.commands[dir][subDir][pull.name] = pull;
console.log("Loaded!");
});
}
})
}
});
client.commands = {};

fs.readdirSync("./slashCommands")
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading slash command ${file}...`);
let pull = require(`./slashCommands/${file}`);
client.commands[pull.name] = pull;
console.log("Loaded!");
});

fs.readdirSync("./slashCommands")
.forEach(dir => {
if (fs.lstatSync(`./slashCommands/${dir}`).isDirectory()) {
fs.readdirSync(`./slashCommands/${dir}`)
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading ${dir} subcommand ${file}...`);
let pull = require(`./slashCommands/${dir}/${file}`);
client.commands[dir][pull.name] = pull;
console.log("Loaded!");
});
}
});

fs.readdirSync("./slashCommands")
.forEach(dir => {
if (fs.lstatSync(`./slashCommands/${dir}`).isDirectory()) {
fs.readdirSync(`./slashCommands/${dir}`)
.forEach(subDir => {
if (fs.lstatSync(`./slashCommands/${dir}/${subDir}`).isDirectory()) {
client.commands[dir][subDir] = {};
fs.readdirSync(`./slashCommands/${dir}/${subDir}`)
.filter(file => file.endsWith(".js"))
.forEach(file => {
console.log(`Loading ${dir} subcommandGroup ${subDir} subcommand ${file}...`);
let pull = require(`./slashCommands/${dir}/${subDir}/${file}`);
client.commands[dir][subDir][pull.name] = pull;
console.log("Loaded!");
});
}
})
}
});
Already had that in mind and I could add a subcommand property where I throw them in, but I mean the general thought of storing and nesting them in an object like this. Is this good to do or does it come with issues? So far it solves my problem
Want results from more Discord servers?
Add your server