koishi-plugin-cocoyyy-console 1.2.2-alpha.2 → 1.2.2-alpha.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.
Files changed (2) hide show
  1. package/lib/index.js +46 -74
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1781,77 +1781,64 @@ function extractImageAndText(content) {
1781
1781
  return { imageUrls, text };
1782
1782
  }
1783
1783
  __name(extractImageAndText, "extractImageAndText");
1784
- function parseOneBotForwardNodes(nodes) {
1785
- const allTexts = [];
1786
- const allImageUrls = [];
1787
- for (const node of nodes) {
1788
- const sender = node?.sender?.nickname ?? String(node?.sender?.user_id ?? "未知");
1789
- const segments = node?.message ?? node?.content ?? [];
1790
- const msgTexts = [];
1791
- for (const seg of segments) {
1792
- if (seg?.type === "text") {
1784
+ function parseSegments(segments, depth) {
1785
+ const texts = [];
1786
+ const imageUrls = [];
1787
+ for (const seg of segments) {
1788
+ switch (seg?.type) {
1789
+ case "text": {
1793
1790
  const t = seg.data?.text ?? "";
1794
- if (t) msgTexts.push(t);
1795
- } else if (seg?.type === "image") {
1791
+ if (t) texts.push(t);
1792
+ break;
1793
+ }
1794
+ case "image": {
1796
1795
  const url = seg.data?.url ?? seg.data?.file ?? "";
1797
- if (url) allImageUrls.push(url);
1796
+ if (url) imageUrls.push(url);
1797
+ break;
1798
1798
  }
1799
- }
1800
- if (msgTexts.length > 0) {
1801
- allTexts.push(`[${sender}]: ${msgTexts.join("")}`);
1799
+ case "forward": {
1800
+ if (depth >= 1) {
1801
+ texts.push("[嵌套转发消息]");
1802
+ } else {
1803
+ const nestedNodes = seg.data?.content ?? [];
1804
+ if (nestedNodes.length > 0) {
1805
+ const nested = parseOneBotForwardNodes(nestedNodes, depth + 1);
1806
+ if (nested.text) texts.push(`[嵌套转发记录]
1807
+ ${nested.text}
1808
+ [嵌套转发记录结束]`);
1809
+ imageUrls.push(...nested.imageUrls);
1810
+ } else {
1811
+ texts.push("[嵌套转发消息]");
1812
+ }
1813
+ }
1814
+ break;
1815
+ }
1816
+ case "json":
1817
+ case "xml":
1818
+ texts.push("[卡片消息]");
1819
+ break;
1820
+ default:
1821
+ break;
1802
1822
  }
1803
1823
  }
1804
- return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1824
+ return { texts, imageUrls };
1805
1825
  }
1806
- __name(parseOneBotForwardNodes, "parseOneBotForwardNodes");
1807
- function extractFromForwardChildren(children) {
1826
+ __name(parseSegments, "parseSegments");
1827
+ function parseOneBotForwardNodes(nodes, depth = 0) {
1808
1828
  const allTexts = [];
1809
1829
  const allImageUrls = [];
1810
- for (const msgEl of children) {
1811
- if (msgEl.type !== "message") continue;
1812
- const msgChildren = msgEl.children ?? [];
1813
- const authorEl = msgChildren.find((c) => c.type === "author");
1814
- const senderName = authorEl?.attrs?.name ?? String(authorEl?.attrs?.id ?? "未知");
1815
- const msgTexts = [];
1816
- for (const child of msgChildren) {
1817
- if (child.type === "text") {
1818
- const t = child.attrs?.content ?? "";
1819
- if (t) msgTexts.push(t);
1820
- } else if (child.type === "img" || child.type === "image") {
1821
- const url = child.attrs?.src ?? child.attrs?.url ?? "";
1822
- if (url) allImageUrls.push(url);
1823
- }
1824
- }
1825
- if (msgTexts.length > 0) {
1826
- allTexts.push(`[${senderName}]: ${msgTexts.join("")}`);
1830
+ for (const node of nodes) {
1831
+ const sender = node?.sender?.nickname ?? String(node?.sender?.user_id ?? "未知");
1832
+ const segments = node?.message ?? node?.content ?? [];
1833
+ const { texts, imageUrls } = parseSegments(segments, depth);
1834
+ allImageUrls.push(...imageUrls);
1835
+ if (texts.length > 0) {
1836
+ allTexts.push(`[${sender}]: ${texts.join("")}`);
1827
1837
  }
1828
1838
  }
1829
1839
  return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1830
1840
  }
1831
- __name(extractFromForwardChildren, "extractFromForwardChildren");
1832
- async function fetchForwardMessages(session, forwardId) {
1833
- const internal = session.bot.internal;
1834
- let result;
1835
- try {
1836
- result = await internal.getForwardMsg({ id: forwardId });
1837
- } catch (e1) {
1838
- logger.warn("[fetchForwardMessages Warn]: getForwardMsg 调用失败,尝试 get_forward_msg: " + e1);
1839
- try {
1840
- result = await internal.get_forward_msg({ id: forwardId });
1841
- } catch (e2) {
1842
- logger.error("[fetchForwardMessages Error]: 两种 API 调用均失败: " + e2);
1843
- return { imageUrls: [], text: "" };
1844
- }
1845
- }
1846
- logger.info("[fetchForwardMessages Info]: API 返回结构预览: " + JSON.stringify(result)?.slice(0, 300));
1847
- const messages = result?.messages ?? [];
1848
- if (messages.length === 0) {
1849
- logger.warn("[fetchForwardMessages Warn]: API 返回 messages 为空,result keys: " + Object.keys(result ?? {}).join(", "));
1850
- return { imageUrls: [], text: "" };
1851
- }
1852
- return parseOneBotForwardNodes(messages);
1853
- }
1854
- __name(fetchForwardMessages, "fetchForwardMessages");
1841
+ __name(parseOneBotForwardNodes, "parseOneBotForwardNodes");
1855
1842
  function extractForwardId(elements, content) {
1856
1843
  const forwardEl = elements.find((el) => el.type === "forward");
1857
1844
  if (forwardEl?.attrs?.id) return String(forwardEl.attrs.id);
@@ -1905,21 +1892,6 @@ async function checkShitOrNot_v2(session, parttern_msg, config) {
1905
1892
  imageUrls = extracted.imageUrls;
1906
1893
  text = extracted.text;
1907
1894
  }
1908
- if (!text && imageUrls.length === 0) {
1909
- const forwardChildren = forwardEl?.children ?? [];
1910
- if (forwardChildren.length > 0) {
1911
- logger.info("[checkShitOrNot_v2 Info]: 从 children 提取转发内容,共 " + forwardChildren.length + " 条");
1912
- const extracted = extractFromForwardChildren(forwardChildren);
1913
- imageUrls = extracted.imageUrls;
1914
- text = extracted.text;
1915
- }
1916
- }
1917
- if (!text && imageUrls.length === 0) {
1918
- logger.info("[checkShitOrNot_v2 Info]: 本地提取失败,尝试通过 NapCat API 拉取转发消息");
1919
- const extracted = await fetchForwardMessages(session, forwardId);
1920
- imageUrls = extracted.imageUrls;
1921
- text = extracted.text;
1922
- }
1923
1895
  } else {
1924
1896
  const extracted = extractImageAndText(quoteContent);
1925
1897
  imageUrls = extracted.imageUrls;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-cocoyyy-console",
3
3
  "description": "自用koishi插件,功能包含复读,记录黑历史,*人等",
4
- "version": "1.2.2-alpha.2",
4
+ "version": "1.2.2-alpha.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [