-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
808 lines (803 loc) · 49.1 KB
/
index.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
/*
Copyright (C) <2017> <github.com/ssysm>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation,version 3 of the License
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//Import Module
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const mongojs = require('mongojs');
const querystring = require('querystring');
//Config Working Env
const db = mongojs('localhost:27017/ddbot');
const app = express();
//Config Database
const userCollection = db.collection('user');
const tagCollection = db.collection('tag');
//Config Express Setting
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({
extended: true
})); // for parsing application/x-www-form-urlencoded
//Config Software Setting
const gpid = require('./group')
const triggerWord = "我永远喜欢";
const botWord = "!ddbot";
//Config QQ API url
const api_base = "http://localhost:5000/openqq/";
const message_api = api_base + "send_group_message"
app.post('/bot', (req, res) => {
console.log(req.body);
//TODO: 这个res可能会把程序卡掉
//es6确定事件变量
var {
content,
sender,
sender_uid,
group,
group_uid
} = req.body;
//确认发送群在群白名单(group.js)
if (gpid.includes(group_uid)) {
//防止有人 “@我永远喜欢xxx”
if (!content.match("@我")) {
//防止自行反射命令
if (!content.match("ddbot消息")) {
if (req.body.sender_uid !== req.body.receiver_uid && req.body.from !== "api") {
console.log(req.body.sender_uid, req.body.receiver_id)
switch (true) {
//自动检测部分
case /我永远喜欢/.test(content):
//cotent filter
var content = content.match(triggerWord).input.replace(/[&\/\\#,+()$~%:*?<>{}]/g, '');
//f提取名字
var name = content.split("我永远喜欢")[1];
//确定不是空名字
//TODO:确定不是拿空格当名字
if (name !== "" && req.body.from !== "api") {
//确定对象常量
const data = new Object;
data.user = sender;
data.userId = sender_uid;
data.idol = name;
data.groupId = group_uid;
//查询是否被推
userCollection.find({
idol: data.idol,
userId: data.userId
}, (err, dep) => {
//直接throw错误
if (err) throw err;
else {
//如果已经被推
if (dep.length !== 0) {
console.log(dep)
//传出消息
var message = `ddbot消息: \n ${dep[0].user}(${dep[0].userId}),您已经推 ${dep[0].idol},不能再推了`
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
//存储新推&idol
userCollection.save(data, (err, docs) => {
//错误log到console
if (err) {
console.log(err);
} else {
//查询已推
userCollection.find({
userId: docs.userId
}, (err, user) => {
if (err) throw err;
else {
//发送信息
var message = `ddbot消息: \n ${docs.user}(${docs.userId})刚刚加推了${docs.idol}\n现在TA一共推${user.length}个偶像`;
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
})
}
})
}
}
})
}
break;
//手动触发命令
case /!ddbot/.test(content):
//filter
var content = content.match(botWord).input.replace(/[&\/\\#,+()$~%.:*?<>!{}]/g, '');
//截取 command 和 query
var arr = content.split(" "),
result = arr.splice(0, 2);
result.push(arr.join(" "));
var keyword = result;
console.log(keyword)
var command = keyword[1];
if (keyword[2]) var query = keyword[2];
//command开关
switch (command) {
case "about":
var message = "ddbot:\n Made By github.com/ssysm \n Check Out at : https://github.com/reQueateTeam/ddbot\n Under GPLv3.0"
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
break;
//用qq号查询推圈
case "lookup":
if (!query) {
//没有query的情况下
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您没有写出想要查询的qq号,范例:lookup ${sender_uid}`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
//将query变成int
var query = parseInt(query);
//查询数据库
userCollection.find({
userId: query
}, (err, docs) => {
//直接throw错误
if (err) throw err;
else {
//没有推
if (docs.length == 0) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},这个人->${query},啥也不推,╮(╯▽╰)╭)`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
tagCollection.find({
userId: docs.userId
}, (err, tag_data) => {
if (err) throw err;
//单推
else {
if (docs.length == 1) {
var message = `ddbot消息:\n @${sender_uid},${docs[0].user}(${docs[0].userId}),单推${docs[0].idol}\nTA拥有${tag_data.length}个标签`
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
//DD
var message = `ddbot消息:\n@${sender_uid},${docs[0].user}(${docs[0].userId}),是个dd,一共推${docs.length}个偶像,\nTA推的第一个偶像是${docs[0].idol}\n最近推的偶像是${docs[docs.length - 1].idol}\nTA拥有${tag_data.length}个标签`;
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
}
})
}
}
})
}
break;
//帮助
case "help":
var message =
`
ddbot 帮助:\n
自动检测dd: 我永远(喜欢)...
手动命令(前缀带!ddbot):\n
about -> 关于机器人
lookup qq_id -> 用qq号(qq_id)来查询TA的圈
list qq_id -> 用qq号(qq_id)来查询TA推的idol
idol xxx -> 查询在群里被推的idol
unlink xxx -> 解推一个idol\n
标签命令(前缀带!tag):\n
[tag名字] -> 直接返回在数据库里的tag内容
create [tag名字] [tag内容] -> 创建新内容
delete [tag名字] -> 删除tag
lookup [tag名字] -> 查询在群里的tag
search [tag名字(模糊)] -> 模糊搜索群里的tag`
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
break;
//查询idol
case "idol":
//如果query不存在
if (!query) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您没有写出想要查询的idol名字,范例:idol xxx`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
//query存在,查询
console.log(query)
//调用数据库
userCollection.find({
idol: query
}, (err, docs) => {
//直接throw错误
if (err) {
throw err
} else {
//没有查询到
if (docs.length == 0) {
var message = `ddbot消息:\n @${sender_uid},您查询的idol 「${query}」不存在`;
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
//查询到被多人推
if (docs.length !== 1)
var message =
`
ddbot消息:\n
您查询的idol 「${docs[0].idol}」
被${docs.length}个人推过,
第一个推TA的是->${docs[0].user}(${docs[0].userId}),
最近一个推TA的是->${docs[docs.length - 1].user}(${docs[docs.length - 1].userId})
`;
//查询到被一人推
//代码by ct
else if (docs.length === 1)
var message =
`
ddbot消息:\n
您查询的idol 「${docs[0].idol}」
被${docs.length}个人推过,
第一个和最近一个推TA的都是->${docs[0].user}(${docs[0].userId})
迫真单推?
`;
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
}
})
}
break;
//解推idol
case "unlink":
//query不存在
if (!query) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您没有写出想要解推的idol名字,范例:unlink xxx`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
//query存在,调用数据库
userCollection.findOne({
userId: sender_uid,
idol: query
}, (err, docs) => {
//直接throw错误
if (err) throw err;
else {
//如果没有找到解推的idol
if (!docs) {
var message = `ddbot消息:\n@${sender},您输入的信息有误,请重新输入`
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
//找到解推的idol,执行删除操作
userCollection.remove({
_id: mongojs.ObjectId(docs._id)
}, (err, remove) => {
//直接throw错误
if (err) throw err;
else {
//发送信息
var message = `ddbot消息:\n@${sender_uid},您已经不再推${docs.idol}了`
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
})
}
}
})
}
break;
//list操作
case "list":
//query不存在
if (!query) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您没有写出想要被查看qqid,范例:list ${sender_uid}`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
//将query变成int
var query = parseInt(query);
//执行数据库查询操作
userCollection.find({
userId: query
}, (err, docs) => {
//直接throw错误
if (err) {
throw err
} else {
if (docs.length !== 0) {
//自定义阵列
var minArray = new Array;
//for出最小队列
for (var i = 0; i < docs.length; i++) {
var minify = new Object;
minify = docs[i].idol
minArray.push(minify);
}
//发送信息
/*
注意! 在这里有些人可能d的太多,腾讯会返回1202
这里可能需要修一下
(dd太可怕了
*/
var message = `ddbot消息:\n,${docs[0].user}(${docs[0].userId})的偶像推列表:\n ${JSON.stringify(minArray)}`
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},Array Not Found or User Not Found`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
}
})
}
break;
}
break;
case /!tag/.test(content):
//filter
var content = content.replace(/[\/\\<>{}]/g, '');
//截取 command 和 query
var arr = content.split(" "),
result = arr.splice(0, 3);
result.push(arr.join(" "));
var keyword = result;
console.log(keyword)
var command = keyword[1];
if (keyword[2]) var query = keyword[2];
if (keyword[3]) var tag_content = keyword[3];
switch (command) {
case "create":
//query不存在
if (!query) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您没有写出想要创建tag内容,范例:!tag create [tag名字] [tag内容]`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
if (!tag_content) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您没有写出想要创建tag内容,范例:!tag create [tag名字] [tag内容]`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
tagCollection.findOne({
tag: query,
groupId: group_uid
}, (err, docs) => {
if (err) throw err;
else {
if (!docs) {
var tag = {
userId: sender_uid,
groupId: group_uid,
tag: query,
content: tag_content,
create_date: new Date,
used: 0,
}
tagCollection.save(tag, (err, docs) => {
if (err) throw err;
else {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},tag创建成功,「${docs.tag}」`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
})
} else {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},tag「${docs.tag}」已经存在`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
}
})
}
}
break;
case "delete":
//query不存在
if (!query) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您没有写出想要delete的tag,范例:!tag delete [tag name]`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
tagCollection.findOne({
tag: query,
groupId: group_uid,
userId: sender_uid
}, (err, docs) => {
if (err) throw err;
else {
if (!docs) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您删除的tag不存在`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
tagCollection.remove({
_id: mongojs.ObjectId(docs._id)
}, (err, docs) => {
if (err) throw err;
else {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},tag(${docs.tag})删除成功`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
})
}
}
})
}
break;
case "lookup":
if (!query) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您没有写出想要查看的tag,范例:!tag lookup [tag name]`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
tagCollection.findOne({
tag: query.toString(),
groupId: group_uid
}, (err, docs) => {
if (err) throw err;
else {
if (!docs) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},数据库返回空数据`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
var message =
`ddbot消息:
@${sender_uid},您查询的tag「${docs.tag}」(创建者id:${docs.userId})的内容是:\n
${docs.content}\n
在${docs.create_date}创建了这个tag
一共被使用了${docs.used}次
`;
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: message
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
}
});
}
break;
case "search":
if (!query) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},您没有写出想要搜索的tag,范例:!tag search [tag name]`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
tagCollection.find({
tag: {
$regex: query.toString(),
$options: 'i'
},
groupId: group_uid
}, (err, docs) => {
var minArray = new Array;
//for出最小队列
for (var i = 0; i < docs.length; i++) {
var minify = new Object;
minify = docs[i].tag
minArray.push(minify);
}
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},关于「${query}」的tag是:\n${minArray}`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
})
}
break;
default:
if (!command) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},请输入tag`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
tagCollection.findOne({
tag: command,
groupId: group_uid
}, (err, docs) => {
if (err) throw err;
else {
if (!docs) {
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `ddbot消息:\n @${sender_uid},tag「${command}」不存在`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
} else {
tagCollection.update({
_id: mongojs.ObjectId(docs._id)
}, {
$inc: {
used: 1
}
}, () => { });
axios.post(message_api, querystring.stringify({
uid: group_uid,
content: `${docs.content}`
}))
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err);
throw err;
})
}
}
})
}
break;
}
break;
//默认
default:
break;
}
}
}
}
}
res.sendStatus(200);
})
app.get('/bot', (req, res) => {
res.send('ok');
})
//服务器监听端口
app.listen(3000);