'use strict' // Description: // Generates help commands for Hubot. // // Commands: // hubot help - Displays all of the help commands that this bot knows about. // hubot help - Displays all help commands that match . // // URLS: // /hubot/help // // Configuration: // HUBOT_HELP_REPLY_IN_PRIVATE - if set to any value, all `hubot help` replies are sent in private // HUBOT_HELP_DISABLE_HTTP - if set, no web entry point will be declared // HUBOT_HELP_HIDDEN_COMMANDS - comma-separated list of commands that will not be displayed in help // // Notes: // These commands are grabbed from comment blocks at the top of each file. const helpContents = (name, commands) => `\ ${name} Help

${name} Help

${commands}
\ ` module.exports = (robot) => { robot.respond(/help(?:\s+(.*))?$/i, async (msg) => { let cmds = getHelpCommands(robot) const filter = msg.match[1] if (filter) { cmds = cmds.filter(cmd => cmd.match(new RegExp(filter, 'i'))) if (cmds.length === 0) { await msg.send(`No available commands match ${filter}`) return } } const emit = cmds.join('\n') if (process.env.HUBOT_HELP_REPLY_IN_PRIVATE && msg.message && msg.message.user && msg.message.user.name && msg.message.user.name !== msg.message.room) { await msg.reply('I just replied to you in private.') return await robot.send({ room: msg.message.user.id }, emit) } else { return await msg.send(emit) } }) if (process.env.HUBOT_HELP_DISABLE_HTTP == null) { return robot.router.get(`/${robot.name}/help`, (req, res) => { let cmds = getHelpCommands(robot).map(cmd => cmd.replace(/&/g, '&').replace(//g, '>')) if (req.query.q != null) { cmds = cmds.filter(cmd => cmd.match(new RegExp(req.query.q, 'i'))) } let emit = `

${cmds.join('

')}

` emit = emit.replace(new RegExp(`${robot.name}`, 'ig'), `${robot.name}`) res.setHeader('content-type', 'text/html') res.end(helpContents(robot.name, emit)) }) } } const getHelpCommands = function getHelpCommands (robot) { let helpCommands = robot.helpCommands() const robotName = robot.alias || robot.name if (hiddenCommandsPattern()) { helpCommands = helpCommands.filter(command => !hiddenCommandsPattern().test(command)) } helpCommands = helpCommands.map((command) => { if (robotName.length === 1) { return command.replace(/^hubot\s*/i, robotName) } return command.replace(/^hubot/i, robotName) }) return helpCommands.sort() } const hiddenCommandsPattern = function hiddenCommandsPattern () { const hiddenCommands = process.env.HUBOT_HELP_HIDDEN_COMMANDS != null ? process.env.HUBOT_HELP_HIDDEN_COMMANDS.split(',').map(c => c.trim()) : undefined if (hiddenCommands) { return new RegExp(`^hubot (?:${hiddenCommands != null ? hiddenCommands.join('|') : undefined}) - `) } }