/
parser.js
41 lines (31 loc) · 841 Bytes
/
parser.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
33
34
35
36
37
38
39
40
41
const constants = require('./constants.json')
function splitMessage(message) {
const regex = /([^ "]+|(?:")(?:\\"|[^"])+(?:"))/g
const args = message.match(regex)
const result = args.map(a => {
if(a.startsWith(constants.QUOTE_CHAR)) {
return a.substring(1, a.length - 1).replace(/\\"/g, constants.QUOTE_CHAR)
}
return a
})
return result
}
function parseMessage(message, prefix) {
if(!prefix) {
throw new Error('You must specify a prefix')
}
if(!message) {
throw new Error('You must specify a message')
}
let args = splitMessage(message)
let command = args.shift()
command = command.replace(prefix, constants.EMPTY_CHAR)
return {
command,
args
}
}
module.exports = {
splitMessage,
parseMessage
}