forked from hczhcz/telegram-kuso-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1a2b.js
592 lines (523 loc) · 15.6 KB
/
1a2b.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
'use strict';
const fs = require('fs');
const config = require('./config');
const bot = require('./bot.' + config.bot)(config.abToken);
const multiplayer = require('./multiplayer')();
const play = require('./1a2b.play');
const fd = fs.openSync('log_1a2b', '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.abUsername) {
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 playerLine = (player) => {
if (player) {
return '\n'
+ '\n'
+ (
player.username
? '@' + player.username
: player.first_name
) + ' 轮到你啦';
}
return '';
};
const playerInfo = (list) => {
let info = '玩家列表:\n';
let total = 0;
for (let i = 0; i < list.length; i += 1) {
info += (list[i].username || list[i].first_name) + '\n';
total += 1;
}
info += '(总共' + total + '人)';
return info;
};
const playerUpdate = (msg, list) => {
if (list.update) {
list.update = () => {
playerUpdate(msg, list);
};
return;
}
list.update = () => {
// nothing
};
bot.editMessageText(
playerInfo(list) + '\n'
+ '\n'
+ '/1a2b@' + config.abUsername + ' 开始新游戏\n'
+ '/0a0b@' + config.abUsername + ' 结束游戏',
{
chat_id: msg.chat.id,
message_id: msg.message_id,
reply_to_message_id: msg.reply_to_message.message_id,
reply_markup: {
inline_keyboard: [[{
text: '加入',
callback_data: 'join',
}, {
text: '离开',
callback_data: 'flee',
}, {
text: '清空',
callback_data: 'clear',
}]],
},
}
).finally(() => {
setTimeout(() => {
const update = list.update;
delete list.update;
update();
}, config.multiplayerUpdateDelay);
});
};
const gameInfo = (guess, hint) => {
let info = '猜测历史:\n';
let total = 0;
for (const i in guess) {
info += i.slice(1) + ' ' + guess[i][0] + 'A' + guess[i][1] + 'B\n';
total += 1;
}
info += '(总共' + total + '次)\n'
+ '\n'
+ '猜测目标:\n'
+ hint;
return info;
};
const gameEnd = (game) => {
for (const i in game.msgs) {
const sentmsg = game.msgs[i];
bot.deleteMessage(sentmsg.chat.id, sentmsg.message_id);
}
delete game.msgs;
fs.write(fd, JSON.stringify(game) + '\n', () => {
// nothing
});
};
const gameEvent = event((msg, match) => {
play.guess(
msg.chat.id,
match[0],
(game) => {
// guess
bot.sendMessage(
msg.chat.id,
gameInfo(game.guess, game.hint) + playerLine(multiplayer.get(msg.chat.id)),
{
reply_to_message_id: msg.message_id,
}
).then((sentmsg) => {
if (game.msgs) {
game.msgs.push(sentmsg);
} else {
bot.deleteMessage(sentmsg.chat.id, sentmsg.message_id);
}
});
},
(game) => {
// game end
gameEnd(game);
bot.sendMessage(
msg.chat.id,
gameInfo(game.guess, game.charset) + '\n'
+ '\n'
+ '猜对啦!答案是:\n'
+ game.answer + '\n'
+ '\n'
+ '/1a2b@' + config.abUsername + ' 开始新游戏\n'
+ '/3a4b@' + config.abUsername + ' 多人模式',
{
reply_to_message_id: msg.message_id,
}
);
},
() => {
// guess duplicated
bot.sendMessage(
msg.chat.id,
'已经猜过啦',
{
reply_to_message_id: msg.message_id,
}
);
},
() => {
// game not exist
// never reach
throw Error(JSON.stringify(msg));
}
);
}, -1);
bot.onText(/^[^\n\r\s]+$/, (msg, match) => {
play.verify(
msg.chat.id,
match[0],
() => {
// valid
multiplayer.verify(
msg.chat.id,
msg.from,
() => {
// valid
gameEvent(msg, match);
},
() => {
// not valid
}
);
},
() => {
// not valid
},
() => {
// game not exist
}
);
});
bot.onText(/^\/1a2b(@\w+)?(?: ([^\0]+))?$/, event((msg, match) => {
play.init(
msg.chat.id,
match[2] || msg.reply_to_message && msg.reply_to_message.text || '',
msg.from.id,
(game) => {
// game init
game.msgs = [];
bot.sendMessage(
msg.chat.id,
'游戏开始啦,猜测目标:\n'
+ game.hint + playerLine(multiplayer.get(msg.chat.id)) + '\n'
+ '\n'
+ '将根据第一次猜测决定答案长度',
{
reply_to_message_id: msg.message_id,
}
);
},
() => {
// game exist
bot.sendMessage(
msg.chat.id,
'已经开始啦',
{
reply_to_message_id: msg.message_id,
}
);
}
);
}, 1));
bot.onText(/^\/3a4b(@\w+)?$/, event((msg, match) => {
multiplayer.add(
msg.chat.id,
msg.from,
(list) => {
// added
bot.sendMessage(
msg.chat.id,
'一大波玩家正在赶来……',
{
reply_to_message_id: msg.message_id,
reply_markup: {
inline_keyboard: [[{
text: '加入',
callback_data: JSON.stringify(['join']),
}, {
text: '离开',
callback_data: JSON.stringify(['flee']),
}, {
text: '清空',
callback_data: JSON.stringify(['clear']),
}]],
},
}
).then((sentmsg) => {
playerUpdate(
sentmsg,
list
);
});
},
(list) => {
// player exist
bot.sendMessage(
msg.chat.id,
'一大波玩家正在赶来……',
{
reply_to_message_id: msg.message_id,
reply_markup: {
inline_keyboard: [[{
text: '加入',
callback_data: JSON.stringify(['join']),
}, {
text: '离开',
callback_data: JSON.stringify(['flee']),
}, {
text: '清空',
callback_data: JSON.stringify(['clear']),
}]],
},
}
).then((sentmsg) => {
playerUpdate(
sentmsg,
list
);
});
},
(list) => {
// list full
bot.sendMessage(
msg.chat.id,
'玩家列表满啦',
{
reply_to_message_id: msg.message_id,
}
);
}
);
}, 1));
bot.onText(/^\/0a0b(@\w+)?$/, event((msg, match) => {
play.end(
msg.chat.id,
(game) => {
// game end
gameEnd(game);
if (game.answer) {
bot.sendMessage(
msg.chat.id,
gameInfo(game.guess, game.charset) + '\n'
+ '\n'
+ '游戏结束啦,答案是:\n'
+ game.answer + '\n'
+ '\n'
+ '/1a2b@' + config.abUsername + ' 开始新游戏\n'
+ '/3a4b@' + config.abUsername + ' 多人模式',
{
reply_to_message_id: msg.message_id,
}
);
} else {
bot.sendMessage(
msg.chat.id,
'游戏结束啦\n'
+ '\n'
+ '/1a2b@' + config.abUsername + ' 开始新游戏\n'
+ '/3a4b@' + config.abUsername + ' 多人模式',
{
reply_to_message_id: msg.message_id,
}
);
}
},
() => {
// game not exist
bot.sendMessage(
msg.chat.id,
'不存在的!\n'
+ '\n'
+ '/1a2b@' + config.abUsername + ' 开始新游戏\n'
+ '/3a4b@' + config.abUsername + ' 多人模式',
{
reply_to_message_id: msg.message_id,
}
);
}
);
}, 1));
bot.onText(/^\/help(@\w+)?$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
'1A2B 猜数游戏\n'
+ '\n'
+ '命令列表:\n'
+ '/1a2b 开始新游戏\n'
+ '/1a2b <target> 指定猜测目标开始新游戏\n'
+ '/3a4b 多人模式\n'
+ '/0a0b 结束游戏\n'
+ '/help 显示帮助\n'
+ '/status 查看 bot 状态\n'
+ '\n'
+ '备注:\n'
+ '/1a2b 可将回复的消息设为猜测目标\n'
+ 'inline 查询将触发喵喵模式\n'
+ '多行的猜测目标将触发喵喵模式\n'
+ '\n'
+ '源码:\n'
+ 'https://github.com/hczhcz/telegram-kuso-bots'
);
}, 1));
bot.onText(/^\/status(@\w+)?$/, event((msg, match) => {
bot.sendMessage(
msg.chat.id,
'当前活跃游戏 ' + play.count(),
{
reply_to_message_id: msg.message_id,
}
);
}, 1));
bot.on('callback_query', (query) => {
const msg = query.message;
if (!msg || config.ban[query.from.id]) {
return;
}
if (query.data === 'join') {
log(
msg.chat.id + '@' + (msg.chat.username || '')
+ ':callback:' + query.from.id + '@' + (query.from.username || ''),
'join'
);
multiplayer.add(
msg.chat.id,
query.from,
(list) => {
// added
playerUpdate(
msg,
list
);
bot.answerCallbackQuery(query.id).catch((err) => {
// nothing
});
},
(list) => {
// player exist
bot.answerCallbackQuery(query.id).catch((err) => {
// nothing
});
},
(list) => {
// list full
bot.answerCallbackQuery(query.id).catch((err) => {
// nothing
});
}
);
} else if (query.data === 'flee') {
log(
msg.chat.id + '@' + (msg.chat.username || '')
+ ':callback:' + query.from.id + '@' + (query.from.username || ''),
'flee'
);
multiplayer.remove(
msg.chat.id,
query.from,
(list) => {
// removed
playerUpdate(
msg,
list
);
bot.answerCallbackQuery(query.id).catch((err) => {
// nothing
});
},
() => {
// player not exist
bot.answerCallbackQuery(query.id).catch((err) => {
// nothing
});
}
);
} else if (query.data === 'clear') {
log(
msg.chat.id + '@' + (msg.chat.username || '')
+ ':callback:' + query.from.id + '@' + (query.from.username || ''),
'clear'
);
multiplayer.clear(
msg.chat.id,
() => {
// cleared
playerUpdate(
msg,
[]
);
bot.answerCallbackQuery(query.id).catch((err) => {
// nothing
});
},
() => {
// not multiplayer
bot.answerCallbackQuery(query.id).catch((err) => {
// nothing
});
}
);
}
});
bot.on('inline_query', (query) => {
if (!query.query) {
bot.answerInlineQuery(
query.id,
[],
{
cache_time: 0,
is_personal: true,
}
);
} else if (config.ban[query.from.id]) {
bot.answerInlineQuery(
query.id,
[{
type: 'article',
id: 'banned',
title: '喵a喵b',
input_message_content: {
message_text: '该用户因存在恶意使用 bot 的报告,已被列入黑名单',
},
}],
{
cache_time: 0,
is_personal: true,
}
);
} else {
bot.answerInlineQuery(
query.id,
[{
type: 'article',
id: 'playmeow',
title: '喵a喵b',
input_message_content: {
message_text: (
query.from.username
? '@' + query.from.username
: query.from.first_name
) + ' 喵喵模式已装载!\n'
+ '\n'
+ '/1a2b@' + config.abUsername + ' 开始新游戏\n'
+ '/3a4b@' + config.abUsername + ' 多人模式\n'
+ '/0a0b@' + config.abUsername + ' 结束游戏',
},
}],
{
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
);
if (chosen.result_id === 'playmeow') {
play.meowInit(chosen.from.id, chosen.query);
}
});