-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (99 loc) · 3.18 KB
/
app.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
'use strict'
let Express = require('express');
let fs = require('fs');
let utils = require('./utils');
let config = require('./config');
let routes = require('./route');
let mongo = require('./mongo');
let handler = require('./handler');
let notification = require('./notification');
let app = new Express();
app.use(Express.json())
app.use('/', async (req, res, next) => {
await mongo.prepare();
next();
});
app.use('/', async (req, res, next) => {
// 消息处理
if (req.body.post_type === 'message') {
// bot 不接受私聊
if (req.body.message_type === 'private') return res.send({});
req.body.sender.group_id = req.body.group_id;
let routeNameList = Object.keys(routes.codeMap).sort((a, b) => b.length - a.length);
for (let routeName of routeNameList) {
let handler = routes.codeMap[routeName];
// 不分大小写
routeName = routeName.toLocaleLowerCase();
// 判断是命令还是吹水
if (req.body.message.toLocaleLowerCase().startsWith(routeName)) {
console.log(`${req.body.sender.nickname}: ${req.body.message}`);
let message = req.body.message.split('\n');
message[0] = message[0].toLocaleLowerCase().match(new RegExp(`^${routeName}(.*)`))[1];
message = message.join('\n');
let sender = req.body.sender;
let output;
try {
output = await handler(message, sender);
if (output) return res.send({ at_sender: true, reply: output });
else return res.send({});
} catch (err) {
console.error(err);
// return res.send({ at_sender: true, reply: `执行错误. 联系管理员查看日志.` });
}
}
};
}
// 请求处理
if (req.body.post_type === 'request') {
if (req.body.request_type === 'group' && req.body.sub_type === 'invite') {
return await handler.acceptGroupInvite(req, res);
}
if (req.body.request_type === 'friend') {
return await handler.acceptFriendInvite(req, res);
}
}
if (req.body.post_type === 'notice') {
if (req.body.notice_type === 'group_admin') {
await utils.setGroupAdmin(req.body.group_id, req.body.user_id, req.body.sub_type === 'set');
}
if (req.body.notice_type === 'group_increase') {
await utils.findOrCreateGroupUser(req.body.group_id, req.body.user_id);
}
}
return res.send({ status: 'ok' });
})
async function init() {
await mongo.prepare();
let version = require('./package.json').version;
fs.writeFileSync('./current_version', version);
process.env.version = version
await utils.syncGroupAdmin();
await notification.sendNews();
}
init();
setInterval(async () => {
await mongo.prepare();
await notification.sendNews();
}, 1000 * 60 * 5)
app.listen(config.listenPort, function () {
console.log(`Listening on port ${config.listenPort} now!`);
});
// {
// font: 1611312,
// message: 'hello',
// message_id: 123,
// message_type: 'private',
// post_type: 'message',
// raw_message: 'hello',
// self_id: 123123123,
// sender:
// {
// age: 88,
// nickname: '123123',
// sex: 'male',
// user_id: 123123123,
// },
// sub_type: 'friend',
// time: 123123123,
// user_id: 123123123
// }