koishi-plugin-cat-raising 0.0.8 → 0.0.9

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 +115 -29
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -32,21 +32,108 @@ var Config = import_koishi.Schema.object({
32
32
  isGroup: import_koishi.Schema.boolean().description("是否为QQ群").default(false),
33
33
  monitorGroup: import_koishi.Schema.string().description("监听的群号(只检测此群的消息)").required()
34
34
  });
35
+ function extractAllRoomIds(text) {
36
+ const patterns = [
37
+ /(?:播间号|房间号|直播间)[::\s]*(\d{6,15})/g,
38
+ /\b(\d{8,15})\b/g
39
+ // 独立的8位以上数字
40
+ ];
41
+ const foundIds = /* @__PURE__ */ new Set();
42
+ for (const pattern of patterns) {
43
+ const matches = text.matchAll(pattern);
44
+ for (const match of matches) {
45
+ if (match[1]) {
46
+ foundIds.add(match[1]);
47
+ }
48
+ }
49
+ }
50
+ return Array.from(foundIds);
51
+ }
52
+ __name(extractAllRoomIds, "extractAllRoomIds");
53
+ function extractDateTime(line) {
54
+ let match = line.match(/(\d{1,2})\s*[月.]\s*(\d{1,2})\s*日?/);
55
+ if (match) return `${match[1]}月${match[2]}日`;
56
+ match = line.match(/每晚\s*(\d{1,2})\s*点/);
57
+ if (match) return `每晚 ${match[1].padStart(2, "0")}:00`;
58
+ match = line.match(/(\d{1,2}\s*月\s*(?:上|中|下)旬)/);
59
+ if (match) return match[1];
60
+ match = line.match(/(\d{1,2})[::.点时]\s*(\d{1,2})/);
61
+ if (match) return `${match[1].padStart(2, "0")}:${match[2].padStart(2, "0")}`;
62
+ match = line.match(/(\d{1,2})\s*点\s*半/);
63
+ if (match) return `${match[1].padStart(2, "0")}:30`;
64
+ match = line.match(/(\d{1,2})\s*分/);
65
+ if (match) {
66
+ const now = /* @__PURE__ */ new Date();
67
+ const minuteVal = parseInt(match[1]);
68
+ let hourVal = now.getMinutes() > minuteVal ? now.getHours() + 1 : now.getHours();
69
+ hourVal = hourVal % 24;
70
+ return `${hourVal.toString().padStart(2, "0")}:${match[1].padStart(2, "0")}`;
71
+ }
72
+ match = line.match(/.*?(?:生日|周年|新衣|活动).*/);
73
+ if (match) return match[0].trim();
74
+ return null;
75
+ }
76
+ __name(extractDateTime, "extractDateTime");
77
+ function extractRewards(line) {
78
+ const rewards = [];
79
+ const regex = /(?:(\d{1,2})\s*级(?:灯牌)?\s*)?(\d+\.?\d*w?\+?)(?:神金|钻石|猫猫钻)?/gi;
80
+ let match;
81
+ while ((match = regex.exec(line)) !== null) {
82
+ const condition = match[1] ? `${match[1]}级灯牌` : "无限制";
83
+ let amountStr = match[2].toLowerCase();
84
+ let amount = 0;
85
+ if (amountStr.includes("w")) {
86
+ amount = parseFloat(amountStr.replace("w", "")) * 1e4;
87
+ } else {
88
+ amount = parseInt(amountStr, 10);
89
+ }
90
+ rewards.push({ amount, condition });
91
+ }
92
+ return rewards;
93
+ }
94
+ __name(extractRewards, "extractRewards");
95
+ function parseEvents(text) {
96
+ const lines = text.split("\n").filter((line) => line.trim() !== "");
97
+ const events = [];
98
+ let currentDateTime = null;
99
+ for (const line of lines) {
100
+ const foundDateTime = extractDateTime(line);
101
+ const foundRewards = extractRewards(line);
102
+ if (foundDateTime) {
103
+ currentDateTime = foundDateTime;
104
+ }
105
+ if (foundRewards.length > 0) {
106
+ const eventTime = currentDateTime || "时间未知";
107
+ events.push({ dateTime: eventTime, rewards: foundRewards });
108
+ if (foundDateTime) currentDateTime = null;
109
+ }
110
+ }
111
+ return events.length > 0 ? events : null;
112
+ }
113
+ __name(parseEvents, "parseEvents");
35
114
  function apply(ctx, config) {
36
- const forwardedMessageHistory = [];
37
- const HISTORY_SIZE = 10;
38
- const messageMap = /* @__PURE__ */ new Map();
115
+ const forwardedHistory = [];
116
+ const HISTORY_SIZE = 30;
39
117
  ctx.on("message", async (session) => {
118
+ if (session.channelId !== config.monitorGroup) return;
40
119
  const originalMessageContent = session.content;
41
120
  const messageForChecks = session.stripped.content;
42
121
  const messageId = session.messageId;
43
- if (session.channelId !== config.monitorGroup) return;
44
- if (!messageForChecks.includes("神金")) return;
45
- const numberRegex = /\d{6,15}/;
46
- const match = messageForChecks.match(numberRegex);
47
- if (!match) return;
48
- const roomId = match[0];
49
- if (forwardedMessageHistory.includes(originalMessageContent)) {
122
+ const roomIds = extractAllRoomIds(messageForChecks);
123
+ if (roomIds.length > 1) {
124
+ return;
125
+ }
126
+ if (roomIds.length === 0) {
127
+ }
128
+ const roomId = roomIds.length === 1 ? roomIds[0] : null;
129
+ const parsedEvents = parseEvents(messageForChecks);
130
+ if (!parsedEvents || !roomId) {
131
+ if (messageForChecks.match(/神金|w|发|掉落|\d{3,5}/)) {
132
+ ctx.logger.info(`消息可能为神金信息但无法完整解析(缺少房间号或事件),已忽略: ${messageForChecks.substring(0, 50)}...`);
133
+ }
134
+ return;
135
+ }
136
+ if (forwardedHistory.some((entry) => entry.originalContent === originalMessageContent)) {
50
137
  session.send("看到啦看到啦,不要发那么多次嘛~");
51
138
  return;
52
139
  }
@@ -54,15 +141,11 @@ function apply(ctx, config) {
54
141
  try {
55
142
  const roomInfoUrl = `https://api.live.bilibili.com/room/v1/Room/get_info?room_id=${roomId}`;
56
143
  const roomInfo = await ctx.http.get(roomInfoUrl);
57
- if (roomInfo.code !== 0 || !roomInfo.data || !roomInfo.data.uid) {
58
- throw new Error("无法通过直播间号获取UID");
59
- }
144
+ if (roomInfo.code !== 0 || !roomInfo.data?.uid) throw new Error("无法通过直播间号获取UID");
60
145
  const uid = roomInfo.data.uid;
61
146
  const statsUrl = `https://api.bilibili.com/x/space/navnum?mid=${uid}`;
62
147
  const statsInfo = await ctx.http.get(statsUrl);
63
- if (statsInfo.code !== 0 || !statsInfo.data || statsInfo.data.video === void 0) {
64
- throw new Error("无法获取用户投稿数");
65
- }
148
+ if (statsInfo.code !== 0 || statsInfo.data?.video === void 0) throw new Error("无法获取用户投稿数");
66
149
  const videoCount = statsInfo.data.video;
67
150
  biliInfo = `
68
151
 
@@ -87,10 +170,14 @@ function apply(ctx, config) {
87
170
  const result = await session.bot.sendPrivateMessage(config.targetQQ, forwardMessage);
88
171
  forwardedMessageId = result[0];
89
172
  }
90
- messageMap.set(messageId, forwardedMessageId);
91
- forwardedMessageHistory.push(originalMessageContent);
92
- if (forwardedMessageHistory.length > HISTORY_SIZE) {
93
- forwardedMessageHistory.shift();
173
+ const newEntry = {
174
+ originalMessageId: messageId,
175
+ forwardedMessageId,
176
+ originalContent: originalMessageContent
177
+ };
178
+ forwardedHistory.push(newEntry);
179
+ if (forwardedHistory.length > HISTORY_SIZE) {
180
+ forwardedHistory.shift();
94
181
  }
95
182
  } catch (error) {
96
183
  session.send("🐱 - 转发失败,请检查配置");
@@ -99,17 +186,16 @@ function apply(ctx, config) {
99
186
  });
100
187
  ctx.on("message-deleted", async (session) => {
101
188
  const originalMessageId = session.messageId;
102
- if (messageMap.has(originalMessageId)) {
103
- const forwardedMessageId = messageMap.get(originalMessageId);
189
+ const entryIndex = forwardedHistory.findIndex((entry) => entry.originalMessageId === originalMessageId);
190
+ if (entryIndex !== -1) {
191
+ const entry = forwardedHistory[entryIndex];
104
192
  try {
105
- if (config.isGroup) {
106
- await session.bot.deleteMessage(config.targetQQ, forwardedMessageId);
107
- } else {
108
- await session.bot.deleteMessage(config.targetQQ, forwardedMessageId);
109
- }
110
- messageMap.delete(originalMessageId);
193
+ await session.bot.deleteMessage(config.targetQQ, entry.forwardedMessageId);
194
+ ctx.logger.info(`成功撤回转发的消息: ${entry.forwardedMessageId}`);
111
195
  } catch (error) {
112
- ctx.logger.error("撤回转发消息失败:", error);
196
+ ctx.logger.error(`撤回转发消息 (ID: ${entry.forwardedMessageId}) 失败:`, error);
197
+ } finally {
198
+ forwardedHistory.splice(entryIndex, 1);
113
199
  }
114
200
  }
115
201
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-cat-raising",
3
3
  "description": "",
4
- "version": "0.0.8",
4
+ "version": "0.0.9",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [