-
Notifications
You must be signed in to change notification settings - Fork 343
/
commandHandler.js
59 lines (49 loc) · 1.79 KB
/
commandHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const fs = require('fs');
const path = require('path');
// Define the directory where command files are located
const cmdsDir = path.join(__dirname, 'Cmds');
// Function to find all command files recursively in a directory
function findAllCommandFiles(directory) {
let commandFiles = [];
let totalCommands = 0;
// Recursive function to search for .js files in the given directory
function searchDirectory(currentDir) {
// Read all files and directories inside the current directory
const filesAndDirs = fs.readdirSync(currentDir);
// Loop through each file or directory
for (const fileOrDir of filesAndDirs) {
const fullPath = path.join(currentDir, fileOrDir);
const stat = fs.statSync(fullPath);
// If it's a directory, recursively search it
if (stat.isDirectory()) {
searchDirectory(fullPath);
}
// If it's a .js file, add it to the list of command files
else if (fileOrDir.endsWith('.js')) {
commandFiles.push(fullPath);
totalCommands++;
}
}
}
// Start searching from the given directory
searchDirectory(directory);
// Return an object containing the list of command files and the total number of commands
return {
commandFiles,
totalCommands
};
}
// Get all command files and their count
const { commandFiles, totalCommands } = findAllCommandFiles(cmdsDir);
// Load the commands into an object
const commands = {};
commandFiles.forEach(file => {
const commandName = path.basename(file, '.js'); // Extract command name from the file name
const commandModule = require(file); // Load the command module
commands[commandName] = commandModule; // Store the command in the commands object
});
// Export the loaded commands and the total command count
module.exports = {
commands,
totalCommands
};