-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdBus.js
45 lines (42 loc) · 949 Bytes
/
cmdBus.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
/**
* @constructor
*/
function CmdBus() {
this.commands = [];
}
/**
* @param regexp
* @param callback
*/
CmdBus.prototype.on = function (regexp, callback) {
this.commands.push({regexp: regexp, callback: callback});
};
/**
* @param client
* @returns {boolean}
*/
CmdBus.prototype.condition = function (client) {
const message = client.payload.message || {};
const text = message.text || '';
return text.charAt(0) === '/';
};
/**
* @param client
* @returns {*}
*/
CmdBus.prototype.handle = function (client) {
const message = client.payload.message.text || '';
for (var i in this.commands) {
var cmd = this.commands[i];
var tokens = cmd.regexp.exec(message);
if (tokens != null) {
try {
const args = tokens.splice(1);
return cmd.callback.apply(client, args);
} catch (e) {
console.error('Error: ', e, e.stack);
}
}
}
return client.sendMessage(messages.help);
};