koishi-plugin-cocoyyy-console 1.2.2-alpha.1 → 1.2.2-alpha.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.
Files changed (2) hide show
  1. package/lib/index.js +55 -29
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1781,6 +1781,29 @@ 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") {
1793
+ const t = seg.data?.text ?? "";
1794
+ if (t) msgTexts.push(t);
1795
+ } else if (seg?.type === "image") {
1796
+ const url = seg.data?.url ?? seg.data?.file ?? "";
1797
+ if (url) allImageUrls.push(url);
1798
+ }
1799
+ }
1800
+ if (msgTexts.length > 0) {
1801
+ allTexts.push(`[${sender}]: ${msgTexts.join("")}`);
1802
+ }
1803
+ }
1804
+ return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1805
+ }
1806
+ __name(parseOneBotForwardNodes, "parseOneBotForwardNodes");
1784
1807
  function extractFromForwardChildren(children) {
1785
1808
  const allTexts = [];
1786
1809
  const allImageUrls = [];
@@ -1807,33 +1830,26 @@ function extractFromForwardChildren(children) {
1807
1830
  }
1808
1831
  __name(extractFromForwardChildren, "extractFromForwardChildren");
1809
1832
  async function fetchForwardMessages(session, forwardId) {
1833
+ const internal = session.bot.internal;
1834
+ let result;
1810
1835
  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
- }
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: "" };
1831
1844
  }
1832
- return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1833
- } catch (e) {
1834
- logger.error("[fetchForwardMessages Error]: 获取转发消息失败: " + e);
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(", "));
1835
1850
  return { imageUrls: [], text: "" };
1836
1851
  }
1852
+ return parseOneBotForwardNodes(messages);
1837
1853
  }
1838
1854
  __name(fetchForwardMessages, "fetchForwardMessages");
1839
1855
  function extractForwardId(elements, content) {
@@ -1873,6 +1889,7 @@ async function checkShitOrNot_v2(session, parttern_msg, config) {
1873
1889
  const quoteContent = session.quote.content;
1874
1890
  const quoteElements = session.quote.elements ?? [];
1875
1891
  logger.info("[checkShitOrNot_v2 Info]: 引用消息内容: " + quoteContent);
1892
+ logger.info("[checkShitOrNot_v2 Info]: quoteElements 长度: " + quoteElements.length);
1876
1893
  let imageUrls = [];
1877
1894
  let text = "";
1878
1895
  let isForward = false;
@@ -1881,15 +1898,24 @@ async function checkShitOrNot_v2(session, parttern_msg, config) {
1881
1898
  isForward = true;
1882
1899
  logger.info("[checkShitOrNot_v2 Info]: 检测到转发聊天记录,ID: " + forwardId);
1883
1900
  const forwardEl = quoteElements.find((el) => el.type === "forward");
1884
- const forwardChildren = forwardEl?.children ?? [];
1885
- if (forwardChildren.length > 0) {
1886
- const extracted = extractFromForwardChildren(forwardChildren);
1901
+ const attrContent = Array.isArray(forwardEl?.attrs?.content) ? forwardEl.attrs.content : [];
1902
+ if (attrContent.length > 0) {
1903
+ logger.info("[checkShitOrNot_v2 Info]: attrs.content 提取转发内容,共 " + attrContent.length + " 条");
1904
+ const extracted = parseOneBotForwardNodes(attrContent);
1887
1905
  imageUrls = extracted.imageUrls;
1888
1906
  text = extracted.text;
1889
- logger.info("[checkShitOrNot_v2 Info]: 从 children 提取转发内容,共 " + forwardChildren.length + " 条");
1890
1907
  }
1891
1908
  if (!text && imageUrls.length === 0) {
1892
- logger.info("[checkShitOrNot_v2 Info]: children 为空,尝试通过 NapCat API 拉取转发消息");
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 拉取转发消息");
1893
1919
  const extracted = await fetchForwardMessages(session, forwardId);
1894
1920
  imageUrls = extracted.imageUrls;
1895
1921
  text = extracted.text;
@@ -1903,7 +1929,7 @@ async function checkShitOrNot_v2(session, parttern_msg, config) {
1903
1929
  logger.warn("[checkShitOrNot_v2 Warn]: 引用消息中未发现文本或图片");
1904
1930
  return isForward ? "转发聊天记录中未发现可分析的文本或图片" : "引用消息中未发现文本或图片";
1905
1931
  }
1906
- logger.info("[checkShitOrNot_v2 Info]: 提取图片 URL: " + imageUrls.join(", "));
1932
+ logger.info("[checkShitOrNot_v2 Info]: 提取文本长度: " + text.length + ",图片数: " + imageUrls.length);
1907
1933
  const displayText = isForward ? `以下是转发聊天记录的内容:
1908
1934
 
1909
1935
  ${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.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [