Created
July 6, 2015 16:34
-
-
Save AccaliaDeElementia/c914545253c15eae6eff to your computer and use it in GitHub Desktop.
parseCommands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.parseCommands = function parseCommands(post, events, callback) { | |
if (typeof callback !== 'function'){ | |
throw new Error('callback must be supplied'); | |
} | |
if (!events || typeof events.emit !== 'function'){ | |
throw new Error('events must be supplied'); | |
} | |
if (!post ||!post.raw){ | |
callback(null, []); | |
return; | |
} | |
const commands = post.raw.split('\n').map((line) => line.replace(/\s+$/, '')).map((line) => { | |
if (line[0] === '!') { | |
return parseShortCommand(line); | |
} else { | |
return parseMentionCommand(line); | |
} | |
}).filter((command) => !!command); | |
commands.forEach((command) => { | |
setImmediate(() => { | |
command.post = post; | |
const handled = events.emit('command#' + command.command, command); | |
if (!handled && !command.mention) { | |
if (!events.emit('command#ERROR', command)) { | |
events.emit('error', new Error('command `' + command.command + '` was unhandled.')); | |
} | |
} | |
}); | |
}); | |
callback(null, commands); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment