#How to incorporate subfolders into a command handler?

3 messages · Page 1 of 1 (latest)

bronze gorge
#

I'd like to organize my command files within the commands folder because they're getting messy. I want to add subfolders by category but I'm not sure how to read these commands with the command handler. Currently, I have this for a command handler:

client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
  const filePath = path.join(commandsPath, file);
  const command = require(filePath);
  if ('data' in command && 'execute' in command) {
    client.commands.set(command.data.name, command);
  } else {
    console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
  }
};```

This skips over the subfolders, so how can I make it include the folders? Thanks.
daring ingot
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

bronze gorge
#

djs v14.7.1
node v16.15.0