-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (83 loc) 路 2.07 KB
/
Copy pathindex.js
File metadata and controls
102 lines (83 loc) 路 2.07 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* An easy work with commands for Telekit
*
* @module telekit-cmd
* @author Denis Maslennikov <mrdenniska@gmail.com> (nofach.com)
* @license MIT
*/
/**
* Constants
* @private
*/
const RE_BOT_COMMAND = /^\/([a-z0-9\-\_]+)(\@[a-z0-9\-\_]+)?$/i;
/**
* Methods
* @private
*/
function parseEntityBotCommand(text, entity) {
let result = RE_BOT_COMMAND.exec(text.substr(
entity.offset,
entity.length
));
return {
type: entity.type,
name: result[1],
mention: result[2],
};
}
function parseEntity(text, entity) {
return {
type: entity.type,
value: text.substr(entity.offset, entity.length),
};
}
function parse(update) {
let result = null;
if (!update.text || update.text[0] != '/' || !update.entities) {
return false;
}
result = { text: update.text, entities: [] };
update.entities.forEach((item) => {
if (item.type == 'bot_command') {
return result.entities.push(parseEntityBotCommand(
update.text,
item
));
}
result.entities.push(parseEntity(
update.text,
item
));
});
return result;
}
/**
* Implementation
* @public
*/
function telekit_cmd(telekit) {
let handle = (context) => {
let cmd = parse(context.update);
if (cmd) {
cmd.name = cmd.entities[0].name;
cmd.mention = cmd.entities[0].mention;
context.isCommand = true;
context.command = cmd;
if (telekit.command) {
telekit.command(context);
}
telekit.emit('command', context);
telekit.emit(`/${context.command.name}`, context);
telekit.dispatch('command', context);
return;
}
context.isCommand = false;
context.command = null;
};
telekit.context.isCommand = false;
telekit.context.command = null;
telekit.on('message', handle);
telekit.on('post', handle);
}
/** Exports */
module.exports = telekit_cmd;