Fix crash for unsupported modules and refactor message sending

This commit is contained in:
Tanguy Herbron 2021-05-09 20:28:17 +02:00
parent 9817172d65
commit fc1c2db27c

View File

@ -6,6 +6,11 @@ const UNKNOWN_MESSAGE = {
message: "Unkown command" message: "Unkown command"
} }
const OUTPUT_ERROR = {
format: "standard",
message: "Module returned unknown output, contact the administrator"
}
class ModuleManager { class ModuleManager {
constructor() { constructor() {
console.log("Creating module manager"); console.log("Creating module manager");
@ -43,39 +48,61 @@ class ModuleManager {
let module = this.moduleList[prefix]; let module = this.moduleList[prefix];
if(module === undefined) { if(module === undefined) {
this.reply(message, UNKNOWN_MESSAGE); this.send(UNKNOWN_MESSAGE, {channel: message.channel});
return; return;
} }
let rawResult = module.run(message); let result = module.run(message);
let result = JSON.parse(rawResult.toString());
if(Buffer.isBuffer(result)) {
if(result.length === 0) {
this.send(OUTPUT_ERROR, {channel: message.channel});
return;
}
let resultString = result.toString();
try {
result = JSON.parse(resultString);
} catch(exception) {
result = resultString;
}
}
if(result.status === "success") {
switch(result.action) { switch(result.action) {
case "reply": case "reply":
this.reply(message, result); this.send(result, {origin: message});
break; break;
case "send": case "send":
this.send(message, result);
default: default:
this.send(result, {channel: message.channel});
break; break;
} }
} }
}
reply(origin, result) { send(result, options) {
let message = undefined; let message = undefined;
if(result.format === "standard") { switch(result.format) {
case "embed":
message = {embed: result.profile};
break;
default:
if(result.message) {
message = result.message; message = result.message;
} else {
message = result;
}
} }
if(result.format === "embed") { if(options.origin) {
message = {embed: result.profile} options.origin.reply(message)
} }
origin.reply(message); if(options.channel) {
options.channel.send(message);
}
} }
} }