Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const path = require('path');
- const availableCommands = [];
- function loadCommands(dir = path.join(__dirname, '../commands')) {
- fs.readdirSync(dir).forEach((file) => {
- const filepath = path.join(dir, file);
- const stat = fs.statSync(filepath);
- if (stat.isDirectory() && !stat.isSymbolicLink()) {
- // If read file is a directory, search in that directory
- loadCommands(filepath);
- } else if (file === 'index.js') {
- // If we encounter index.js file, it means that we found one of commands
- // Extract command from given dir path
- const lastDir = dir.split('/').pop();
- const command = lastDir !== 'commands' ? lastDir : '';
- availableCommands.push({
- command,
- ...require(filepath),
- });
- }
- });
- return availableCommands;
- }
- module.exports = loadCommands;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement