-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
32 lines (26 loc) · 833 Bytes
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var _ = require('lodash');
var quotes = require('./quotes');
var triggers = ['!quote', 'quotebot', 'quotbot'];
function parseMessage(msg) {
return Promise.resolve().then(() => {
const [trigger, command, input, ...rest] = _.split(msg, ' ');
if (!trigger || !_.includes(triggers, trigger)) {
return;
}
return processCommand(command, input);
});
}
function processCommand(command, input) {
switch (command.toLowerCase()) {
case 'get':
if (!_.isNumber(Number(input))) {
throw new Error('quoteId is not a number.');
}
return quotes.get(input);
case 'search':
return quotes.search(input);
default:
throw new Error('command not matched');
}
}
module.exports = parseMessage;