-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (47 loc) · 1.31 KB
/
index.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
const fs = require('fs');
module.exports = class Handler {
constructor(config) {
if (!config.folder) throw new Error('Folder necessary! config.folder');
this.commands = new Map();
this.aliases = new Map();
this.folder = config.folder;
this.prefix = config.prefix || ['!'];
this.prefix.sort((a, b) => a.length < b.length);
this.load(this.folder);
}
load(folder) {
const files = fs.readdirSync(folder);
files.filter(f => f.endsWith('.js'));
files.forEach(f => {
const file = require(folder + f);
const cmd = new file();
this.commands.set(cmd.name, cmd);
cmd.aliases.forEach(alias => {
this.aliases.set(alias, cmd.name);
});
});
console.info('Loaded All Commands! TOTAL: ' + files.length);
console.info('Prefixes: ' + this.prefix.join(' '));
}
get(string) {
if (!string || typeof string !== 'string') return null;
let prefix = false;
let cmd = '';
this.prefix.forEach(p => {
if (string.indexOf(p) === 0) {
prefix = p;
cmd = string.slice(prefix.length);
}
});
if (prefix === null) return null;
const file =
this.commands.get(cmd) ||
this.commands.get(this.aliases.get(cmd)) ||
null;
if (file) {
return file;
} else {
return null;
}
}
};