This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
63 lines (53 loc) · 1.81 KB
/
utils.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
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
function buildHelp(meta) {
const examples = meta.examples.map(e => ` example: ${e}`).join('\n');
return `${meta.name} - ${meta.short}\n${examples}`;
}
function pre(text) {
return `\`\`\`${text}\`\`\``;
}
class Plugin {
constructor(options) {
this.options = options;
this.bot = options.bot;
this.rtm = options.rtm;
this.web = options.web;
this.config = options.config;
this.secret = options.secret;
this.routes = [];
this.initialize();
}
route(pattern, handler, routeOptions) {
this.routes.push([pattern, handler, routeOptions]);
}
initialize() {
this.rtm.on(RTM_EVENTS.MESSAGE, (message) => {
message.reply = text => this.rtm.sendMessage(text, message.channel);
message.reply_thread = (text) => {
this.web.chat.postMessage(
message.channel,
text,
{ as_user: true, thread_ts: message.ts });
};
if (message.text != null) {
this.routes.forEach((route) => {
const [pattern, handler, routeOptions] = route;
const match = message.text.match(pattern) || [];
if (match.length > 0) {
const groups = match.slice(1);
const [who, _] = groups;
if (!routeOptions.self
|| (routeOptions.self && who === this.options.bot.self.id)) {
handler(this.options, message, ...match.slice(1), routeOptions);
}
}
});
}
});
}
}
module.exports = {
buildHelp,
Plugin,
pre,
};