koishi-plugin-best-cave 1.5.1 → 1.5.3
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/index.js +42 -40
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -346,14 +346,20 @@ var IdManager = class {
|
|
|
346
346
|
throw new Error("IdManager not initialized");
|
|
347
347
|
}
|
|
348
348
|
let nextId;
|
|
349
|
-
if (this.deletedIds.size
|
|
350
|
-
|
|
349
|
+
if (this.deletedIds.size > 0) {
|
|
350
|
+
const minDeletedId = Math.min(...Array.from(this.deletedIds));
|
|
351
|
+
if (!isNaN(minDeletedId) && minDeletedId > 0) {
|
|
352
|
+
nextId = minDeletedId;
|
|
353
|
+
this.deletedIds.delete(nextId);
|
|
354
|
+
} else {
|
|
355
|
+
nextId = this.maxId + 1;
|
|
356
|
+
}
|
|
351
357
|
} else {
|
|
352
|
-
nextId =
|
|
353
|
-
this.deletedIds.delete(nextId);
|
|
358
|
+
nextId = this.maxId + 1;
|
|
354
359
|
}
|
|
355
|
-
while (this.usedIds.has(nextId)) {
|
|
356
|
-
nextId =
|
|
360
|
+
while (isNaN(nextId) || nextId <= 0 || this.usedIds.has(nextId)) {
|
|
361
|
+
nextId = this.maxId + 1;
|
|
362
|
+
this.maxId++;
|
|
357
363
|
}
|
|
358
364
|
this.usedIds.add(nextId);
|
|
359
365
|
this.saveStatus().catch(
|
|
@@ -1342,13 +1348,16 @@ async function apply(ctx, config) {
|
|
|
1342
1348
|
async function processAdd(ctx2, config2, caveFilePath, resourceDir, pendingFilePath, session, content) {
|
|
1343
1349
|
let caveId;
|
|
1344
1350
|
try {
|
|
1351
|
+
caveId = await idManager.getNextId();
|
|
1352
|
+
if (isNaN(caveId) || caveId <= 0) {
|
|
1353
|
+
throw new Error("Invalid ID generated");
|
|
1354
|
+
}
|
|
1345
1355
|
const inputContent = content.length > 0 ? content.join("\n") : await (async () => {
|
|
1346
1356
|
await sendMessage(session, "commands.cave.add.noContent", [], true, 6e4);
|
|
1347
1357
|
const reply = await session.prompt({ timeout: 6e4 });
|
|
1348
|
-
if (!reply)
|
|
1358
|
+
if (!reply) sendMessage(session, "commands.cave.add.operationTimeout", [], true);
|
|
1349
1359
|
return reply;
|
|
1350
1360
|
})();
|
|
1351
|
-
caveId = await idManager.getNextId();
|
|
1352
1361
|
if (inputContent.includes("/app/.config/QQ/")) {
|
|
1353
1362
|
return sendMessage(session, "commands.cave.add.localFileNotAllowed", [], true);
|
|
1354
1363
|
}
|
|
@@ -1384,6 +1393,7 @@ async function apply(ctx, config) {
|
|
|
1384
1393
|
]);
|
|
1385
1394
|
const newCave = {
|
|
1386
1395
|
cave_id: caveId,
|
|
1396
|
+
// 确保使用有效的数字ID
|
|
1387
1397
|
elements: [
|
|
1388
1398
|
...textParts,
|
|
1389
1399
|
...imageElements.map((el, idx) => ({
|
|
@@ -1392,9 +1402,11 @@ async function apply(ctx, config) {
|
|
|
1392
1402
|
// 保持原始文本和图片的相对位置
|
|
1393
1403
|
index: el.index
|
|
1394
1404
|
}))
|
|
1395
|
-
].sort((a, b) => a.index -
|
|
1396
|
-
contributor_number: session.userId,
|
|
1397
|
-
|
|
1405
|
+
].sort((a, b) => a.index - b.index),
|
|
1406
|
+
contributor_number: session.userId || "100000",
|
|
1407
|
+
// 添加默认值
|
|
1408
|
+
contributor_name: session.username || "User"
|
|
1409
|
+
// 添加默认值
|
|
1398
1410
|
};
|
|
1399
1411
|
if (videoUrls.length > 0 && savedVideos.length > 0) {
|
|
1400
1412
|
newCave.elements.push({
|
|
@@ -1430,34 +1442,24 @@ async function apply(ctx, config) {
|
|
|
1430
1442
|
elements: cleanElementsForSave(newCave.elements, false)
|
|
1431
1443
|
});
|
|
1432
1444
|
if (config2.enableImageDuplicate || config2.enableTextDuplicate) {
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
throw new Error("duplicate_found");
|
|
1452
|
-
}
|
|
1453
|
-
} catch (error) {
|
|
1454
|
-
if (error.message !== "duplicate_found") {
|
|
1455
|
-
await idManager.markDeleted(caveId);
|
|
1456
|
-
}
|
|
1457
|
-
if (error.message === "duplicate_found") {
|
|
1458
|
-
return "";
|
|
1459
|
-
}
|
|
1460
|
-
return sendMessage(session, "commands.cave.error.addFailed", [], true);
|
|
1445
|
+
const duplicateResults = await contentHashManager.findDuplicates({
|
|
1446
|
+
images: config2.enableImageDuplicate ? imageBuffers : void 0,
|
|
1447
|
+
texts: config2.enableTextDuplicate ? textParts.filter((p) => p.type === "text").map((p) => p.content) : void 0
|
|
1448
|
+
}, {
|
|
1449
|
+
image: config2.imageDuplicateThreshold,
|
|
1450
|
+
text: config2.textDuplicateThreshold
|
|
1451
|
+
});
|
|
1452
|
+
for (const result of duplicateResults) {
|
|
1453
|
+
if (!result) continue;
|
|
1454
|
+
const originalCave = data.find((item) => item.cave_id === result.caveId);
|
|
1455
|
+
if (!originalCave) continue;
|
|
1456
|
+
await idManager.markDeleted(caveId);
|
|
1457
|
+
const duplicateMessage = session.text(
|
|
1458
|
+
"commands.cave.error.similarDuplicateFound",
|
|
1459
|
+
[(result.similarity * 100).toFixed(1)]
|
|
1460
|
+
);
|
|
1461
|
+
await session.send(duplicateMessage + await buildMessage(originalCave, resourceDir, session));
|
|
1462
|
+
throw new Error("duplicate_found");
|
|
1461
1463
|
}
|
|
1462
1464
|
}
|
|
1463
1465
|
await Promise.all([
|
|
@@ -1470,7 +1472,7 @@ async function apply(ctx, config) {
|
|
|
1470
1472
|
await idManager.addStat(session.userId, caveId);
|
|
1471
1473
|
return sendMessage(session, "commands.cave.add.addSuccess", [caveId], false);
|
|
1472
1474
|
} catch (error) {
|
|
1473
|
-
if (
|
|
1475
|
+
if (typeof caveId === "number" && !isNaN(caveId) && caveId > 0) {
|
|
1474
1476
|
await idManager.markDeleted(caveId);
|
|
1475
1477
|
}
|
|
1476
1478
|
if (error.message === "duplicate_found") {
|