UNPKG

hubot-command-mapper

Version:
66 lines (65 loc) 2.18 kB
import { getValues } from "./parameters/ValueExtractor.mjs"; export class CommandResolver { robot; constructor(robot) { this.robot = robot; } resolve(res) { let tool = null; if (this.robot.__tools) { tool = this.robot.__tools.find(t => t != null && t.canHandle(res.message.text)); } return this.resolveFromTool(tool, res); } resolveFromTool(tool, res) { if (!res.message.text) return null; const result = new CommandResolverResult(); result.user = res.message.user; result.text = res.message.text; if (tool == null) { return result; } result.tool = tool; const matchingCommands = result.tool.commands.filter(cmd => cmd.validationRegex.test(res.message.text)); if (matchingCommands.length == 0) { return result; } result.command = matchingCommands[0]; result.authorized = (!result.tool.auth || result.tool.auth.length === 0 || result.tool.auth.indexOf(res.message.user.name) > -1) && (!result.command.auth || result.command.auth.length === 0 || result.command.auth.indexOf(res.message.user.name) > -1); result.match = result.command.validationRegex.exec(res.message.text); result.values = getValues(this.robot.name, this.robot.alias, result.tool, result.command, res.message.text); return result; } } export class CommandResolverResult { tool; command; authorized; match; values; text; user; log(logger) { if (logger) { const debug = this.getDebugInfo(); logger.info("Command", debug); } } getDebugInfo() { return { user: this.user ? this.user.name : null, userId: this.user ? this.user.id : null, authorized: this.authorized, text: this.text, tool: this.tool ? this.tool.name : null, command: this.command ? this.command.name : null, match: this.match, values: this.values }; } }