commandDispatcher: handle async functions properly

This commit is contained in:
Cynthia Foxwell 2021-03-14 20:22:28 -06:00
parent 97e46f6447
commit a7a8b90854

View file

@ -17,7 +17,7 @@ function parseArguments(str) {
).a;
}
function runCommand(msg, cmd, line, args) {
async function runCommand(msg, cmd, line, args) {
let cmdObj = hf.commands.get(cmd);
if (!cmdObj) {
for (const c of hf.commands.values()) {
@ -38,14 +38,19 @@ function runCommand(msg, cmd, line, args) {
}
try {
return cmdObj.callback(msg, line, ...args);
const ret = cmdObj.callback(msg, line, ...args);
if (ret instanceof Promise) {
return await ret;
} else {
return ret;
}
} catch (err) {
logger.error("hf:cmd:" + cmd, err);
return ":warning: An internal error occurred.";
}
}
function CommandDispatcher(msg) {
async function CommandDispatcher(msg) {
let str = msg.content;
let inCommand = false;
@ -72,8 +77,8 @@ function CommandDispatcher(msg) {
const args = parseArguments(line);
const response = runCommand(msg, cmd, line, args);
if (response) {
const response = await runCommand(msg, cmd, line, args);
if (response != null) {
msg.channel.createMessage(
Object.assign(
typeof response === "string" ? {content: response} : response,