Fix crash for unsupported modules and refactor message sending
This commit is contained in:
parent
9817172d65
commit
fc1c2db27c
@ -6,6 +6,11 @@ const UNKNOWN_MESSAGE = {
|
||||
message: "Unkown command"
|
||||
}
|
||||
|
||||
const OUTPUT_ERROR = {
|
||||
format: "standard",
|
||||
message: "Module returned unknown output, contact the administrator"
|
||||
}
|
||||
|
||||
class ModuleManager {
|
||||
constructor() {
|
||||
console.log("Creating module manager");
|
||||
@ -43,39 +48,61 @@ class ModuleManager {
|
||||
let module = this.moduleList[prefix];
|
||||
|
||||
if(module === undefined) {
|
||||
this.reply(message, UNKNOWN_MESSAGE);
|
||||
this.send(UNKNOWN_MESSAGE, {channel: message.channel});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let rawResult = module.run(message);
|
||||
let result = JSON.parse(rawResult.toString());
|
||||
let result = module.run(message);
|
||||
|
||||
if(result.status === "success") {
|
||||
switch(result.action) {
|
||||
case "reply":
|
||||
this.reply(message, result);
|
||||
break;
|
||||
case "send":
|
||||
this.send(message, result);
|
||||
default:
|
||||
break;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
switch(result.action) {
|
||||
case "reply":
|
||||
this.send(result, {origin: message});
|
||||
break;
|
||||
case "send":
|
||||
default:
|
||||
this.send(result, {channel: message.channel});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
reply(origin, result) {
|
||||
send(result, options) {
|
||||
let message = undefined;
|
||||
|
||||
if(result.format === "standard") {
|
||||
message = result.message;
|
||||
switch(result.format) {
|
||||
case "embed":
|
||||
message = {embed: result.profile};
|
||||
break;
|
||||
default:
|
||||
if(result.message) {
|
||||
message = result.message;
|
||||
} else {
|
||||
message = result;
|
||||
}
|
||||
}
|
||||
|
||||
if(result.format === "embed") {
|
||||
message = {embed: result.profile}
|
||||
if(options.origin) {
|
||||
options.origin.reply(message)
|
||||
}
|
||||
|
||||
origin.reply(message);
|
||||
if(options.channel) {
|
||||
options.channel.send(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user