-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandhandler.js
More file actions
61 lines (51 loc) · 1.57 KB
/
Copy pathcommandhandler.js
File metadata and controls
61 lines (51 loc) · 1.57 KB
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
"use strict";
const fs = require("fs");
function uncacheTree(root) {
let uncache = [require.resolve(root)];
do {
let newuncache = [];
for (let i = 0; i < uncache.length; ++i) {
if (require.cache[uncache[i]]) {
newuncache.push.apply(
newuncache,
require.cache[uncache[i]].children.map(module => module.filename)
);
delete require.cache[uncache[i]];
}
}
uncache = newuncache;
} while (uncache.length > 0);
}
const COMMAND_RECURSION = 10; // how deep command search recursion can go.
class CommandHandler {
constructor() {
this.commands = {};
this.load();
}
load() {
this.commands = {};
const files = fs.readdirSync('./commands/');
for (const file of files) {
if (file === 'command-data') continue; // skip the localized database files specifically for commands
uncacheTree('./commands/' + file);
const tempCmds = require(`./commands/${file}`);
Object.assign(this.commands, tempCmds);
}
console.log('Loaded Commands');
}
get(cmd) {
if (!this.commands[cmd]) return null;
for (let i = 0; i < COMMAND_RECURSION; i++) {
if (typeof cmd === 'string') {
cmd = this.commands[cmd];
} else {
return cmd;
}
}
return null;
}
}
module.exports = function() {
const coms = new CommandHandler();
return coms;
}