mirror of
https://github.com/arthur-pbty/bot-discord-coins.git
synced 2026-06-03 23:36:29 +02:00
35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
module.exports = function loadCommands(client, dir) {
|
|
let count = 0;
|
|
fs.readdirSync(path.join(__dirname, dir)).forEach((file) => {
|
|
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) => {
|
|
client.commands.set(alias, command);
|
|
});
|
|
}
|
|
count++;
|
|
} catch (error) {
|
|
console.error(`Failed to load file: ${filePath}`);
|
|
console.error(error);
|
|
}
|
|
}
|
|
});
|
|
return count;
|
|
} |