-
Notifications
You must be signed in to change notification settings - Fork 4
/
message-handler.js
75 lines (71 loc) · 1.52 KB
/
message-handler.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import EventEmitter from 'events'
class MessageHandler extends EventEmitter {
constructor() {
super()
}
handle(message) {
if (message.startsWith('/')) {
const trimmed = message.substring(1)
const args = trimmed.split(' ').slice(1)
const command = trimmed.split(' ')[0]
switch (command.toLowerCase()) {
case 'help':
this.emit('help')
break
case 'version':
this.emit('version')
break
case 'info':
this.emit('info')
break
case 'invoice':
// args should be [amount_sats, label, description]
if (args.length !== 3) {
this.emit('error', 'invoice command must have 3 arguments [amount_sats, label, description]')
break
} else {
this.emit('invoice', args)
break
}
case 'address':
this.emit('address')
break
case 'channels':
this.emit('channels')
break
case 'funds':
this.emit('funds')
break
case 'donate':
this.emit('donate')
break
case 'issue':
this.emit('issue')
break
case 'pay':
if (args.length !== 1) {
this.emit('error', 'pay command must have 1 argument [bolt11]')
break
} else {
this.emit('pay', args)
break
}
case 'quiet':
this.emit('quiet')
break
case 'silent':
this.emit('silent')
break
case 'verbose':
this.emit('verbose')
break
default:
this.emit('unknown')
break
}
} else {
this.emit('error', 'must start with a slash /')
}
}
}
export default MessageHandler