-
Notifications
You must be signed in to change notification settings - Fork 1
/
associate.js
108 lines (93 loc) · 3.07 KB
/
associate.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
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
const ensure = (obj, key, def = {}) => {
if (!obj[key]) {
obj[key] = def;
}
return obj[key];
};
const runCache = {};
const updateCache = () => {
for (let thread in exports.config) {
if (thread === 'settings') {
continue;
}
runCache[thread] = [];
for (let assoc in exports.config[thread]) {
const res = {
association: assoc.toLowerCase(),
out: exports.config[thread][assoc]
};
try {
const regex = new RegExp(assoc, 'gim');
res.test = regex.test.bind(regex);
}
catch (e) {
res.test = input => input.toLowerCase().contains(res.association);
}
runCache[thread].push(res);
}
}
};
exports.load = updateCache;
exports.match = (event, commandPrefix) => {
if (event.arguments[0] === `${commandPrefix}associate`) {
return true;
}
if (event.__associateLoopback) {
return false;
}
for (const assoc of runCache[event.thread_id] || []) {
if (assoc.test(event.body)) {
ensure(event, '__associateResp', []).push(assoc.out);
}
}
return !!event.__associateResp;
}
const toggleAssociation = (thread, hook, text) => {
hook = hook.toLowerCase();
if (text) {
ensure(exports.config, thread)[hook] = text;
}
else {
if (ensure(exports.config, thread)[hook]) {
delete exports.config[thread][hook];
}
}
updateCache();
};
const printAssociations = (api, event) => {
const assoc = ensure(exports.config, event.thread_id);
const message = Object.keys(assoc).map(a => `${a} →\n\t${assoc[a]}`).join('\n');
api.sendMessage(message, event.thread_id);
};
const clear = (api, event) => {
exports.config[event.thread_id] = {};
api.sendMessage('Associations cleared.', event.thread_id);
};
const executeResponses = (api, event) => {
const outSettings = ensure(ensure(exports.config, 'settings'), event.thread_id);
for (let response of event.__associateResp) {
api.sendMessage(response, event.thread_id);
if (outSettings.loopback === true) {
const newEvent = shim.createEvent(event.thread_id, event.sender_id, event.sender_name, response);
newEvent.event_source = event.event_source;
newEvent.__associateLoopback = true;
process.nextTick(exports.platform.onMessage.bind(null, api, newEvent));
}
}
};
exports.run = (api, event) => {
if (event.__associateResp) {
return executeResponses(api, event);
}
if (event.arguments.length === 1 || event.arguments.length === 2 && event.arguments[1] === 'print') {
return printAssociations(api, event);
}
if (event.arguments.length === 1 && event.arguments[1] === 'clear') {
return clear(api, event);
}
if (event.arguments.length !== 2 && event.arguments.length !== 3) {
return api.sendMessage('WTF are you doing????!', event.thread_id);
}
toggleAssociation(event.thread_id, event.arguments[1], event.arguments[2]);
api.sendMessage('Association changed.', event.thread_id);
};