This repository was archived by the owner on Feb 6, 2024. It is now read-only.
forked from Moishe/catbot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcatbot-runner.js
More file actions
122 lines (101 loc) · 2.99 KB
/
catbot-runner.js
File metadata and controls
122 lines (101 loc) · 2.99 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const statsd = require("./lib/statsd");
function CatRunner() {
console.log("constructing.");
this.RtmClient = undefined;
this.RTM_EVENTS = undefined;
this.token = undefined;
this.rtm = undefined;
this.web = undefined;
this.DEFAULT_MODULE_NAME = "default";
console.log("constructed.");
}
CatRunner.prototype.init = function(slackClient, tok) {
console.log("initializing.");
this.RtmClient = slackClient.RtmClient;
this.RTM_EVENTS = slackClient.RTM_EVENTS;
this.token = tok;
this.rtm = new this.RtmClient(this.token, {
logLevel: "warning",
dataStore: false
});
this.web = new slackClient.WebClient(this.token);
this.sanitize = require("sanitize-filename");
var sqlite3 = require("sqlite3").verbose();
this.db = new sqlite3.Database("db");
this.channelRe = /#.*/;
this.userRe = /<@[UW][A-Za-z0-9]+>/;
console.log("initialized.");
this.regex = /^\?/;
};
CatRunner.prototype.start = function() {
console.log("starting");
this.rtm.start();
var self = this;
this.rtm.on(this.RTM_EVENTS.MESSAGE, m => {
self.handleRtmMessage(m);
});
console.log("started");
};
CatRunner.prototype.loader = function(moduleName) {
// don't throw if moduleName doesn't exist.
try {
return require(moduleName);
} catch (e) {
console.log("couldn't find module named " + moduleName);
}
};
CatRunner.prototype.shouldInvokeOn = function(message) {
return (
message.type == "message" &&
message.text &&
message.text.match &&
message.text.match(this.regex) &&
message.text !== "?"
);
};
CatRunner.prototype.handleRtmMessage = function(message) {
if (this.shouldInvokeOn(message)) {
var cleanMessage = message.text.replace(this.regex, "");
var pieces = cleanMessage.split(" ");
var bareModule = this.sanitize(pieces[0]);
var moduleName = "./modules/" + bareModule + ".js";
console.log("Attempting to load " + moduleName);
var handler = this.loader(moduleName);
if (!handler) {
// if we didn't find a handler, try the default handler.
console.log("loading default handler");
moduleName = "./modules/" + this.DEFAULT_MODULE_NAME + ".js";
bareModule = pieces[0];
handler = this.loader(moduleName);
}
if (!handler) {
console.log("no handler");
return;
}
pieces.shift();
const self = this;
statsd.increment("fcat." + moduleName);
this.web.users.info(message.user).then(response => {
const sender = response.user;
handler.handle(
sender,
pieces.slice(0),
this.db,
function(result) {
if (result) {
if (result.message) {
self.web.chat.postMessage(message.channel, result.message, {
as_user: true
});
}
}
},
bareModule
);
});
// unload the module so changes will be picked up without restarting the server
var name = require.resolve(moduleName);
delete require.cache[name];
}
};
exports.CatRunner = CatRunner;