-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandHandler.js
75 lines (60 loc) · 2.29 KB
/
CommandHandler.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
73
74
75
class CommandHandler {
constructor(data = {}) {
if (!data.folder) throw new Error("[HANDLER] Klasör belirtilmemiş.");
this.folder = data.folder;
if(!data.prefix) throw new Error("[HANDLER] Prefix belirtilmemiş.");
if (!Array.isArray(data.prefix)) data.prefix = [data.prefix];
data.prefix.sort((a, b) => a.length < b.length);
this.prefix = data.prefix;
this._loadFrom(data.folder)
}
_loadFrom(folder) {
const commands = new Map();
const aliases = new Map();
const fs = require("fs");
const files = fs.readdirSync(folder);
files.filter(f => fs.statSync(folder + f).isDirectory())
.forEach(nested => fs.readdirSync(folder + nested).forEach(f => files.push(nested + '/' + f)));
const jsFiles = files.filter(f => f.endsWith('.js'));
if (files.length <= 0) throw new Error('[HANDLER] Yüklenecek komut yok!');
const fileAmount = `${jsFiles.length}`;
console.log(`[HANDLER] ${fileAmount} tane dosya bulundu, yükleniyor\n`);
for (const f of jsFiles) {
const file = require(folder + f);
const cmd = new file();
const name = cmd.name;
commands.set(name, cmd);
console.log(`[HANDLER] Komut yükleniyor: '${name}'`);
for (const alias of cmd.alias) {
aliases.set(alias, name);
}
}
console.log('[HANDLER] Bütün komutlar yüklendi');
this.commands = commands;
this.aliases = aliases;
}
getCommand(string) {
if (!string) return null;
let prefix = '';
let prefixExists = false;
for (const x of this.prefix) {
if (string.startsWith(x)) {
prefix = x;
prefixExists = true;
break;
}
}
if (!prefixExists) return null;
const command = string.substring(prefix.length);
let cmd = this.commands.get(command);
if (!cmd) {
const alias = this.aliases.get(command);
if (!alias) return null;
cmd = this.commands.get(alias);
}
return cmd;
}
}
module.exports = {
CommandHandler
};