koishi-plugin-cocoyyy-console 1.2.2-alpha.1 → 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 +57 -59
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1781,61 +1781,64 @@ function extractImageAndText(content) {
1781
1781
  return { imageUrls, text };
1782
1782
  }
1783
1783
  __name(extractImageAndText, "extractImageAndText");
1784
- function extractFromForwardChildren(children) {
1785
- const allTexts = [];
1786
- const allImageUrls = [];
1787
- for (const msgEl of children) {
1788
- if (msgEl.type !== "message") continue;
1789
- const msgChildren = msgEl.children ?? [];
1790
- const authorEl = msgChildren.find((c) => c.type === "author");
1791
- const senderName = authorEl?.attrs?.name ?? String(authorEl?.attrs?.id ?? "未知");
1792
- const msgTexts = [];
1793
- for (const child of msgChildren) {
1794
- if (child.type === "text") {
1795
- const t = child.attrs?.content ?? "";
1796
- if (t) msgTexts.push(t);
1797
- } else if (child.type === "img" || child.type === "image") {
1798
- const url = child.attrs?.src ?? child.attrs?.url ?? "";
1799
- if (url) allImageUrls.push(url);
1784
+ function parseSegments(segments, depth) {
1785
+ const texts = [];
1786
+ const imageUrls = [];
1787
+ for (const seg of segments) {
1788
+ switch (seg?.type) {
1789
+ case "text": {
1790
+ const t = seg.data?.text ?? "";
1791
+ if (t) texts.push(t);
1792
+ break;
1800
1793
  }
1801
- }
1802
- if (msgTexts.length > 0) {
1803
- allTexts.push(`[${senderName}]: ${msgTexts.join("")}`);
1794
+ case "image": {
1795
+ const url = seg.data?.url ?? seg.data?.file ?? "";
1796
+ if (url) imageUrls.push(url);
1797
+ break;
1798
+ }
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;
1804
1822
  }
1805
1823
  }
1806
- return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1824
+ return { texts, imageUrls };
1807
1825
  }
1808
- __name(extractFromForwardChildren, "extractFromForwardChildren");
1809
- async function fetchForwardMessages(session, forwardId) {
1810
- try {
1811
- const result = await session.bot.internal.getForwardMsg({ id: forwardId });
1812
- const messages = result?.messages ?? [];
1813
- const allTexts = [];
1814
- const allImageUrls = [];
1815
- for (const msg of messages) {
1816
- const sender = msg?.sender?.nickname ?? String(msg?.sender?.user_id ?? "未知");
1817
- const segments = msg?.message ?? msg?.content ?? [];
1818
- const msgTexts = [];
1819
- for (const seg of segments) {
1820
- if (seg.type === "text") {
1821
- const t = seg.data?.text ?? "";
1822
- if (t) msgTexts.push(t);
1823
- } else if (seg.type === "image") {
1824
- const url = seg.data?.url ?? seg.data?.file ?? "";
1825
- if (url) allImageUrls.push(url);
1826
- }
1827
- }
1828
- if (msgTexts.length > 0) {
1829
- allTexts.push(`[${sender}]: ${msgTexts.join("")}`);
1830
- }
1826
+ __name(parseSegments, "parseSegments");
1827
+ function parseOneBotForwardNodes(nodes, depth = 0) {
1828
+ const allTexts = [];
1829
+ const allImageUrls = [];
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("")}`);
1831
1837
  }
1832
- return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1833
- } catch (e) {
1834
- logger.error("[fetchForwardMessages Error]: 获取转发消息失败: " + e);
1835
- return { imageUrls: [], text: "" };
1836
1838
  }
1839
+ return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1837
1840
  }
1838
- __name(fetchForwardMessages, "fetchForwardMessages");
1841
+ __name(parseOneBotForwardNodes, "parseOneBotForwardNodes");
1839
1842
  function extractForwardId(elements, content) {
1840
1843
  const forwardEl = elements.find((el) => el.type === "forward");
1841
1844
  if (forwardEl?.attrs?.id) return String(forwardEl.attrs.id);
@@ -1873,6 +1876,7 @@ async function checkShitOrNot_v2(session, parttern_msg, config) {
1873
1876
  const quoteContent = session.quote.content;
1874
1877
  const quoteElements = session.quote.elements ?? [];
1875
1878
  logger.info("[checkShitOrNot_v2 Info]: 引用消息内容: " + quoteContent);
1879
+ logger.info("[checkShitOrNot_v2 Info]: quoteElements 长度: " + quoteElements.length);
1876
1880
  let imageUrls = [];
1877
1881
  let text = "";
1878
1882
  let isForward = false;
@@ -1881,16 +1885,10 @@ async function checkShitOrNot_v2(session, parttern_msg, config) {
1881
1885
  isForward = true;
1882
1886
  logger.info("[checkShitOrNot_v2 Info]: 检测到转发聊天记录,ID: " + forwardId);
1883
1887
  const forwardEl = quoteElements.find((el) => el.type === "forward");
1884
- const forwardChildren = forwardEl?.children ?? [];
1885
- if (forwardChildren.length > 0) {
1886
- const extracted = extractFromForwardChildren(forwardChildren);
1887
- imageUrls = extracted.imageUrls;
1888
- text = extracted.text;
1889
- logger.info("[checkShitOrNot_v2 Info]: 从 children 提取转发内容,共 " + forwardChildren.length + " 条");
1890
- }
1891
- if (!text && imageUrls.length === 0) {
1892
- logger.info("[checkShitOrNot_v2 Info]: children 为空,尝试通过 NapCat API 拉取转发消息");
1893
- const extracted = await fetchForwardMessages(session, forwardId);
1888
+ const attrContent = Array.isArray(forwardEl?.attrs?.content) ? forwardEl.attrs.content : [];
1889
+ if (attrContent.length > 0) {
1890
+ logger.info("[checkShitOrNot_v2 Info]: attrs.content 提取转发内容,共 " + attrContent.length + " 条");
1891
+ const extracted = parseOneBotForwardNodes(attrContent);
1894
1892
  imageUrls = extracted.imageUrls;
1895
1893
  text = extracted.text;
1896
1894
  }
@@ -1903,7 +1901,7 @@ async function checkShitOrNot_v2(session, parttern_msg, config) {
1903
1901
  logger.warn("[checkShitOrNot_v2 Warn]: 引用消息中未发现文本或图片");
1904
1902
  return isForward ? "转发聊天记录中未发现可分析的文本或图片" : "引用消息中未发现文本或图片";
1905
1903
  }
1906
- logger.info("[checkShitOrNot_v2 Info]: 提取图片 URL: " + imageUrls.join(", "));
1904
+ logger.info("[checkShitOrNot_v2 Info]: 提取文本长度: " + text.length + ",图片数: " + imageUrls.length);
1907
1905
  const displayText = isForward ? `以下是转发聊天记录的内容:
1908
1906
 
1909
1907
  ${text}` : text;
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.1",
4
+ "version": "1.2.2-alpha.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [