-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand-handler.js
234 lines (184 loc) · 6.08 KB
/
command-handler.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var fs = require("fs");
var path = require("path");
var is_initilized = false;
var commands = {};
var categories = {};
//main functions
function load(prefix = "%", directory = "./commands") {
//setup global variable
this.prefix = prefix;
this.directory = directory;
//initiliaze command handler
let categories_ = [];
let aliases = [];
if (!isDirectory(directory))
fs.mkdirSync(directory);
if (categories["default"] == null)
categories["default"] = { enabled: true, commands: {} };
fs.readdirSync(directory).forEach(file => {
let absolute_path = path.resolve(`${directory}/${file}`);
if (isDirectory(absolute_path) && path.parse(absolute_path).name != "default") {
categories_.push(absolute_path);
return;
}
let required = loadCommand(absolute_path);
if (required == null)
return;
if (commands[required.name] == null) {
commands[required.name] = required;
categories[required.category].commands[required.name] = required;
}
if (required.aliases.length != 0)
aliases.push(required);
});
categories_.forEach(category => {
if (categories[path.parse(category).name] == null)
categories[path.parse(category).name] = { enabled: true, commands: {} };
fs.readdirSync(category).forEach(file => {
let absolute_path = path.resolve(`${category}/${file}`);
let required = loadCommand(absolute_path, path.parse(category).name);
if (required == null)
return;
if (commands[required.name] == null) {
commands[required.name] = required;
categories[required.category].commands[required.name] = required;
}
if (required.aliases.length != 0)
aliases.push(required);
});
});
aliases.forEach(command => {
command.aliases.forEach(alias => {
if (commands[alias] == null)
commands[alias] = command;
});
});
is_initilized = true;
}
function loadCommand(absolute_path, category = "default") {
if (!isFile(absolute_path) || path.parse(absolute_path).ext != ".js")
return;
let file = path.parse(absolute_path).base;
try {
let required = require(absolute_path);
if (!isValid(required)) {
console.log(`Command ${file} is invalid!`)
return;
}
required.path = absolute_path;
required.category = category;
return required;
} catch (err) {
console.log(`Couldnt load ${file}:\n ${err}`);
return;
}
}
function execute(bot, cmd, username, args, ...custom) {
if (!is_initilized)
return error(`The command ahndler was not initlized!`, "not_init");
if (!isCommand(cmd))
return error(`Invalid command ${cmd}!`, "invalid_command");
let cmd_info = info(cmd);
if (!cmd_info.enabled)
return error(`Command ${cmd} is currently disabled!`);
try {
let output = cmd_info.execute(bot, cmd, username, args, this, ...custom);
return success(output);
} catch (err) {
console.log(`Error while executing ${cmd} (args: [${args.join(", ")}])!`);
console.log(err.stack);
return error(`Error while executing the command!`);
}
}
function reload(command) {
if (!is_initilized)
return error(`The command ahndler was not initlized!`, "not_init");
if (command == null) {
try {
Object.keys(commands).forEach(key => {
let command = commands[key];
delete require.cache[command.path];
});
} catch (err) { }
commands = {};
categories = {};
load();
return success(`successfully reloaded all commands!`);
} else {
let cmd_info = info(command);
if (cmd_info == null)
return error(`${this.prefix}${command} doesnt exist or was not loaded before!`);
try {
let path = cmd_info.path;
let category = cmd_info.category;
let aliases = cmd_info.aliases;
aliases.forEach(alias => {
if (commands[alias] == cmd_info)
delete commands[alias];
});
delete commands[cmd_info.name];
delete categories[cmd_info.category].commands[cmd_info.name];
delete require.cache[cmd_info.path];
let required = loadCommand(path, category);
if (required == null)
return;
if (commands[required.name] == null) {
commands[required.name] = required;
categories[required.category].commands[required.name] = required;
}
if (required.aliases.length != 0) {
required.aliases.forEach(alias => {
if (commands[alias] == null)
commands[alias] = required;
});
}
return success(`Successfully reloaded ${this.prefix}${command}`);
} catch (err) {
console.log(`Error while realoding ${command}!`);
console.log(err.stack);
return error(`Couldn't reload ${this.prefix}${command}`, "reload_error");
}
}
}
//utility functions
function isCommand(command) {
return commands[command] != null;
}
function info(command) {
if (!isCommand(command))
return null;
return commands[command];
}
function getCategory(category) {
if (categories[category] != null && Object.keys(categories[category].commands).length == 0)
return null;
return categories[category];
}
function getCategories() {
return Object.keys(categories);
}
function success(message) {
return { status: "success", message };
}
function error(message, code = "unknown") {
return { status: "error", message, code };
}
function isFile(filePath) {
return fs.existsSync(filePath) && fs.statSync(filePath).isFile();
}
function isDirectory(filePath) {
return fs.existsSync(filePath) && fs.statSync(filePath).isDirectory();
}
function isValid(command) {
return command != null && typeof command.execute == "function" && typeof command.name == "string" && typeof command.description == "string" && typeof command.usage == "string" && typeof command.enabled == "boolean" && Array.isArray(command.aliases);
}
//module.exports
module.exports = load;
module.exports.reload = reload;
module.exports.execute = execute;
module.exports.isCommand = isCommand;
module.exports.getCategories = getCategories;
module.exports.getCategory = getCategory;
module.exports.info = info;
module.exports.prefix = "%";
module.exports.directory = "./commands";