-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
139 lines (109 loc) · 3.27 KB
/
utils.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
/**
* Utils
*/
const ModuleStatus = require('./db/Schemas/ModuleStatus');
const redis = require('./redis');
const config = require('./config.json');
module.exports = {
/**
* 设置模块状态
*/
async setModuleStatus(message, moduleName, status) {
if (message.sender.permission == 'MEMBER' && !verifyAccess(message.sender.id)) {
message.quoteReply("该命令仅限群主或管理员执行");
return;
}
result = await ModuleStatus.updateMany({
group: message.sender.group.id,
details: {
$elemMatch: {
module: moduleName,
}
}
}, {
$set: {
"details.$.status": status
}
}, {
new: true,
multi: true
})
// 如果不存在,则新建 群/规则
if (!result) {
result = await ModuleStatus.findOneAndUpdate({
group: message.sender.group.id
}, {
$addToSet: {
details: {
module: moduleName,
status: status
}
}
}, {
upsert: true,
new: true
})
}
message.quoteReply("操作完毕");
},
async getModuleStatus(message, moduleName) {
return await ModuleStatus.aggregate([
{
$match: {
group: message.sender.group.id
}
},
{
$unwind: "$details"
},
{
$match: {
"details.module": moduleName
}
}
]).then(r => {
if (r.length == 0) return 1
else return r[0].details.status;
});
},
async setModuleTempStatus (message, moduleName, status, expire) {
if (message.type != 'GroupMessage') return;
if (isNaN(expire)) expire = 180;
return await redis.setEx(`haoran:${message.sender.group.id}:${message.sender.id}:${moduleName}`, expire, status);
},
async getModuleTempStatus (message, moduleName) {
if (message.type != 'GroupMessage') return;
return await redis.get(`haoran:${message.sender.group.id}:${message.sender.id}:${moduleName}`);
},
async delModuleTempStatus (message, moduleName) {
if (message.type != 'GroupMessage') return;
return await redis.del(`haoran:${message.sender.group.id}:${message.sender.id}:${moduleName}`);
},
/**
* 确认是否有管理权限
* @param {Number} qq QQ
* @returns {Boolean} true 有,false 无
*/
verifyAccess(qq) {
switch (typeof (qq)) {
case 'number':
break;
case 'object':
qq = qq.sender.id;
break;
}
for (let i = 0; i < config.manage.admin.length; i++) {
const admin = config.manage.admin[i];
if (admin == qq) {
return true;
}
}
return false;
},
// 延迟
wait(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time);
})
}
}