#How to incorporate subfolders into a command handler?

1 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.