Refactor command and event loading
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const isJavaScriptFile = (file) => file.endsWith('.js');
|
||||
|
||||
const getCommandFilePaths = (commandsPath) => {
|
||||
const commandFolders = fs
|
||||
.readdirSync(commandsPath, { withFileTypes: true })
|
||||
.filter((entry) => entry.isDirectory())
|
||||
.map((entry) => entry.name);
|
||||
|
||||
return commandFolders.flatMap((folder) => {
|
||||
const folderPath = path.join(commandsPath, folder);
|
||||
return fs
|
||||
.readdirSync(folderPath)
|
||||
.filter(isJavaScriptFile)
|
||||
.map((file) => path.join(folderPath, file));
|
||||
});
|
||||
};
|
||||
|
||||
const loadCommandModules = (commandsPath) => {
|
||||
const commandFiles = getCommandFilePaths(commandsPath);
|
||||
const commands = [];
|
||||
const invalidCommands = [];
|
||||
|
||||
for (const filePath of commandFiles) {
|
||||
const command = require(filePath);
|
||||
|
||||
if ('data' in command && 'execute' in command) {
|
||||
commands.push(command);
|
||||
} else {
|
||||
invalidCommands.push(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
return { commands, invalidCommands };
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
loadCommandModules,
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const isJavaScriptFile = (file) => file.endsWith('.js');
|
||||
|
||||
const getEventFilePaths = (eventsPath) =>
|
||||
fs
|
||||
.readdirSync(eventsPath)
|
||||
.filter(isJavaScriptFile)
|
||||
.map((file) => path.join(eventsPath, file));
|
||||
|
||||
const loadEventModules = (eventsPath) => {
|
||||
const eventFiles = getEventFilePaths(eventsPath);
|
||||
|
||||
return eventFiles.map((filePath) => ({
|
||||
filePath,
|
||||
event: require(filePath),
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
loadEventModules,
|
||||
};
|
||||
Reference in New Issue
Block a user