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