koishi-plugin-bind-bot 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/export-utils.js +13 -6
- package/lib/force-bind-utils.js +12 -8
- package/lib/handlers/binding.handler.js +63 -27
- package/lib/handlers/buid.handler.js +102 -45
- package/lib/handlers/lottery.handler.js +11 -9
- package/lib/handlers/mcid.handler.js +197 -86
- package/lib/handlers/tag.handler.js +86 -31
- package/lib/handlers/whitelist.handler.js +252 -77
- package/lib/index.js +260 -142
- package/lib/repositories/mcidbind.repository.js +2 -2
- package/lib/repositories/schedule-mute.repository.js +2 -2
- package/lib/services/api.service.js +27 -23
- package/lib/services/database.service.js +16 -14
- package/lib/services/nickname.service.js +10 -10
- package/lib/types/api.d.ts +90 -0
- package/lib/types/config.d.ts +61 -0
- package/lib/types/database.d.ts +50 -0
- package/lib/types/update-data.d.ts +83 -0
- package/lib/utils/error-utils.js +7 -8
- package/lib/utils/helpers.js +45 -7
- package/lib/utils/message-utils.js +36 -23
- package/lib/utils/session-manager.js +6 -1
- package/package.json +12 -2
|
@@ -7,17 +7,23 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
7
7
|
register() {
|
|
8
8
|
const cmd = this.ctx.command('mcid', 'Minecraft账号和B站账号绑定管理');
|
|
9
9
|
const tagCmd = cmd.subcommand('.tag', '[管理员]用户标签管理');
|
|
10
|
-
tagCmd
|
|
10
|
+
tagCmd
|
|
11
|
+
.subcommand('.add <tagName:string> [...targets:string]', '为用户添加标签')
|
|
11
12
|
.action(async ({ session }, tagName, ...targets) => this.handleTagAdd(session, tagName, ...targets));
|
|
12
|
-
tagCmd
|
|
13
|
+
tagCmd
|
|
14
|
+
.subcommand('.remove <tagName:string> [...targets:string]', '移除用户标签')
|
|
13
15
|
.action(async ({ session }, tagName, ...targets) => this.handleTagRemove(session, tagName, ...targets));
|
|
14
|
-
tagCmd
|
|
16
|
+
tagCmd
|
|
17
|
+
.subcommand('.list [target:string]', '查看用户的所有标签')
|
|
15
18
|
.action(async ({ session }, target) => this.handleTagList(session, target));
|
|
16
|
-
tagCmd
|
|
19
|
+
tagCmd
|
|
20
|
+
.subcommand('.find <tagName:string>', '查找有特定标签的所有用户')
|
|
17
21
|
.action(async ({ session }, tagName) => this.handleTagFind(session, tagName));
|
|
18
|
-
tagCmd
|
|
22
|
+
tagCmd
|
|
23
|
+
.subcommand('.rename <oldTagName:string> <newTagName:string>', '[管理员]重命名标签')
|
|
19
24
|
.action(async ({ session }, oldTagName, newTagName) => this.handleTagRename(session, oldTagName, newTagName));
|
|
20
|
-
tagCmd
|
|
25
|
+
tagCmd
|
|
26
|
+
.subcommand('.deleteall <tagName:string>', '[主人]删除所有用户的某个标签')
|
|
21
27
|
.action(async ({ session }, tagName) => this.handleTagDeleteAll(session, tagName));
|
|
22
28
|
}
|
|
23
29
|
async isAdmin(userId) {
|
|
@@ -48,7 +54,7 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
48
54
|
async handleTagAdd(session, tagName, ...targets) {
|
|
49
55
|
try {
|
|
50
56
|
const normalizedUserId = this.deps.normalizeQQId(session.userId);
|
|
51
|
-
if (!await this.isAdmin(session.userId)) {
|
|
57
|
+
if (!(await this.isAdmin(session.userId))) {
|
|
52
58
|
this.logger.warn('标签', `权限不足: QQ(${normalizedUserId})不是管理员`);
|
|
53
59
|
return this.deps.sendMessage(session, [koishi_1.h.text('只有管理员才能管理用户标签')]);
|
|
54
60
|
}
|
|
@@ -58,7 +64,9 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
58
64
|
}
|
|
59
65
|
if (!this.validateTagName(tagName)) {
|
|
60
66
|
this.logger.warn('标签', `QQ(${normalizedUserId})提供的标签名称"${tagName}"格式无效`);
|
|
61
|
-
return this.deps.sendMessage(session, [
|
|
67
|
+
return this.deps.sendMessage(session, [
|
|
68
|
+
koishi_1.h.text('标签名称只能包含中文、字母、数字、下划线和连字符')
|
|
69
|
+
]);
|
|
62
70
|
}
|
|
63
71
|
if (!targets || targets.length === 0) {
|
|
64
72
|
this.logger.warn('标签', `QQ(${normalizedUserId})未指定目标用户`);
|
|
@@ -71,20 +79,34 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
71
79
|
let targetBind = await this.deps.databaseService.getMcBindByQQId(normalizedTargetId);
|
|
72
80
|
if (!targetBind) {
|
|
73
81
|
const tempUsername = `_temp_${normalizedTargetId}`;
|
|
74
|
-
await this.repos.mcidbind.create({
|
|
82
|
+
await this.repos.mcidbind.create({
|
|
83
|
+
qqId: normalizedTargetId,
|
|
84
|
+
mcUsername: tempUsername,
|
|
85
|
+
mcUuid: '',
|
|
86
|
+
lastModified: new Date(),
|
|
87
|
+
isAdmin: false,
|
|
88
|
+
whitelist: [],
|
|
89
|
+
tags: []
|
|
90
|
+
});
|
|
75
91
|
targetBind = await this.deps.databaseService.getMcBindByQQId(normalizedTargetId);
|
|
76
92
|
}
|
|
77
93
|
if (targetBind.tags && targetBind.tags.includes(tagName)) {
|
|
78
94
|
this.logger.warn('标签', `QQ(${normalizedTargetId})已有标签"${tagName}"`);
|
|
79
|
-
return this.deps.sendMessage(session, [
|
|
95
|
+
return this.deps.sendMessage(session, [
|
|
96
|
+
koishi_1.h.text(`用户 ${normalizedTargetId} 已有标签"${tagName}"`)
|
|
97
|
+
]);
|
|
80
98
|
}
|
|
81
99
|
const newTags = [...(targetBind.tags || []), tagName];
|
|
82
100
|
await this.repos.mcidbind.update(normalizedTargetId, { tags: newTags });
|
|
83
101
|
this.logger.info('标签', `成功: 管理员QQ(${normalizedUserId})为QQ(${normalizedTargetId})添加了标签"${tagName}"`, true);
|
|
84
|
-
return this.deps.sendMessage(session, [
|
|
102
|
+
return this.deps.sendMessage(session, [
|
|
103
|
+
koishi_1.h.text(`已成功为用户 ${normalizedTargetId} 添加标签"${tagName}"`)
|
|
104
|
+
]);
|
|
85
105
|
}
|
|
86
106
|
this.logger.info('标签', `管理员QQ(${normalizedUserId})尝试批量为${targets.length}个用户添加标签"${tagName}"`, true);
|
|
87
|
-
await this.deps.sendMessage(session, [
|
|
107
|
+
await this.deps.sendMessage(session, [
|
|
108
|
+
koishi_1.h.text(`开始为${targets.length}个用户添加标签"${tagName}",请稍候...`)
|
|
109
|
+
]);
|
|
88
110
|
let successCount = 0, failCount = 0, skipCount = 0;
|
|
89
111
|
const results = [];
|
|
90
112
|
for (let i = 0; i < targets.length; i++) {
|
|
@@ -94,7 +116,15 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
94
116
|
let targetBind = await this.deps.databaseService.getMcBindByQQId(normalizedTargetId);
|
|
95
117
|
if (!targetBind) {
|
|
96
118
|
const tempUsername = `_temp_${normalizedTargetId}`;
|
|
97
|
-
await this.repos.mcidbind.create({
|
|
119
|
+
await this.repos.mcidbind.create({
|
|
120
|
+
qqId: normalizedTargetId,
|
|
121
|
+
mcUsername: tempUsername,
|
|
122
|
+
mcUuid: '',
|
|
123
|
+
lastModified: new Date(),
|
|
124
|
+
isAdmin: false,
|
|
125
|
+
whitelist: [],
|
|
126
|
+
tags: []
|
|
127
|
+
});
|
|
98
128
|
targetBind = await this.deps.databaseService.getMcBindByQQId(normalizedTargetId);
|
|
99
129
|
}
|
|
100
130
|
if (targetBind.tags && targetBind.tags.includes(tagName)) {
|
|
@@ -132,7 +162,7 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
132
162
|
async handleTagRemove(session, tagName, ...targets) {
|
|
133
163
|
try {
|
|
134
164
|
const normalizedUserId = this.deps.normalizeQQId(session.userId);
|
|
135
|
-
if (!await this.isAdmin(session.userId)) {
|
|
165
|
+
if (!(await this.isAdmin(session.userId))) {
|
|
136
166
|
this.logger.warn('标签', `权限不足: QQ(${normalizedUserId})不是管理员`);
|
|
137
167
|
return this.deps.sendMessage(session, [koishi_1.h.text('只有管理员才能管理用户标签')]);
|
|
138
168
|
}
|
|
@@ -155,15 +185,21 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
155
185
|
}
|
|
156
186
|
if (!targetBind.tags || !targetBind.tags.includes(tagName)) {
|
|
157
187
|
this.logger.warn('标签', `QQ(${normalizedTargetId})没有标签"${tagName}"`);
|
|
158
|
-
return this.deps.sendMessage(session, [
|
|
188
|
+
return this.deps.sendMessage(session, [
|
|
189
|
+
koishi_1.h.text(`用户 ${normalizedTargetId} 没有标签"${tagName}"`)
|
|
190
|
+
]);
|
|
159
191
|
}
|
|
160
192
|
const newTags = targetBind.tags.filter(tag => tag !== tagName);
|
|
161
193
|
await this.repos.mcidbind.update(normalizedTargetId, { tags: newTags });
|
|
162
194
|
this.logger.info('标签', `成功: 管理员QQ(${normalizedUserId})为QQ(${normalizedTargetId})移除了标签"${tagName}"`, true);
|
|
163
|
-
return this.deps.sendMessage(session, [
|
|
195
|
+
return this.deps.sendMessage(session, [
|
|
196
|
+
koishi_1.h.text(`已成功为用户 ${normalizedTargetId} 移除标签"${tagName}"`)
|
|
197
|
+
]);
|
|
164
198
|
}
|
|
165
199
|
this.logger.info('标签', `管理员QQ(${normalizedUserId})尝试批量为${targets.length}个用户移除标签"${tagName}"`, true);
|
|
166
|
-
await this.deps.sendMessage(session, [
|
|
200
|
+
await this.deps.sendMessage(session, [
|
|
201
|
+
koishi_1.h.text(`开始为${targets.length}个用户移除标签"${tagName}",请稍候...`)
|
|
202
|
+
]);
|
|
167
203
|
let successCount = 0, failCount = 0, skipCount = 0;
|
|
168
204
|
const results = [];
|
|
169
205
|
for (let i = 0; i < targets.length; i++) {
|
|
@@ -212,7 +248,7 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
212
248
|
async handleTagList(session, target) {
|
|
213
249
|
try {
|
|
214
250
|
const normalizedUserId = this.deps.normalizeQQId(session.userId);
|
|
215
|
-
if (!await this.isAdmin(session.userId)) {
|
|
251
|
+
if (!(await this.isAdmin(session.userId))) {
|
|
216
252
|
this.logger.warn('标签', `权限不足: QQ(${normalizedUserId})不是管理员`);
|
|
217
253
|
return this.deps.sendMessage(session, [koishi_1.h.text('只有管理员才能查看用户标签')]);
|
|
218
254
|
}
|
|
@@ -229,7 +265,9 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
229
265
|
return this.deps.sendMessage(session, [koishi_1.h.text(`用户 ${normalizedTargetId} 没有任何标签`)]);
|
|
230
266
|
}
|
|
231
267
|
const tagList = targetBind.tags.map(tag => `• ${tag}`).join('\n');
|
|
232
|
-
return this.deps.sendMessage(session, [
|
|
268
|
+
return this.deps.sendMessage(session, [
|
|
269
|
+
koishi_1.h.text(`用户 ${normalizedTargetId} 的标签:\n${tagList}`)
|
|
270
|
+
]);
|
|
233
271
|
}
|
|
234
272
|
this.logger.info('标签', `管理员QQ(${normalizedUserId})查看所有标签统计`, true);
|
|
235
273
|
const allBinds = await this.repos.mcidbind.findAll();
|
|
@@ -244,7 +282,10 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
244
282
|
if (Object.keys(tagStats).length === 0) {
|
|
245
283
|
return this.deps.sendMessage(session, [koishi_1.h.text('当前没有任何用户标签')]);
|
|
246
284
|
}
|
|
247
|
-
const sortedTags = Object.entries(tagStats)
|
|
285
|
+
const sortedTags = Object.entries(tagStats)
|
|
286
|
+
.sort((a, b) => b[1] - a[1])
|
|
287
|
+
.map(([tag, count]) => `• ${tag} (${count}人)`)
|
|
288
|
+
.join('\n');
|
|
248
289
|
return this.deps.sendMessage(session, [koishi_1.h.text(`所有标签统计:\n${sortedTags}`)]);
|
|
249
290
|
}
|
|
250
291
|
catch (error) {
|
|
@@ -256,7 +297,7 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
256
297
|
async handleTagFind(session, tagName) {
|
|
257
298
|
try {
|
|
258
299
|
const normalizedUserId = this.deps.normalizeQQId(session.userId);
|
|
259
|
-
if (!await this.isAdmin(session.userId)) {
|
|
300
|
+
if (!(await this.isAdmin(session.userId))) {
|
|
260
301
|
this.logger.warn('标签', `权限不足: QQ(${normalizedUserId})不是管理员`);
|
|
261
302
|
return this.deps.sendMessage(session, [koishi_1.h.text('只有管理员才能查找标签')]);
|
|
262
303
|
}
|
|
@@ -271,12 +312,18 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
271
312
|
this.logger.info('标签', `没有用户有标签"${tagName}"`);
|
|
272
313
|
return this.deps.sendMessage(session, [koishi_1.h.text(`没有用户有标签"${tagName}"`)]);
|
|
273
314
|
}
|
|
274
|
-
const userList = usersWithTag
|
|
275
|
-
|
|
315
|
+
const userList = usersWithTag
|
|
316
|
+
.map(bind => {
|
|
317
|
+
const mcInfo = bind.mcUsername && !bind.mcUsername.startsWith('_temp_')
|
|
318
|
+
? ` (MC: ${bind.mcUsername})`
|
|
319
|
+
: '';
|
|
276
320
|
return `• ${bind.qqId}${mcInfo}`;
|
|
277
|
-
})
|
|
321
|
+
})
|
|
322
|
+
.join('\n');
|
|
278
323
|
this.logger.info('标签', `找到${usersWithTag.length}个用户有标签"${tagName}"`, true);
|
|
279
|
-
return this.deps.sendMessage(session, [
|
|
324
|
+
return this.deps.sendMessage(session, [
|
|
325
|
+
koishi_1.h.text(`有标签"${tagName}"的用户 (共${usersWithTag.length}人):\n${userList}`)
|
|
326
|
+
]);
|
|
280
327
|
}
|
|
281
328
|
catch (error) {
|
|
282
329
|
const normalizedUserId = this.deps.normalizeQQId(session.userId);
|
|
@@ -287,7 +334,7 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
287
334
|
async handleTagRename(session, oldTagName, newTagName) {
|
|
288
335
|
try {
|
|
289
336
|
const normalizedUserId = this.deps.normalizeQQId(session.userId);
|
|
290
|
-
if (!await this.isAdmin(session.userId)) {
|
|
337
|
+
if (!(await this.isAdmin(session.userId))) {
|
|
291
338
|
this.logger.warn('标签', `权限不足: QQ(${normalizedUserId})不是管理员`);
|
|
292
339
|
return this.deps.sendMessage(session, [koishi_1.h.text('只有管理员才能重命名标签')]);
|
|
293
340
|
}
|
|
@@ -297,7 +344,9 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
297
344
|
}
|
|
298
345
|
if (!this.validateTagName(newTagName)) {
|
|
299
346
|
this.logger.warn('标签', `QQ(${normalizedUserId})提供的新标签名称"${newTagName}"格式无效`);
|
|
300
|
-
return this.deps.sendMessage(session, [
|
|
347
|
+
return this.deps.sendMessage(session, [
|
|
348
|
+
koishi_1.h.text('新标签名称只能包含中文、字母、数字、下划线和连字符')
|
|
349
|
+
]);
|
|
301
350
|
}
|
|
302
351
|
const allBinds = await this.repos.mcidbind.findAll();
|
|
303
352
|
const usersWithOldTag = allBinds.filter(bind => bind.tags && bind.tags.includes(oldTagName));
|
|
@@ -308,14 +357,18 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
308
357
|
const usersWithNewTag = allBinds.filter(bind => bind.tags && bind.tags.includes(newTagName));
|
|
309
358
|
if (usersWithNewTag.length > 0) {
|
|
310
359
|
this.logger.warn('标签', `新标签"${newTagName}"已存在,无法重命名`);
|
|
311
|
-
return this.deps.sendMessage(session, [
|
|
360
|
+
return this.deps.sendMessage(session, [
|
|
361
|
+
koishi_1.h.text(`新标签"${newTagName}"已存在,请选择其他名称`)
|
|
362
|
+
]);
|
|
312
363
|
}
|
|
313
364
|
this.logger.info('标签', `管理员QQ(${normalizedUserId})开始将标签"${oldTagName}"重命名为"${newTagName}"`, true);
|
|
314
|
-
await this.deps.sendMessage(session, [
|
|
365
|
+
await this.deps.sendMessage(session, [
|
|
366
|
+
koishi_1.h.text(`找到${usersWithOldTag.length}个用户有标签"${oldTagName}",开始重命名为"${newTagName}"...`)
|
|
367
|
+
]);
|
|
315
368
|
let successCount = 0, failCount = 0;
|
|
316
369
|
for (const bind of usersWithOldTag) {
|
|
317
370
|
try {
|
|
318
|
-
const newTags = bind.tags.map(tag => tag === oldTagName ? newTagName : tag);
|
|
371
|
+
const newTags = bind.tags.map(tag => (tag === oldTagName ? newTagName : tag));
|
|
319
372
|
await this.repos.mcidbind.update(bind.qqId, { tags: newTags });
|
|
320
373
|
successCount++;
|
|
321
374
|
this.logger.debug('标签', `成功为用户QQ(${bind.qqId})将标签"${oldTagName}"重命名为"${newTagName}"`);
|
|
@@ -354,7 +407,9 @@ class TagHandler extends base_handler_1.BaseHandler {
|
|
|
354
407
|
return this.deps.sendMessage(session, [koishi_1.h.text(`没有用户有标签"${tagName}",无需删除`)]);
|
|
355
408
|
}
|
|
356
409
|
this.logger.info('标签', `找到${usersWithTag.length}个用户有标签"${tagName}",开始批量删除`, true);
|
|
357
|
-
await this.deps.sendMessage(session, [
|
|
410
|
+
await this.deps.sendMessage(session, [
|
|
411
|
+
koishi_1.h.text(`找到${usersWithTag.length}个用户有标签"${tagName}",开始批量删除...`)
|
|
412
|
+
]);
|
|
358
413
|
let successCount = 0, failCount = 0;
|
|
359
414
|
for (const bind of usersWithTag) {
|
|
360
415
|
try {
|