commandDispatcher: handle async functions properly
This commit is contained in:
		
							parent
							
								
									97e46f6447
								
							
						
					
					
						commit
						a7a8b90854
					
				
					 1 changed files with 10 additions and 5 deletions
				
			
		| 
						 | 
					@ -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,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue