-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloadedCommands.js
72 lines (67 loc) · 2.77 KB
/
loadedCommands.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
60
61
62
63
64
65
66
67
68
69
70
71
72
import './configs/global.js';
import functions from './library/functions.js';
import fs from 'fs';
import path from 'path';
async function loadCommands() {
const commandsPath = './commands';
const plugins = fs.readdirSync(commandsPath);
for (const plugin of plugins) {
if (!/\.js$/g.test(plugin)) {
const commandFiles = fs.readdirSync(path.join(commandsPath, plugin)).filter(file => file.endsWith('.js'));
for (const filename of commandFiles) {
const pathFiles = path.join(commandsPath, plugin, filename);
try {
const command = (await import(`./${pathFiles}`)).default;
const allCommand = functions.requireJson('./database/allCommands.json');
if (command) {
global.headersCommands.push({ category: plugin, command: command.views });
if (allCommand.length < 1) {
global.allCommands.push(command.views[0].split(' ')[0]);
} else {
global.allCommands.push(...allCommand, command.views[0].split(' ')[0]);
}
global.allCommands = [...new Set(global.allCommands)];
global.plugins[`${plugin}-${filename}`] = command;
}
} catch (error) {
console.error(`Error loading command file ${pathFiles}: ${error.message}`);
functions.reloadModule(pathFiles);
}
}
}
}
}
function updateCommandsFile() {
const headers = global.headersCommands;
const objects = headers.reduce((objects, items) => {
if (objects[items.category]) {
objects[items.category].command.push(...items.command);
} else {
objects[items.category] = { ...items };
}
return objects;
}, {});
Object.keys(objects).forEach(category => {
global.Commands[category] = [...new Set(objects[category].command)].sort();
});
const keysCommander = Object.keys(global.commander);
const keysCommands = Object.keys(global.Commands);
if (keysCommander.length === 0 && keysCommands.length !== 0) {
fs.writeFileSync('./database/commands.json', JSON.stringify(global.Commands, null, 2));
logger.success('Successfully loaded plugins');
} else if (keysCommands.length === keysCommander.length) {
keysCommander.forEach(key => {
if (global.commander[key].length !== global.Commands[key].length) {
fs.writeFileSync('./database/commands.json', JSON.stringify(global.Commands, null, 2));
logger.success('Successfully added plugins');
}
});
} else if (keysCommands.length !== (keysCommander.length || keysCommander.length === 0)) {
fs.writeFileSync('./database/commands.json', JSON.stringify(global.Commands, null, 2));
logger.success('Successfully updated plugins');
}
}
export default async function index(functions) {
await loadCommands(functions);
updateCommandsFile();
}