forked from Tsuk1ko/cq-picsearcher-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
1778 lines (1698 loc) · 79.9 KB
/
main.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
version
} from './package.json';
import bot from './bot';
import config from './modules/config';
import saucenao from './modules/saucenao';
import {
snDB
} from './modules/saucenao';
//import { globalReg } from './modules/utils/global';
import whatanime from './modules/whatanime';
import ascii2d from './modules/ascii2d';
import CQ from './modules/CQcode';
import Logger from './modules/Logger';
import PFCache from './modules/cache';
import RandomSeed from 'random-seed';
import sendSetu from './modules/plugin/setu';
import ocr from './modules/plugin/ocr';
import Akhr from './modules/plugin/akhr';
//import _, { random} from 'lodash';
import minimist from 'minimist';
import {
rmdInit,
rmdHandler
} from './modules/plugin/reminder';
import logger2 from './modules/logger2';
import schedule from 'node-schedule';
import node_localStorage2 from 'node-localstorage';
import dayjs from 'dayjs';
import broadcast from './modules/broadcast';
import bilibili from './modules/plugin/bilibili';
//import sign from './modules/sign';
import logError from './modules/logError';
import NodeCache from 'node-cache';
import path from 'path';
import fs from 'fs';
import JOIN from 'path'
const join = JOIN.join;
//常量
const ext = ['', '.jpeg', '.gif', '.png', '.jpg', '.mp4'];
const node_localStorage = node_localStorage2.LocalStorage;
const qiandaosuo = new node_localStorage('../qiandaosuo'); //跨插件签到锁,转推时禁止签到
const ocrspace = new node_localStorage('./ocrspace');
const ascii2dday = new node_localStorage('./ascii2dday');
const qiandaotu = new node_localStorage('./qiandaotu');
const setting = config.picfinder;
const rand = RandomSeed.create();
const searchModeOnReg = new RegExp(setting.regs.searchModeOn);
const searchModeOffReg = new RegExp(setting.regs.searchModeOff);
const signReg = new RegExp(setting.regs.sign);
const signReg2 = new RegExp(setting.regs.sign2);
const huluezifu = setting.replys.huluezifu;
const bangzhuzhiling1 = setting.replys.bangzhuzhiling1;
const bangzhuzhiling2 = setting.replys.bangzhuzhiling2;
const mingling1 = setting.replys.mingling1;
const mingling2 = setting.replys.mingling2;
const qiandaoxianzhishu = setting.sign.qiandaoxianzhishu;
const chouqianxianzhishu = setting.sign.chouqianxianzhishu;
const signdelay = setting.sign.delay * 1000;
var qiandaotupianjishu = 0; //签到总数限制
var chouqiantupianjishu = 0; //抽签总数限制
const cache2 = new NodeCache({
stdTTL: 1 * 180 //秒
});
const cache3 = new NodeCache({
stdTTL: 1 * 2 //秒
});
//初始化
var pic1 = -1;
const pfcache = setting.cache.enable ? new PFCache() : null;
const logger = new Logger();
let t = new Date();
logger2.info('搜图插件,' + t.toString() + dayjs(t.toString()).format(' A 星期d'));
/**
*
* @param startPath 起始目录文件夹路径
* @returns {Array}
*/
//https://www.imooc.com/wenda/detail/459466 nodejs的FS或path如何获取某文件夹下的所有文件的文件名呢。
function findSync(startPath, jishu = false) {
let isFile_result = [];
//let isDirectory_result = [];
function finder(pathx, isdirectory = false) {
let files = fs.readdirSync(pathx);
files.forEach((val, index) => {
let fPath = join(pathx, val);
let stats = fs.statSync(fPath);
if (jishu == false) {
if (stats.isDirectory() == true) {
finder(fPath, true);
//isDirectory_result.push(fPath);
//logger2.info("是文件夹" + fPath)
}
else if (stats.isFile() == true && isdirectory == true) {
//logger.info(fPath);
isFile_result.push(fPath);
//logger2.info("是文件夹中的文件" + fPath)
}
else if (stats.isFile() == true) {
//logger.info(fPath);
isFile_result.push(fPath);
//logger2.info("是文件" + fPath)
}
} else {
let extname = path.extname(fPath).split(".")[1];
if (extname != "txt") {//计算图片视频文件数,排除txt说明文件
isFile_result.push(fPath);
//logger2.info("计算图片视频文件数,排除txt说明文件:" + fPath)
}
}
});
}
finder(startPath);
return isFile_result;
}
async function start() {
if (setting.akhr.enable) Akhr.init().catch(console.error);
if (setting.reminder.enable) rmdInit(replyMsg);
pic1 = await new Promise(function (resolve, reject) {
resolve(findSync('./tuku', true).length);
});
logger2.info("签到图数:" + pic1);
//好友请求
bot.on('request', context => {
if (context.request_type === 'friend') {
let approve = setting.autoAddFriend;
const answers = setting.addFriendAnswers;
if (approve && answers.length > 0) {
const comments = context.comment.split('\n');
try {
answers.forEach((ans, i) => {
const a = /(?<=回答:).*/.exec(comments[i * 2 + 1])[0];
if (ans != a) approve = false;
});
} catch (e) {
console.error(e);
let t = new Date();
logger2.info(t.toString() + ",好友申请:" + e);
approve = false;
}
}
if (approve)
bot('set_friend_add_request', {
flag: context.flag,
sub_type: 'invite',
approve: true,
});
}
});
//加群请求
const groupAddRequests = {};
bot.on('request', context => {
if (context.request_type === 'group') {
if (context.sub_type === 'invite') {
if (setting.autoAddGroup)
bot('set_group_add_request', {
flag: context.flag,
approve: true,
});
else groupAddRequests[context.group_id] = context.flag;
}
}
});
//管理员指令
bot.on('message', context => {
if (context.message_type == "private") {
if (context.user_id != setting.admin) return false;
const args = parseArgs(context.message);
//允许加群
const group = args['add-group'];
if (group && typeof group == 'number') {
if (typeof groupAddRequests[context.group_id] == 'undefined') {
replyMsg(context, `将会同意进入群${group}的群邀请`);
//注册一次性监听器
bot.once('request', context2 => {
if (context2.request_type === 'group') {
if (context2.sub_type === 'invite') {
if (context2.group_id == group) {
bot('set_group_add_request', {
flag: context2.flag,
type: 'invite',
approve: true,
});
replyMsg(context, `已进入群${context2.group_id}`);
return true;
}
return false;
}
}
});
} else {
bot('set_group_add_request', {
flag: groupAddRequests[context.group_id],
type: 'invite',
approve: true,
});
replyMsg(context, `已进入群${context2.group_id}`);
delete groupAddRequests[context.group_id];
}
}
if (args.broadcast) broadcast(bot, parseArgs(context.message, false, 'broadcast')); //群发消息功能
//Ban
const {
'ban-u': bu,
'ban-g': bg
} = args;
if (bu && typeof bu == 'number') {
Logger.ban('u', bu);
replyMsg(context, `已封禁用户${bu}`);
}
if (bg && typeof bg == 'number') {
Logger.ban('g', bg);
replyMsg(context, `已封禁群组${bg}`);
}
//明日方舟
if (args['update-akhr'])
Akhr.updateData()
.then(() => replyMsg(context, '方舟公招数据已更新'))
.catch(e => {
logError(e);
let t = new Date();
logger2.info(t.toString() + ",方舟公招数据更新:" + e);
replyMsg(context, '方舟公招数据更新失败,请查看错误日志');
});
//停止程序(利用pm2重启)
if (args.shutdown) {
replyMsg(context, '搜图已关闭!');
process.exit(); //并没有使用pm2
}
}
});
//设置监听器
if (setting.debug) {
//私聊
if (setting.enablePM) {
bot.on('message', debugRrivateAndAtMsg);
}
//讨论组@
//bot.on('message.discuss.@me', debugRrivateAndAtMsg);
//群组@
if (setting.enableGM) {
bot.on('message', debugGroupMsg);
}
} else {
//私聊
if (setting.enablePM) {
bot.on('message', privateAndAtMsg);
}
//讨论组@
//bot.on('message.discuss.@me', privateAndAtMsg);
//群组@
//群组
if (setting.enableGM) {
bot.on('message', groupMsg);
}
}
/*
{"app_enabled":true,"app_good":true,"app_initialized":true,"good":true,"online":true,"plugins_good":null,"stat":{"PacketReceived":43,"PacketSent":34,"PacketLost":0,"MessageReceived":0,"MessageSent":0,"LastMessageTime":0,"DisconnectTimes":0,"LostTimes":0}}
get_status 在go-cqhttp v1.0.0-rc1 有关键词变化
*/
//连接相关监听
bot('get_status').then(data1 => {
bot('get_version_info').then(data2 => {
//https://www.jb51.net/article/134067.htm js保留两位小数方法总结
//> 注意: 所有统计信息都将在重启后重制
let stats = `接受包: ${data1.stat.packet_received || data1.stat.PacketReceived} , 发送包: ${data1.stat.packet_sent || data1.stat.PacketSent} , 丢包: ${data1.stat.packet_lost || data1.stat.PacketLost} , 丢包率:${(data1.stat.packet_lost || data1.stat.PacketLost / (data1.stat.packet_lost || data1.stat.PacketLost + data1.stat.packet_sent || data1.stat.PacketSent) * 100).toFixed(3)}
接受消息: ${data1.stat.message_received || data1.stat.MessageReceived} , 发送消息: ${data1.stat.message_sent || data1.stat.MessageSent} , TCP链接断开: ${data1.stat.disconnect_times || data1.stat.DisconnectTimes} , 账号掉线: ${data1.stat.lost_times || data1.stat.LostTimes}`;
logger2.info("get_status: " + JSON.stringify(data1) + "\n" + "get_version_info" + JSON.stringify(data2))
logger2.info("go-cqhttp在线中:" + data1.online + "\n" + "go-cqhttp版本:" + data2.version + "\n" + "go语言版本:" + data2.runtime_version + "\n" + "cqhttp版本:" + data2.plugin_version + "\n" + "搜图插件版本:" + version + "\n数据统计:" + stats)
bot('send_private_msg', {
user_id: setting.admin,
message: "搜图插件已启动\ngo-cqhttp在线中:" + data1.online + "\n" + "go-cqhttp版本:" + data2.version + "\n" + "go语言版本:" + data2.runtime_version + "\n" + "cqhttp版本:" + data2.plugin_version + "\n" + "搜图插件版本:" + version + "\n数据统计:" + stats
});
}).catch(err => {
logger.error(new Date().toString() + "get_status:" + JSON.stringify(err));
});
}).catch(err => {
try {
logger.error(new Date().toString() + "get_version_info1:" + JSON.stringify(err));
} catch (e) {
logger.error(new Date().toString() + "get_version_info2:" + err);
}
});
/*.then(data => {}).catch(err => {
logger2.error(new Date().toString() + "," + err);
logger2.error("未连接上go-cqhttp,退出程序 ");
process.exit();
});*/
//http://nodejs.cn/learn/how-to-exit-from-a-nodejs-program 如何从 Node.js 程序退出
//自动帮自己签到(诶嘿
//以及每日需要更新的一些东西
//setInterval(() => {
//logger2.info("管理员签到1");
//if (bot.isReady() && logger.canAdminSign()) { //
//logger2.info("管理员签到2");
//setTimeout(() => {
/*if (setting.admin > 0 && logger.canSign(setting.admin) == true) {
//logger2.info("管理员签到3");
}*/
//更新明日方舟干员数据
// if (setting.akhr.enable) Akhr.updateData();
// }, 60 * 1000);
// }
//}, 60 * 60 * 1000);
//通用处理
function commonHandle(context) {
//黑名单检测
if (Logger.checkBan(context.user_id, context.group_id)) return true;
//兼容其他机器人
const startChar = context.message.charAt(0);
if (startChar == '/' || startChar == '<') return true;
//通用指令
const args = parseArgs(context.message);
if (args.help) {
replyMsg(context, 'https://github.com/Tsuk1ko/CQ-picfinder-robot/wiki/%E5%A6%82%E4%BD%95%E9%A3%9F%E7%94%A8');
return true;
}
if (args.version) {
/*
{"app_enabled":true,"app_good":true,"app_initialized":true,"good":true,"online":true,"plugins_good":null}
{"coolq_directory":"/home/user/coolq/gocqhttp","coolq_edition":"pro","go-cqhttp":true,"plugin_build_configuration":"release","plugin_build_number":99,"plugin_version":"4.15.0","runtime_os":"linux","runtime_version":"go1.14.7"}
{"app_enabled":true,"app_good":true,"app_initialized":true,"good":true,"online":true,"plugins_good":null,"stat":{"packet_received":66,"packet_sent":62,"packet_lost":2,"message_received":0,"message_sent":1,"disconnect_times":0,"lost_times":0}},"retcode":0,"status":"ok"}
旧版本的
*/
bot('get_status').then(data1 => {
bot('get_version_info').then(data2 => {
let stats = `接受包: ${data1.stat.packet_received} , 发送包: ${data1.stat.packet_sent} , 丢包: ${data1.stat.packet_lost} , 丢包率:${(data1.stat.packet_lost / (data1.stat.packet_lost + data1.stat.packet_sent) * 100).toFixed(3)}%
接受消息: ${data1.stat.message_received} , 发送消息: ${data1.stat.message_sent} , TCP链接断开: ${data1.stat.disconnect_times} , 账号掉线: ${data1.stat.lost_times}`;
logger2.info("get_status: " + JSON.stringify(data1) + "\n" + "get_version_info" + JSON.stringify(data2))
logger2.info("go-cqhttp在线中:" + data1.online + "\n" + "go-cqhttp版本:" + data2.version + "\n" + "go语言版本:" + data2.runtime_version + "\n" + "cqhttp版本:" + data2.plugin_version + "\n" + "搜图插件版本:" + version + "\n数据统计:" + stats)
replyMsg(context, "搜图插件已启动\ngo-cqhttp在线中:" + data1.online + "\n" + "go-cqhttp版本:" + data2.version + "\n" + "go语言版本:" + data2.runtime_version + "\n" + "cqhttp版本:" + data2.plugin_version + "\n" + "搜图插件版本:" + version);
replyMsg(context, "数据统计:" + stats);
}).catch(err => {
logger.error(new Date().toString() + "get_status:" + JSON.stringify(err));
});
}).catch(err => {
logger.error(new Date().toString() + "get_version_info:" + JSON.stringify(err));
});
return true;
}
if (args.about) {
replyMsg(context, 'https://github.com/Tsuk1ko/CQ-picfinder-robot');
return true;
}
//setu
if (setting.setu.enable) {
if (sendSetu(context, replyMsg, logger, bot)) return true;
}
//reminder
if (setting.reminder.enable) {
if (rmdHandler(context)) return true;
}
// 反哔哩哔哩小程序
bilibili(context, replyMsg);
return false;
}
//私聊以及群组@的处理
var privateqq = new Array(); //针对QQ号延时保护
function privateAndAtMsg(context) {
let temp = context.message.split("CQ:at,qq=");
let temp3 = -1;
// logger2.info(temp.length + ",原始:" + temp);
if (temp.length == 2) {
let temp2 = temp[1].split("]");
//logger2.info(temp2.length + ",原始2:" + temp2[0]);
if (temp2.length >= 2) {
temp3 = parseInt(temp2[0]);
}
}
//logger2.info("目标QQ号:" + temp3);
//限制为好友私聊有效
if (((context.message_type == "private" && context.sub_type == "friend") /* || context.user_id == setting.admin*/) || (context.message.toString().search("CQ:at,qq=") != -1 && temp3 == context.self_id && temp3 != -1 && context.message_type == "group")) {
let uid = context.user_id;
if (uid) {
let cacheKeys = [`${uid}-${true}`]; //防御私聊狂刷
if (cacheKeys.some(key => cache3.has(key))) {
return;
} else {
[true].forEach((id, i) => id && cache3.set(cacheKeys[i], true));
}
} else {
return;
}
if (commonHandle(context)) {
//e.stopPropagation();
return;
}
//logger2.info("66666666666666666666666666666");
//logger2.info(JSON.stringify(context));
//暂时禁掉几乎所有私聊
if (context.message == '。搜图') {
//e.stopPropagation();
returnmsg(context, 0);
return;
}
if (context.message == 'ocr' || context.message == 'OCR') {
//e.stopPropagation();
returnmsg(context, 1);
return;
}
if (hasImage(context.message)) {
//搜图
//e.stopPropagation();
searchImg(context);
/*} else if (signReg.exec(context.message)) {
//签到
//e.stopPropagation();
if (logger.canSign(context.user_id)) {
bot('send_like', {
user_id: context.user_id,
times: 10,
});
return setting.replys.sign;
} else return setting.replys.signed;*/
} else if (context.message.toString().search('--') != -1) {
return;
} else if (!context.group_id && !context.discuss_id) {
const db = snDB[context.message];
if (db) {
logger.smSwitch(0, context.user_id, true);
logger.smSetDB(0, context.user_id, db);
replyMsg(context, `已临时切换至[${context.message}]搜图模式√`);
return;
} else {
if (context.message_type == 'private') {
let ss = context.message;
let has = false;
for (let i = 0; i < huluezifu.length; i++) {
if (ss == huluezifu[i]) {
has = true;
break;
}
}
if (has == false) {
//e.stopPropagation();
if (privateqq[context.user_id.toString()] == null) {
privateqq[context.user_id.toString()] = true;
let t = setTimeout(() => {
clearTimeout(t);
privateqq[context.user_id.toString()] = null;
}, 60000);
if (context.sub_type == "friend") {
replyMsg(context, setting.replys.default);
} else {
//replyMsg(context, setting.replys.bangzhuzhiling0);
}
}
}
}
}
} else {
//其他指令
if (context.message_type == 'private') {
let ss = context.message;
let has = false;
for (let i = 0; i < s.length; i++) {
if (ss == s[i]) {
has = true;
break;
}
}
if (has == false) {
//e.stopPropagation();
if (privateqq[context.user_id.toString()] == null) {
privateqq[context.user_id.toString()] = true;
let t = setTimeout(() => {
clearTimeout(t);
privateqq[context.user_id.toString()] = null;
}, 60000);
if (context.sub_type == "friend") {
replyMsg(context, setting.replys.default);
} else {
//replyMsg(context, setting.replys.bangzhuzhiling0);
}
}
}
}
}
}
}
//调试模式
function debugRrivateAndAtMsg(context) {
if ((context.message_type == "private" || (context.message.toString().search('CQ:at,qq=') != -1 && context.message_type == "group") && context.sub_type == "friend")) {
if (context.user_id != setting.admin) {
//e.stopPropagation();
replyMsg(context, setting.replys.debug);
return;
}
logger2.info(`${getTime()} 私聊消息:` + context.message);
return privateAndAtMsg(context);
}
}
function debugGroupMsg(context) {
if (context.message_type == "group") {
if (context.user_id != setting.admin) {
//e.stopPropagation();
return;
} else {
logger2.info(`${getTime()} 群组消息:` + context.message);
return groupMsg(context);
}
}
}
//群组消息处理
var qiandaoxianzhi = false;
async function groupMsg(context) {
if (context.message_type == "group") {
//logger2.info(JSON.stringify(context));
let uid = context.user_id;
let cacheKeys = `${uid}`; //防御群聊狂刷,计数制
let cacheKeys2 = [`${uid}-${true}`]; //防御群聊狂刷,延时2秒
let sign = false;//是否是签到
let temp2 = 0;
let temp = qiandaotu.getItem('jishu');
if (uid) {
if ( /*cache2.has(cacheKeys)*/ cache2.get(cacheKeys) == 0) {
return;
}
if (cacheKeys2.some(key => cache3.has(key))) {
return;
} else {
[true].forEach((id, i) => id && cache3.set(cacheKeys2[i], true));
}
//logger2.info(uid + ": " + cache2.get(cacheKeys));
} else {
return;
}
if (commonHandle(context)) {
//e.stopPropagation();
return;
}
if (context.message == '。搜图') {
//e.stopPropagation();
cache(uid, true);
returnmsg(context, 0);
return;
}
if (context.message == 'ocr' || context.message == 'OCR') {
//e.stopPropagation();
cache(uid, true);
returnmsg(context, 1);
return;
}
//if (logger.canSign(context.user_id)) {
if (signReg.exec(context.message.trim())) {
//签到
//e.stopPropagation();
let blackgroup = false;
let blackgroup2 = setting.sign.blackgroup;
let i = 0;
for (i = 0; i < blackgroup2.length; i++) {
if (context.group_id == blackgroup2[i]) {
blackgroup = true;
break;
}
}
if (qiandaoxianzhi == false && (qiandaosuo.getItem("qiandaosuo") == "false" || qiandaosuo.getItem("qiandaosuo") == undefined) && blackgroup == false) {
qiandaoxianzhi = true;
let t = setTimeout(() => {
clearInterval(t);
qiandaoxianzhi = false;
}, signdelay);
cache(uid, true);
qiandaotupianjishu++;
logger2.info("签到最大限制数(-1等于不限制):" + qiandaoxianzhishu);
logger2.info("签到数:" + qiandaotupianjishu);
//签到和抽签功能基本合并了,只剩统计和限制功能
if (qiandaotupianjishu <= qiandaoxianzhishu || qiandaoxianzhishu == -1) { //签到总数限制
if (temp == null || parseInt(temp) == pic1) {
qiandaotu.setItem('jishu', "0");
} else {
temp2 = parseInt(qiandaotu.getItem('jishu'));
temp2++;
qiandaotu.setItem('jishu', temp2);
}
sign = true;
}
}
} else if (signReg2.exec(context.message.trim())) {
//抽签
//e.stopPropagation();
let blackgroup = false;
let blackgroup2 = setting.sign.blackgroup;
let i = 0;
for (i = 0; i < blackgroup2.length; i++) {
if (context.group_id == blackgroup2[i]) {
blackgroup = true;
break;
}
}
if (qiandaoxianzhi == false && (qiandaosuo.getItem("qiandaosuo") == "false" || qiandaosuo.getItem("qiandaosuo") == undefined) && blackgroup == false) {
qiandaoxianzhi = true;
let t = setTimeout(() => {
clearInterval(t);
qiandaoxianzhi = false;
}, signdelay);
cache(uid, true);
chouqiantupianjishu++;
logger2.info("抽签最大限制数(-1等于不限制):" + chouqianxianzhishu);
logger2.info("抽签数:" + chouqiantupianjishu);
if (chouqiantupianjishu <= chouqianxianzhishu || chouqianxianzhishu == -1) { //抽签总数限制
temp2 = getIntRand(pic1);//从0-总签到图数中选一个数字
sign = true;
}
}
}
//还有补一个抽指定签
///^(签到|抽签)(\d+)$/.exec("签到1")
let s = /^(签到|抽签)(\d+)$/.exec(context.message.trim());
if (s != null) {
logger2.info("抽指定签" + s[2]);
let n = parseInt(s[2]);
if (n <= pic1 && n >= 0) {
temp2 = n;
sign = true;
}
}
if (sign == true) {
let pictemp = null;
let tmp = "";
let tmp2 = "";
let leixing0 = "";//储存主目录中的图片视频路径(其实是文件后缀名,只有一个)
let leixing1 = new Array();//储存次级目录中的图片视频路径(真的是路径,文件数不限)
let result0 = ""//储存说明文本
let result1 = [];//储存合并转发内容
if (pic1 != -1) {
pictemp = path.join(__dirname, "./tuku/" + temp2);
//检查文件是否存在于当前目录中
logger2.info(ext.length);
for (let index = 0; index < ext.length; index++) {
let item = ext[index];
logger2.info(ext[index]);
tmp = pictemp + item
tmp2 = await new Promise(function (resolve, reject) {
fs.access(`${tmp}`, fs.constants.F_OK, err => {
if (err) {
logger2.info(tmp + ',不存在于当前目录中')
resolve(false)
}
else {
logger2.info(tmp + ',存在于当前目录中')
resolve(true)
}
})
});
if (tmp2 == true) {
if (item == "") {
//是文件夹,进入目录搜索一遍,获取文件路径
leixing1 = await new Promise(function (resolve, reject) {
resolve(findSync(tmp));//返回数组
});
result0 = await new Promise(function (resolve, reject) {
fs.readFile(`${path.join(pictemp + "/" + temp2 + ".txt")}`, function (err, data) {
if (err) {
logger2.info("读取签到说明失败:" + err);
resolve("");
}
else {
logger2.info("读取签到说明成功:" + data.toString());
resolve(data.toString());
}
})
});
break;
}
leixing0 = item;
//尝试读取签到说明
result0 = await new Promise(function (resolve, reject) {
fs.readFile(`${pictemp + ".txt"}`, function (err, data) {
if (err) {
logger2.info("读取签到说明失败:" + err);
resolve("");
}
else {
logger2.info("读取签到说明成功:" + data.toString());
resolve(data.toString());
}
})
});
break;
}
logger2.info(tmp + ":" + tmp2);
/*
作者:静昕妈妈芦培培
链接:https://www.jianshu.com/p/44b37920f837
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/
}
//统一规定若签到图是文件夹,则所有文件在文件夹里,txt的文件名=上级文件夹,图片用1,2,3,4
if (leixing0 != "") {
if (leixing0 == ".jpg"||leixing0 == ".jpeg"||leixing0 == ".png"||leixing0 == ".gif") {
replyMsg(context, `[CQ:at,qq=${context.user_id}]` + setting.replys.sign + `\n[CQ:image,file=file:///${pictemp + leixing0}]\n` + temp2)
}
else {
replyMsg(context, `[CQ:at,qq=${context.user_id}]` + setting.replys.sign + temp2)
replyMsg(context, `[CQ:video,cache=0,file=file:///${pictemp + leixing0},c=3]`)
//视频会吞掉所有文本,只能单独发
}
if (result0 != "") {
replyMsg(context, `${result0}`)
}
}
else if (leixing1.length != 0) {
let extname = ""
replyMsg(context, `[CQ:at,qq=${context.user_id}]` + setting.replys.sign + temp2)
for (let index = 0; index < leixing1.length; index++) {
let item2 = leixing1[index]
//获取文件的后缀名
extname = path.extname(item2).split(".")[1]
logger2.info("item2:" + item2 + ",extname:" + extname)
if (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "gif") {
result1.push(`[CQ:image,file=file:///${item2}]\n`)
logger2.info(`[CQ:image,file=file:///${item2}]\n`)
}
else if (extname == "mp4") {
//视频无法合并转发?
result1.push(`[CQ:video,cache=0,file=file:///${item2},c=3}]`)
logger2.info(`[CQ:video,cache=0,file=file:///${item2},c=3}]`)
//result1.push(`[CQ:video,cache=0,file=file:///${item2},c=3,cover=file:///${path.join(__dirname, "./black.jpg")}]`)
//logger2.info(`[CQ:video,cache=0,file=file:///${item2},c=3,cover=file:///${path.join(__dirname, "./black.jpg")}]`)
}
}
if (result0 != "") {
result1.push(`${result0}`)
logger2.info(`result0:${result0}`)
}
logger2.info("result1:" + result1)
sendGroupForwardMsg(context, result1)
}
else {
logger2.info("失败的一次签到:" + temp2);
}
return true;
}
}
// }
//else {
// logger2.info("该用户名还不能签到:" + context.user_id)
// }
}
return true;
//进入或退出搜图模式
const {
group_id,
user_id
} = context;
if (searchModeOnReg.exec(context.message)) {
//进入搜图
//e.stopPropagation();
cache(uid, true);
if (
logger.smSwitch(group_id, user_id, true, () => {
replyMsg(context, setting.replys.searchModeTimeout, true);
})
)
replyMsg(context, setting.replys.searchModeOn, true);
else replyMsg(context, setting.replys.searchModeAlreadyOn, true);
} else if (searchModeOffReg.exec(context.message)) {
//e.stopPropagation();
//退出搜图
cache(uid, true);
if (logger.smSwitch(group_id, user_id, false)) replyMsg(context, setting.replys.searchModeOff, true);
else replyMsg(context, setting.replys.searchModeAlreadyOff, true);
}
//搜图模式检测
let smStatus = logger.smStatus(group_id, user_id);
if (smStatus) {
//获取搜图模式下的搜图参数
const getDB = () => {
let cmd = /^(all|pixiv|danbooru|book|anime)$/.exec(context.message);
if (cmd) return snDB[cmd[1]] || -1;
return -1;
};
//切换搜图模式
const cmdDB = getDB();
if (cmdDB !== -1) {
logger.smSetDB(group_id, user_id, cmdDB);
smStatus = cmdDB;
replyMsg(context, `已切换至[${context.message}]搜图模式√`);
}
//有图片则搜图
if (hasImage(context.message)) {
//刷新搜图TimeOut
logger.smSwitch(group_id, user_id, true, () => {
replyMsg(context, setting.replys.searchModeTimeout, true);
});
//e.stopPropagation();
searchImg(context, smStatus);
}
} else if (setting.repeat.enable) {
//复读(
//随机复读,rptLog得到当前复读次数
cache(uid, true);
if (logger.rptLog(group_id, user_id, context.message) >= setting.repeat.times && getRand() <= setting.repeat.probability) {
logger.rptDone(group_id);
//延迟2s后复读
let t = setTimeout(() => {
clearTimeout(t);
replyMsg(context, context.message);
}, 2000);
} else if (getRand() <= setting.repeat.commonProb) {
//平时发言下的随机复读
let t = setTimeout(() => {
clearTimeout(t);
replyMsg(context, context.message);
}, 2000);
}
}
}
function cache(uid, cache = false) {
if (cache == true) {
if (cache2.get(uid) == undefined) {
cache2.set(uid, 20);
} else {
let temp = cache2.get(uid) - 1;
cache2.set(uid, temp);
}
logger2.info(uid + ": " + cache2.get(uid));
}
}
//通用信息发送
function returnmsg(context, xuanze) {
//console.log(context);
switch (xuanze) {
case 0:
if (context.message_type == 'group') {
replyMsg(context, `[CQ:at,qq=${context.user_id}]\n` + bangzhuzhiling1);
} else if (context.message_type == 'private') {
replyMsg(context, bangzhuzhiling1);
for (let i = 0; i < mingling1.length; i++) {
replyMsg(context, mingling1[i]);
}
}
break;
case 1:
if (context.message_type == 'group') {
replyMsg(context, `[CQ:at,qq=${context.user_id}]\n` + bangzhuzhiling2);
for (let i = 0; i < mingling2.length; i++) {
replyMsg(context, mingling2[i]);
}
} else if (context.message_type == 'private') {
replyMsg(context, bangzhuzhiling2);
for (let i = 0; i < mingling2.length; i++) {
replyMsg(context, mingling2[i]);
}
}
break;
}
}
/*
ch / cn / zh / zhs -> chs (简体中文)
zht -> cht (繁体中文)
en -> eng(英语)
jp -> jpn(日语)
ko -> kor(韩语)
fr -> fre(法语)
ge -> ger(德语)
ru -> rus(俄语)
*/
/**
* 搜图
*
* @param {object} context
* @param {number} [customDB=-1]
* @returns
*/
var searchImgqq = new Array(); //针对搜图的延时保护,以QQ号为单位
async function searchImg(context, customDB = -1) {
const args = parseArgs(context.message);
const hasWord = word => context.message.indexOf(word) !== -1;
if (searchImgqq[context.user_id.toString()] == null) {
searchImgqq[context.user_id.toString()] = true;
let t = setTimeout(() => {
clearTimeout(t);
searchImgqq[context.user_id.toString()] = null;
}, 15 * 1000);
} else {
return;
}
//OCR
if (args.ocr) {
doOCR(context);
return;
}
//明日方舟
if (hasWord('akhr') || hasWord('公招')) {
doAkhr(context);
return;
}
//决定搜索库
let db = snDB[setting.saucenaoDefaultDB] || snDB.all;
if (customDB < 0) {
if (args.pixiv) db = snDB.pixiv;
else if (args.danbooru) db = snDB.danbooru;
else if (args.book) db = snDB.book;
else if (args.anime) db = snDB.anime;
else if (args.a2d) db = -10001;
else if (!context.group_id && !context.discuss_id) {
//私聊搜图模式
//return;
const sdb = logger.smStatus(0, context.user_id);
if (sdb) {
db = sdb;
logger.smSwitch(0, context.user_id, false);
}
}
} else db = customDB;
//console.log(context)
//得到图片链接并搜图
const msg = context.message;
const imgs = getImgs(msg);
//var pic_m = false;
var jishu = 0;
var tupianshu = imgs.length;
var tupianshu2 = imgs.length;
//ascii2d
let whitegroup = false;
let whitegroup2 = setting.a2dwhitegroup;
let whiteqq = false;
let whiteqq2 = setting.a2dwhiteqq;
let blackgroup2 = setting.blackgroup;
let i = 0;
if (context.message_type == "group") {
for (i = 0; i < whitegroup2.length; i++) {
if (context.group_id == whitegroup2[i]) {
whitegroup = true;
break;
}
}
for (i = 0; i < blackgroup2.length; i++) {
if (context.group_id == blackgroup2[i]) {
return;
}
}
} else if (context.message_type == "private") {
for (i = 0; i < whiteqq2.length; i++) {
if (context.user_id == whiteqq2[i]) {
whitegroup = true;
whiteqq = true;
break;
}
}
}
//console.log("本次搜索图片数:" + tupianshu);
var t = setInterval(async () => {
if (tupianshu > 0) {
let img = imgs[imgs.length - tupianshu];
//console.log(tupianshu);
tupianshu--;
//console.log(tupianshu);
if (args['url']) replyMsg(context, img.url.replace(/\/[0-9]+\//, '//').replace(/\?.*$/, ''), true, true);
else {
//获取缓存
let hasCache = false;
if (setting.cache.enable && !args.purge) {
const cache = await pfcache.getCache(img.file, db);
//如果有缓存
if (cache) {
hasCache = true;