koishi-plugin-best-cave 2.4.4 → 2.4.6
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 +56 -24
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -344,7 +344,8 @@ async function buildCaveMessage(cave, config, fileManager, logger2, platform, pr
|
|
|
344
344
|
const followUpMessages = [];
|
|
345
345
|
for (const el of caveHElements) {
|
|
346
346
|
if (problematicTypes.includes(el.type) || el.type === "message" && el.attrs.forward) {
|
|
347
|
-
|
|
347
|
+
const placeholderKey = el.type === "message" && el.attrs.forward ? "forward" : el.type;
|
|
348
|
+
initialMessageContent.push(import_koishi.h.text(placeholderMap[placeholderKey]));
|
|
348
349
|
followUpMessages.push([el]);
|
|
349
350
|
} else {
|
|
350
351
|
initialMessageContent.push(el);
|
|
@@ -402,10 +403,46 @@ __name(getNextCaveId, "getNextCaveId");
|
|
|
402
403
|
async function processMessageElements(sourceElements, newId, session, config, logger2) {
|
|
403
404
|
const mediaToSave = [];
|
|
404
405
|
let mediaIndex = 0;
|
|
406
|
+
const typeMap = { "img": "image", "image": "image", "video": "video", "audio": "audio", "file": "file", "text": "text", "at": "at", "forward": "forward", "reply": "reply" };
|
|
407
|
+
const defaultExtMap = { "image": ".jpg", "video": ".mp4", "audio": ".mp3", "file": ".dat" };
|
|
405
408
|
async function transform(elements) {
|
|
406
409
|
const result = [];
|
|
407
|
-
|
|
408
|
-
|
|
410
|
+
async function processForwardContent(segments) {
|
|
411
|
+
const innerResult = [];
|
|
412
|
+
for (const segment of segments) {
|
|
413
|
+
const sType = typeMap[segment.type];
|
|
414
|
+
if (!sType) continue;
|
|
415
|
+
if (sType === "text" && segment.data?.text?.trim()) {
|
|
416
|
+
innerResult.push({ type: "text", content: segment.data.text.trim() });
|
|
417
|
+
} else if (sType === "at" && (segment.data?.id || segment.data?.qq)) {
|
|
418
|
+
innerResult.push({ type: "at", content: segment.data.id || segment.data.qq });
|
|
419
|
+
} else if (sType === "reply" && segment.data?.id) {
|
|
420
|
+
innerResult.push({ type: "reply", content: segment.data.id });
|
|
421
|
+
} else if (["image", "video", "audio", "file"].includes(sType) && (segment.data?.src || segment.data?.url)) {
|
|
422
|
+
let fileIdentifier = segment.data.src || segment.data.url;
|
|
423
|
+
if (fileIdentifier.startsWith("http")) {
|
|
424
|
+
const ext = path2.extname(segment.data.file || "") || defaultExtMap[sType];
|
|
425
|
+
const currentMediaIndex = ++mediaIndex;
|
|
426
|
+
const fileName = `${newId}_${currentMediaIndex}_${session.channelId || session.guildId}_${session.userId}${ext}`;
|
|
427
|
+
mediaToSave.push({ sourceUrl: fileIdentifier, fileName });
|
|
428
|
+
fileIdentifier = fileName;
|
|
429
|
+
}
|
|
430
|
+
innerResult.push({ type: sType, file: fileIdentifier });
|
|
431
|
+
} else if (sType === "forward" && Array.isArray(segment.data?.content)) {
|
|
432
|
+
const nestedForwardNodes = [];
|
|
433
|
+
for (const nestedNode of segment.data.content) {
|
|
434
|
+
if (!nestedNode.message || !Array.isArray(nestedNode.message)) continue;
|
|
435
|
+
const nestedContentElements = await processForwardContent(nestedNode.message);
|
|
436
|
+
if (nestedContentElements.length > 0) {
|
|
437
|
+
nestedForwardNodes.push({ userId: nestedNode.sender?.user_id, userName: nestedNode.sender?.nickname, elements: nestedContentElements });
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (nestedForwardNodes.length > 0) innerResult.push({ type: "forward", content: nestedForwardNodes });
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return innerResult;
|
|
444
|
+
}
|
|
445
|
+
__name(processForwardContent, "processForwardContent");
|
|
409
446
|
for (const el of elements) {
|
|
410
447
|
const type = typeMap[el.type];
|
|
411
448
|
if (!type) {
|
|
@@ -422,27 +459,10 @@ async function processMessageElements(sourceElements, newId, session, config, lo
|
|
|
422
459
|
const forwardNodes = [];
|
|
423
460
|
for (const node of el.attrs.content) {
|
|
424
461
|
if (!node.message || !Array.isArray(node.message)) continue;
|
|
425
|
-
const
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
const attrs = { ...data };
|
|
430
|
-
if (type2 === "text" && typeof data.text !== "undefined") {
|
|
431
|
-
attrs.content = data.text;
|
|
432
|
-
delete attrs.text;
|
|
433
|
-
}
|
|
434
|
-
if (type2 === "at" && typeof data.qq !== "undefined") {
|
|
435
|
-
attrs.id = data.qq;
|
|
436
|
-
delete attrs.qq;
|
|
437
|
-
}
|
|
438
|
-
if (["image", "video", "audio"].includes(type2) && typeof data.url !== "undefined") {
|
|
439
|
-
attrs.src = data.url;
|
|
440
|
-
delete attrs.url;
|
|
441
|
-
}
|
|
442
|
-
return (0, import_koishi.h)(type2, attrs);
|
|
443
|
-
});
|
|
444
|
-
const contentElements = await transform(elementsToProcess);
|
|
445
|
-
if (contentElements.length > 0) forwardNodes.push({ userId, userName, elements: contentElements });
|
|
462
|
+
const contentElements = await processForwardContent(node.message);
|
|
463
|
+
if (contentElements.length > 0) {
|
|
464
|
+
forwardNodes.push({ userId: node.sender?.user_id, userName: node.sender?.nickname, elements: contentElements });
|
|
465
|
+
}
|
|
446
466
|
}
|
|
447
467
|
if (forwardNodes.length > 0) result.push({ type: "forward", content: forwardNodes });
|
|
448
468
|
} else if (["image", "video", "audio", "file"].includes(type) && el.attrs.src) {
|
|
@@ -1008,6 +1028,18 @@ function apply(ctx, config) {
|
|
|
1008
1028
|
const reviewManager = config.enablePend ? new PendManager(ctx, config, fileManager, logger, reusableIds) : null;
|
|
1009
1029
|
const hashManager = config.enableSimilarity ? new HashManager(ctx, config, logger, fileManager) : null;
|
|
1010
1030
|
const dataManager = config.enableIO ? new DataManager(ctx, config, fileManager, logger) : null;
|
|
1031
|
+
ctx.on("ready", async () => {
|
|
1032
|
+
try {
|
|
1033
|
+
const staleCaves = await ctx.database.get("cave", { status: "preload" });
|
|
1034
|
+
if (staleCaves.length > 0) {
|
|
1035
|
+
const idsToMark = staleCaves.map((c) => ({ id: c.id, status: "delete" }));
|
|
1036
|
+
await ctx.database.upsert("cave", idsToMark);
|
|
1037
|
+
await cleanupPendingDeletions(ctx, fileManager, logger, reusableIds);
|
|
1038
|
+
}
|
|
1039
|
+
} catch (error) {
|
|
1040
|
+
logger.error("清理残留回声洞时发生错误:", error);
|
|
1041
|
+
}
|
|
1042
|
+
});
|
|
1011
1043
|
const cave = ctx.command("cave", "回声洞").option("add", "-a <content:text> 添加回声洞").option("view", "-g <id:posint> 查看指定回声洞").option("delete", "-r <id:posint> 删除指定回声洞").option("list", "-l 查询投稿统计").usage("随机抽取一条已添加的回声洞。").action(async ({ session, options }) => {
|
|
1012
1044
|
if (options.add) return session.execute(`cave.add ${options.add}`);
|
|
1013
1045
|
if (options.view) return session.execute(`cave.view ${options.view}`);
|