-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcodercal.js
342 lines (305 loc) · 11.1 KB
/
codercal.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
'use strict';
const fs = require('fs');
const config = require('./config');
const bot = require('./bot.' + config.bot)(config.coderCalToken);
const data = require('./codercal.data')(config.coderCalPathCals);
const core = require('./codercal.core');
const fd = fs.openSync('log_codercal', 'a');
const log = (head, body) => {
fs.write(fd, '[' + Date() + '] ' + head + ' ' + body + '\n', () => {
// nothing
});
};
const event = (handler, atIndex) => {
return (msg, match) => {
if (!match[atIndex] || match[atIndex] === '@' + config.coderCalUsername) {
log(
msg.chat.id + '@' + (msg.chat.username || '')
+ ':' + msg.from.id + '@' + (msg.from.username || ''),
match[0]
);
// notice: take care of the inline query event
if (!config.ban[msg.from.id]) {
handler(msg, match);
}
}
};
};
const limitNum = (num, min, max) => {
return Math.min(Math.max(parseInt(num, 10), min), max);
};
// /calender <cal id> <title>
bot.onText(/^\/(calender|c) (\w+cal) ([^\n\r@]+)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('Calender', msg, [match[2], match[3]])
);
}, -1));
// /disable calender <cal id>
bot.onText(/^\/(disable calender|dc) (\w+cal)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('DisableCalender', msg, [match[2]])
);
}, -1));
// /dictionary <cal id> <dict id> <random>
// /dictionary <cal id> <dict id> x<pick>
bot.onText(/^\/(dictionary|d) (\w+cal) (\w+) (x)?(\d+)$/, event((msg, match) => {
if (match[4] === 'x') {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('DictionaryPick', msg, [match[2], match[3], limitNum(match[4], 1, 5)])
);
} else {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('DictionaryRandom', msg, [match[2], match[3], limitNum(match[4], 1, 100)])
);
}
}, -1));
// /item <cal id> <dict id> <item>
bot.onText(/^\/(item|i) (\w+cal) (\w+) ([^\n\r@]+)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('Item', msg, [match[2], match[3], match[4]])
);
}, -1));
// /delete item <cal id> <dict id> <item>
bot.onText(/^\/(delete item|di) (\w+cal) (\w+) ([^\n\r@]+)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('DeleteItem', msg, [match[2], match[3], match[4]])
);
}, -1));
// /activity <cal id> <name>@<good>@<bad>
// /activity <cal id> <name>@<good>@<bad>@weekday
// /activity <cal id> <name>@<good>@<bad>@weekend
bot.onText(/^\/(activity|a) (\w+cal) ([^\n\r@]+)@([^\n\r@]*)@([^\n\r@]*)(@weekday|@weekend)?$/, event((msg, match) => {
if (match[6] === '@weekday') {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('ActivityWeekday', msg, [match[2], match[3], match[4], match[5]])
);
} else if (match[6] === '@weekend') {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('ActivityWeekend', msg, [match[2], match[3], match[4], match[5]])
);
} else {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('Activity', msg, [match[2], match[3], match[4], match[5]])
);
}
}, -1));
// /delete activity <cal id> <name>
bot.onText(/^\/(delete activity|da) (\w+cal) ([^\n\r@]+)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('DeleteActivity', msg, [match[2], match[3]])
);
}, -1));
// /special <cal id> <name>@good@<good>@<month>/<day>
// /special <cal id> <name>@bad@<bad>@<month>/<day>
bot.onText(/^\/(special|s) (\w+cal) ([^\n\r@]+)@(good|bad)@([^\n\r@]*)@(\d+)\/(\d+)$/, event((msg, match) => {
if (match[4] === 'good') {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('SpecialGood', msg, [match[2], match[3], match[5], limitNum(match[6], 1, 12) * 100 + limitNum(match[7], 1, 31)])
);
} else if (match[4] === 'bad') {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('SpecialBad', msg, [match[2], match[3], match[5], limitNum(match[6], 1, 12) * 100 + limitNum(match[7], 1, 31)])
);
} else {
// never reach
throw Error();
}
}, -1));
// /delete special <cal id> <name>
bot.onText(/^\/(delete special|ds) (\w+cal) ([^\n\r@]+)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('DeleteSpecial', msg, [match[2], match[3]])
);
}, -1));
// /hint <cal id> <hint>
bot.onText(/^\/(hint|h) (\w+cal) ([^\n\r@]+)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('Hint', msg, [match[2], match[3]])
);
}, -1));
// /delete hint <cal id> <hint>
bot.onText(/^\/(delete hint|dh) (\w+cal) ([^\n\r@]+)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('DeleteHint', msg, [match[2], match[3]])
);
}, -1));
// /luck <luck id> <title>@<random>
bot.onText(/^\/(luck|l) (\w+luck) ([^\n\r@]+)@(\d+)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('Luck', msg, [match[2], match[3], limitNum(match[4], 1, 100)])
);
}, -1));
// /disable luck <luck id>
bot.onText(/^\/(disable luck|dl) (\w+luck)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('DisableLuck', msg, [match[2]])
);
}, -1));
// /rate <luck id> <name>@<weight>@<descrpiton>
bot.onText(/^\/(rate|r) (\w+luck) ([^\n\r@]+)@(\d+)@([^\n\r@]*)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('Rate', msg, [match[2], match[3], limitNum(match[4], 1, 10000), match[5]])
);
}, -1));
// /delete rate <luck id> <name>
bot.onText(/^\/(delete rate|dr) (\w+luck) ([^\n\r@]+)$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
data.writeCalAction('DeleteRate', msg, [match[2], match[3]])
);
}, -1));
bot.onText(/^\/help(@\w+)?$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
'程序员老黄历\n'
+ '\n'
+ '命令列表:\n'
+ '/calender <cal id> <title>\n'
+ '/disable calender <cal id>\n'
+ '/dictionary <cal id> <dict id> <random>\n'
+ '/dictionary <cal id> <dict id> x<pick>\n'
+ '/item <cal id> <dict id> <item>\n'
+ '/delete item <cal id> <dict id> <item>\n'
+ '/activity <cal id> <name>@<good>@<bad>\n'
+ '/activity <cal id> <name>@<good>@<bad>@weekday\n'
+ '/activity <cal id> <name>@<good>@<bad>@weekend\n'
+ '/delete activity <cal id> <name>\n'
+ '/special <cal id> <name>@good@<good>@<month>/<day>\n'
+ '/special <cal id> <name>@bad@<bad>@<month>/<day>\n'
+ '/delete special <cal id> <name>\n'
+ '/hint <cal id> <hint>\n'
+ '/delete hint <cal id> <hint>\n'
+ '/luck <luck id> <title>@<random>\n'
+ '/disable luck <luck id>\n'
+ '/rate <luck id> <name>@<weight>@<descrpiton>\n'
+ '/delete rate <luck id> <name>\n'
+ '/help 显示帮助\n'
+ '\n'
+ '备注:\n'
+ '<cal id> 必须以 cal 结尾\n'
+ '<luck id> 必须以 luck 结尾\n'
+ '除 /help 外,命令可以用首字母缩写\n'
+ '例如 /c 等同 /calender\n'
+ '/dc 等同 /disable calender\n'
+ '\n'
+ '源码:\n'
+ 'https://github.com/hczhcz/telegram-kuso-bots'
);
}, 1));
bot.on('inline_query', (query) => {
if (config.ban[query.from.id]) {
bot.answerInlineQuery(query.id, [{
type: 'article',
id: 'banned',
title: data.calenders[0].title + (data.suffix[core.getTodayInt() % 10000] || ''),
input_message_content: {
message_text: '该用户因存在恶意使用 bot 的报告,已被列入黑名单',
},
}], {
cache_time: 0,
is_personal: true,
});
} else {
const answers = [];
const addCalender = (calender) => {
const pickedEvents = core.pickEvents(
calender.dictionaries,
calender.activities,
calender.specials
);
let calText = calender.title + '\n'
+ core.getTodayString() + '\n'
+ '\n'
+ '宜:';
for (const i in pickedEvents.good) {
calText += '\n' + pickedEvents.good[i].name;
if (pickedEvents.good[i].description) {
calText += ' - ' + pickedEvents.good[i].description;
}
}
calText += '\n'
+ '\n'
+ '不宜:';
for (const i in pickedEvents.bad) {
calText += '\n' + pickedEvents.bad[i].name;
if (pickedEvents.bad[i].description) {
calText += ' - ' + pickedEvents.bad[i].description;
}
}
calText += '\n'
+ '\n'
+ core.pickHints(
calender.dictionaries,
calender.hints
).join('\n');
answers.push({
type: 'article',
id: calender.id,
title: calender.title + (data.suffix[core.getTodayInt() % 10000] || ''),
input_message_content: {
message_text: calText,
},
});
};
for (const i in data.calenders) {
if (data.calenders[i].title && data.calenders[i].title.indexOf(query.query) >= 0) {
addCalender(data.calenders[i]);
}
}
const addLuck = (luck) => {
const pickedLuck = core.pickLuck(luck.rates, luck.random, query);
let luckText = luck.title + '\n'
+ core.getTodayString() + '\n'
+ '\n'
+ '所求事项:' + query.query + '\n'
+ '结果:' + pickedLuck.name;
if (pickedLuck.description) {
luckText += ' - ' + pickedLuck.description;
}
answers.push({
type: 'article',
id: luck.id,
title: luck.title + (data.suffix[core.getTodayInt() % 10000] || ''),
input_message_content: {
message_text: luckText,
},
});
};
if (query.query) {
for (const i in data.lucks) {
if (data.lucks[i].title) {
addLuck(data.lucks[i]);
}
}
}
bot.answerInlineQuery(query.id, answers, {
cache_time: 0,
is_personal: true,
});
}
});
bot.on('chosen_inline_result', (chosen) => {
log(
'inline:' + chosen.from.id + '@' + (chosen.from.username || ''),
chosen.result_id + ' ' + chosen.query
);
});
data.loadCalActions();