-
Notifications
You must be signed in to change notification settings - Fork 0
/
moduleLoader.js
325 lines (324 loc) · 15.3 KB
/
moduleLoader.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//moduleLoader.js: Loads modules and handles events
//Copyright 2018-2019 BlaCoiso
//This file is part of thingBot, licensed under GPL v3.0
//jshint esversion: 6
const fs = require("fs");
const modulePath = "./bot_modules";
const BaseModule = require("./moduleBase");
const commandRegex = /^[0-9a-zA-Z_\-\.]+$/;
const Discord = require("discord.js");
class ModuleLoader {
constructor() {
this.modules = [];
this.loadedModules = new Map();
this.commands = new Map();
this.logger = null;
this.logWrapper = null;
this.handledEvents = new Map();
this.commandRegex = commandRegex;
}
init(logger, logWrapper, eventCallback, DB) {
this.logger = logger;
this.logWrapper = logWrapper;
this.eventCallback = eventCallback;
this.DB = DB;
if (fs.existsSync(modulePath)) {
let moduleDir = fs.readdirSync(modulePath);
this.modules = moduleDir.filter(m => m.endsWith(".js")).map(m => m.replace(/\.js$/, ""));
var moduleCount = 0;
for (let moduleName of this.modules) {
try {
/** @type {BaseModule} */
let moduleObj = require(modulePath + '/' + moduleName);
if (moduleObj && this.initModule(moduleObj, moduleName))++moduleCount;
} catch (e) {
logger(`Error initializing module ${moduleName}`, e);
}
}
if (this.modules.length === 0) {
logger("No modules to be loaded", "fatal");
process.abort();
} else if (moduleCount === 0) {
logger("Failed to load any module", "fatal");
process.abort();
}
logger(`Loaded ${moduleCount}/${this.modules.length} modules`, "debug");
return Array.from(this.handledEvents.keys());
} else {
logger("Failed to load bot modules", "fail");
return null;
}
}
handle(event, client, DB, ...args) {
var handlers = this.handledEvents.get(event);
if (handlers && Array.isArray(handlers) && handlers.length !== 0) {
for (let moduleObj of handlers) {
try {
moduleObj.handle.apply(moduleObj, arguments);
} catch (e) {
moduleObj.logger(`Failed to handle '${event}' event`, e);
}
}
}
}
handleCommand(command, message, args) {
var cmdObj = this.commands.get(command);
var logger = this.logger;
function handleCommandOutput(output) {
function sendMessage(content, channel, options) {
return channel.send(content, options).catch(e => logger("Failed to send command response", e));
}
if (output) {
let outputChannel = ((cmdObj && cmdObj.sendDM) ? output.sendDM !== false : output.sendDM) ? message.author : message.channel;
let sendOptions = {};
let botPerms = args.getBotPerms(outputChannel);
if ((cmdObj && cmdObj.reply) ? output.reply !== false : output.reply) sendOptions.reply = message.author;
if (typeof output === "string") {
if (botPerms.send) return sendMessage(output, outputChannel, sendOptions);
}
else if (output instanceof Promise) return output.then(out => handleCommandOutput(out))
.catch(e => logger(`Failed to handle asynchronous command '${command}'`, e));
else if (typeof output === "object") {
if (output instanceof Discord.Message) { }
else if (output instanceof Discord.RichEmbed) {
if (botPerms.embed) {
sendOptions.embed = output;
return sendMessage(sendOptions, outputChannel);
}
} else if (output instanceof Discord.Attachment) {
if (botPerms.attach) {
sendOptions.file = output;
return sendMessage(sendOptions, outputChannel);
}
} else {
if (output.split) sendOptions.split = output.split;
if (typeof output.disableEveryone === "boolean") sendOptions.disableEveryone = output.disableEveryone;
if (output.tts) sendOptions.tts = true;
if (output.code) sendOptions.code = output.code;
if (output.attachment && botPerms.attach) {
if (Array.isArray(output.attachment)) sendOptions.files = output.attachment;
else sendOptions.file = output.attachment;
}
if (output.options && typeof output.options === "object") Object.assign(sendOptions, options);
if (output.embed && botPerms.embed) {
sendOptions.embed = output.embed;
return sendMessage(sendOptions, outputChannel);
}
else if (output.text && botPerms.send) {
if (botPerms.send) return sendMessage(output.text, outputChannel, sendOptions);
} else if (output.attachment && botPerms.attach) return sendMessage(sendOptions, outputChannel);
if (output.embed && !output.text && !botPerms.embed && botPerms.send) sendMessage(args.getError("NO_EMBED"), outputChannel);
}
}
}
return Promise.resolve(false);
}
args.setOutputCallback(handleCommandOutput);
if (cmdObj) {
if (args.isDM && cmdObj.disableDM) handleCommandOutput(args.getError("NO_DM"));
else if (!args.checkPerms(cmdObj.perms)) handleCommandOutput(args.getError("NO_PERMS"));
else {
args.setDBContext({ module: cmdObj.module });
if (!cmdObj.output || !handleCommandOutput(cmdObj.output)) {
let commandFailLog = `Failed to handle command '${command}'`;
if (cmdObj.prefetch) {
args.wrappedDB.prefetch(cmdObj.prefetch).then(p => {
args.setPrefetched(p);
handleCommandOutput(cmdObj.run(message, args));
}, e => {
this.logger(`Failed to prefetch paths for command ${command}`, e);
handleCommandOutput(args.getError("EXEC_ERR"));
}).catch(e => {
cmdObj.module.logger(commandFailLog, e);
handleCommandOutput(args.getError("EXEC_ERR"));
});
} else {
try {
handleCommandOutput(cmdObj.run(message, args));
}
catch (e) {
cmdObj.module.logger(`Failed to handle command '${command}'`, e);
handleCommandOutput(args.getError("EXEC_ERR"));
}
}
}
}
} else this.handle("command", message.client, command, message, args);
}
/**
* Initializes a module
* @param {BaseModule} moduleObj
*/
initModule(moduleObj, moduleName) {
let events = [];
if (moduleObj.name === "Module") moduleObj.name = moduleName[0].toUpperCase() + moduleName.slice(1);
if (moduleObj.events) {
if (typeof moduleObj.events === "string") {
let evt = moduleObj.events.toLowerCase();
events = [evt];
}
else if (Array.isArray(moduleObj.events)) events = moduleObj.events;
else {
this.logger(`Module ${moduleObj.name} (${moduleName}) has invalid handled event definition`, "warn");
return false;
}
}
else if (!(moduleObj.commands && Array.isArray(moduleObj.commands) && moduleObj.commands.length !== 0)) {
this.logger(`Module ${moduleObj.name} (${moduleName}) doesn't specify handled events, ignoring`, "warn");
return false;
}
for (let evt of events) {
evt = evt.toLowerCase();
if (!this.handledEvents.has(evt)) this.handledEvents.set(evt, [moduleObj]);
else {
let handled = this.handledEvents.get(evt);
if (!handled.includes(moduleObj)) handled.push(moduleObj);
}
}
moduleObj.logger = this.logWrapper(moduleObj.name);
moduleObj.init(moduleObj.logger, this.DB);
this.logger(`Initialized module ${moduleName}`, "debug");
//TODO: Add some kind of utility class thing to be given to all modules
this.loadedModules.set(moduleName, moduleObj);
if (moduleObj.commands && Array.isArray(moduleObj.commands) && moduleObj.commands.length !== 0) {
for (let command of moduleObj.commands) {
command.module = moduleObj;
if (!command.name || typeof command.name !== "string") {
this.logger(`Module ${moduleObj.name} has invalid commands, aborting command loading`, "warn");
break;
}
this.registerCommand(command);
}
}
return true;
}
/**
* Registers a commmand
* @param {ModuleCommand} command
*/
registerCommand(command) {
command.name = command.name.toLowerCase();
if (this.commands.has(command.name)) {
//Assume it's an issue with the module unloader/reloader and not duplicate commands
this.logger(`Command ${command.name} was already registered, ignoring command`, "debug");
return;
}
if (commandRegex.test(command.name)) this.registerCommandName(command.name, command);
if (command.aliases) {
if (typeof command.aliases === "string") this.registerCommandName(command.aliases, command);
else if (Array.isArray(command.aliases) && command.aliases.length !== 0) {
for (let alias of command.aliases) {
if (commandRegex.test(alias)) this.registerCommandName(alias, command);
}
}
}
}
registerCommandName(name, command) {
if (!this.commands.has(name) && typeof name === "string") this.commands.set(name.toLowerCase(), command);
}
getCommands() {
let result = [];
for (let moduleObj of this.loadedModules.values()) {
if (moduleObj.commands && Array.isArray(moduleObj.commands)) {
for (let command of moduleObj.commands) {
if (this.commands.get(command.name.toLowerCase()) === command) result.push(command);
}
}
}
return result;
}
loadModule(moduleName) {
try {
if (this.loadedModules.has(moduleName)) this.unloadModule(moduleName);
if (!this.modules.includes(moduleName)) this.modules.push(moduleName);
let moduleObj = require(modulePath + '/' + moduleName);
if (moduleObj) {
if (moduleObj.events) this.eventCallback(moduleObj.events);
return this.initModule(moduleObj, moduleName);
}
} catch (e) {
this.logger(`Error initializing module ${moduleName}`, e);
}
}
unloadModule(moduleName) {
if (this.loadedModules.has(moduleName)) {
try {
/** @type {BaseModule} */
let moduleObj = this.loadedModules.get(moduleName);
let events;
if (typeof moduleObj.events === "string") events = [moduleObj.events];
else if (Array.isArray(moduleObj.events)) events = moduleObj.events;
for (let event of events) {
let eventHandlers = this.handledEvents.get(event);
if (!eventHandlers) continue;
let evtIdx = eventHandlers.indexOf(moduleObj);
if (evtIdx === -1) continue;
eventHandlers.splice(evtIdx, 1);
}
if (moduleObj.commands && Array.isArray(moduleObj.commands)) {
for (let cmd of moduleObj.commands) {
let cmdName = cmd.name.toLowerCase();
let cmdAliases = [];
if (typeof cmd.aliases === "string") cmdAliases = [cmd.aliases.toLowerCase()];
else if (Array.isArray(cmd.aliases)) cmdAliases = cmd.aliases.map(a => a.toLowerCase());
cmdAliases.push(cmdName);
for (let alias of cmdAliases) {
if (this.commands.get(alias) === cmd) this.commands.delete(alias);
}
}
}
let modIdx = this.modules.indexOf(moduleName);
if (modIdx !== -1) this.modules.splice(modIdx, 1);
this.loadedModules.delete(moduleName);
delete require.cache[require.resolve(modulePath + '/' + moduleName)];
return true;
} catch (e) {
this.logger(`Failed to unload module ${moduleName}`, e);
}
} else this.logger(`Attempted to unload already unloaded module ${moduleName}`, "warn");
}
reloadModule(moduleName) {
if (this.loadedModules.has(moduleName) || this.modules.includes(moduleName)) {
let moduleObj = this.loadedModules.get(moduleName);
this.unloadModule(moduleName);
let result = this.loadModule(moduleName);
if (!result && moduleObj) {
try {
this.initModule(moduleObj, moduleName);
} catch (e) {
this.logger("Failed to revert module from cache", e);
}
}
if (this.loadedModules.size === 0) {
this.logger("No modules loaded after reload", "fatal");
process.abort();
} else return result;
} else this.logger("Attempted to reload a module that wasn't loaded", "warn");
}
reload() {
let oldLoaded = new Map(this.loadedModules);
this.loadedModules.clear();
this.commands.clear();
this.handledEvents.clear();
let recovered = 0;
for (let moduleName of this.modules) {
delete require.cache[require.resolve(modulePath + '/' + moduleName)];
if (!this.loadModule(moduleName)) {
if (oldLoaded.has(moduleName)) {
let moduleObj = oldLoaded.get(moduleName);
try {
if (this.initModule(moduleObj, moduleName))++recovered;
} catch (e) {
this.logger("Failed to revert module from cache", e);
}
}
}
}
if (this.loadedModules.size === 0) {
this.logger("No modules loaded after reload", "fatal");
process.abort();
}
return this.loadedModules.size - recovered;
}
}
module.exports = new ModuleLoader();