Files
gestion-perso/fonctions/loadCommands.ts
T
2024-03-15 20:35:35 +01:00

35 lines
1.3 KiB
TypeScript

import fs from 'fs';
import path from 'path';
module.exports = function loadCommands(client: any, dir: string) {
let count = 0;
fs.readdirSync(path.join(__dirname, dir)).forEach((file: string) => {
const filePath = path.join(__dirname, dir, file);
if (fs.statSync(filePath).isDirectory()) {
count += loadCommands(client, path.join(dir, file));
} else if (file.endsWith('.js') || file.endsWith('.ts')) {
try {
delete require.cache[require.resolve(filePath)];
const command = require(filePath);
const fileName = file.replace(/\.js|\.ts/g, '');
if (!command.name) command.name = fileName;
if (!command.category) {
const parentDir = path.basename(path.dirname(filePath));
command.category = parentDir === 'commands' ? 'other' : parentDir;
}
if (!command.permission) command.permission = 0;
client.commands.set(fileName, command);
if (command.aliases) {
command.aliases.forEach((alias: string) => {
client.commands.set(alias, command);
});
}
count++;
} catch (error) {
console.error(`Failed to load file: ${filePath}`); // Log any errors
console.error(error);
}
}
});
return count;
}