koishi-plugin-cat-raising 0.0.7 → 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.
- package/lib/index.js +139 -24
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -32,23 +32,135 @@ 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
|
|
37
|
-
const HISTORY_SIZE =
|
|
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
|
-
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
|
|
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)) {
|
|
137
|
+
session.send("看到啦看到啦,不要发那么多次嘛~");
|
|
49
138
|
return;
|
|
50
139
|
}
|
|
51
|
-
|
|
140
|
+
let biliInfo = "";
|
|
141
|
+
try {
|
|
142
|
+
const roomInfoUrl = `https://api.live.bilibili.com/room/v1/Room/get_info?room_id=${roomId}`;
|
|
143
|
+
const roomInfo = await ctx.http.get(roomInfoUrl);
|
|
144
|
+
if (roomInfo.code !== 0 || !roomInfo.data?.uid) throw new Error("无法通过直播间号获取UID");
|
|
145
|
+
const uid = roomInfo.data.uid;
|
|
146
|
+
const statsUrl = `https://api.bilibili.com/x/space/navnum?mid=${uid}`;
|
|
147
|
+
const statsInfo = await ctx.http.get(statsUrl);
|
|
148
|
+
if (statsInfo.code !== 0 || statsInfo.data?.video === void 0) throw new Error("无法获取用户投稿数");
|
|
149
|
+
const videoCount = statsInfo.data.video;
|
|
150
|
+
biliInfo = `
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
用户投稿数: ${videoCount}`;
|
|
154
|
+
try {
|
|
155
|
+
await session.send(`直播间: ${roomId}
|
|
156
|
+
用户投稿数: ${videoCount}`);
|
|
157
|
+
} catch (e) {
|
|
158
|
+
ctx.logger.warn(`向监听群 ${config.monitorGroup} 发送B站信息时失败:`, e);
|
|
159
|
+
}
|
|
160
|
+
} catch (error) {
|
|
161
|
+
ctx.logger.warn(`获取直播间 ${roomId} 的B站信息失败: ${error.message}`);
|
|
162
|
+
}
|
|
163
|
+
const forwardMessage = originalMessageContent + biliInfo;
|
|
52
164
|
try {
|
|
53
165
|
let forwardedMessageId;
|
|
54
166
|
if (config.isGroup) {
|
|
@@ -58,29 +170,32 @@ function apply(ctx, config) {
|
|
|
58
170
|
const result = await session.bot.sendPrivateMessage(config.targetQQ, forwardMessage);
|
|
59
171
|
forwardedMessageId = result[0];
|
|
60
172
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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();
|
|
65
181
|
}
|
|
66
182
|
} catch (error) {
|
|
67
183
|
session.send("🐱 - 转发失败,请检查配置");
|
|
68
|
-
|
|
184
|
+
ctx.logger.error("转发失败:", error);
|
|
69
185
|
}
|
|
70
186
|
});
|
|
71
187
|
ctx.on("message-deleted", async (session) => {
|
|
72
188
|
const originalMessageId = session.messageId;
|
|
73
|
-
|
|
74
|
-
|
|
189
|
+
const entryIndex = forwardedHistory.findIndex((entry) => entry.originalMessageId === originalMessageId);
|
|
190
|
+
if (entryIndex !== -1) {
|
|
191
|
+
const entry = forwardedHistory[entryIndex];
|
|
75
192
|
try {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
} else {
|
|
79
|
-
await session.bot.deleteMessage(config.targetQQ, forwardedMessageId);
|
|
80
|
-
}
|
|
81
|
-
messageMap.delete(originalMessageId);
|
|
193
|
+
await session.bot.deleteMessage(config.targetQQ, entry.forwardedMessageId);
|
|
194
|
+
ctx.logger.info(`成功撤回转发的消息: ${entry.forwardedMessageId}`);
|
|
82
195
|
} catch (error) {
|
|
83
|
-
|
|
196
|
+
ctx.logger.error(`撤回转发消息 (ID: ${entry.forwardedMessageId}) 失败:`, error);
|
|
197
|
+
} finally {
|
|
198
|
+
forwardedHistory.splice(entryIndex, 1);
|
|
84
199
|
}
|
|
85
200
|
}
|
|
86
201
|
});
|