koishi-plugin-cat-raising 1.3.0 → 1.3.4

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 +120 -10
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -107,7 +107,7 @@ function extractDateTime(line) {
107
107
  if (match = line.match(/(\d{1,2})\s*[月.]\s*(\d{1,2})\s*日?/)) return `${match[1]}月${match[2]}日`;
108
108
  if (match = line.match(/每晚\s*(\d{1,2})\s*[点时]/)) return `每晚 ${match[1].padStart(2, "0")}:00`;
109
109
  if (match = line.match(/(\d{1,2}\s*月\s*(?:上|中|下)旬)/)) return match[1];
110
- if (match = line.match(/(\d{1,2})[::.点时]\s*(\d{1,2})/)) return `${match[1].padStart(2, "0")}:${match[2].padStart(2, "0")}`;
110
+ if (match = line.match(/(\d{1,2})[::∶.点时]\s*(\d{1,2})/)) return `${match[1].padStart(2, "0")}:${match[2].padStart(2, "0")}`;
111
111
  if (match = line.match(/(\d{1,2})\s*点\s*半/)) return `${match[1].padStart(2, "0")}:30`;
112
112
  if (match = line.match(/\b(\d{1,2})\s*[.点时](?!\d)/)) return `${match[1].padStart(2, "0")}:00`;
113
113
  if (match = line.match(/(\d{1,2})\s*分/)) {
@@ -122,12 +122,14 @@ function extractDateTime(line) {
122
122
  __name(extractDateTime, "extractDateTime");
123
123
  function extractRewards(line) {
124
124
  const rewards = [];
125
- const regex = /(?:(\d{1,2})\s*级(?:灯牌)?\s*)?(?:发\s*)?(\d+\.?\d*w\+?|\b\d{3,5}\b)(?:神金|钻石|猫猫钻)?/gi;
125
+ const regex = /(?:(\d{1,2})\s*级(?:灯牌)?\s*)?(?:发\s*)?(?:神金\s*)?(\d+\.?\d*\s*[wWkK万千]\+?|\b\d{3,5}\b)(?:\s*神金|\s*钻石|\s*猫猫钻)?/gi;
126
126
  let match;
127
127
  while ((match = regex.exec(line)) !== null) {
128
128
  const condition = match[1] ? `${match[1]}级灯牌` : "无限制";
129
- const amountStr = (match[2] || "").toLowerCase();
130
- const amount = amountStr.includes("w") ? parseFloat(amountStr.replace("w", "")) * 1e4 : parseFloat(amountStr);
129
+ const amountStr = (match[2] || "").toLowerCase().replace(/\s+/g, "");
130
+ const base = parseFloat(amountStr.replace(/[^\d.]/g, ""));
131
+ const multiplier = /[w万]/i.test(amountStr) ? 1e4 : /[k千]/i.test(amountStr) ? 1e3 : 1;
132
+ const amount = base * multiplier;
131
133
  if (!isNaN(amount) && amount > 0) {
132
134
  rewards.push({ amount, condition });
133
135
  }
@@ -232,11 +234,32 @@ async function fetchBilibiliInfo(ctx, roomId) {
232
234
  "Origin": "https://live.bilibili.com",
233
235
  "Referer": `https://live.bilibili.com/${roomId}`
234
236
  };
235
- const roomInfo = await ctx.http.get(
236
- `https://api.live.bilibili.com/room/v1/Room/get_info?room_id=${roomId}`,
237
- { headers: commonHeaders }
238
- );
239
- const uid = roomInfo?.data?.uid;
237
+ let uid;
238
+ let anchorName = "未知";
239
+ try {
240
+ const infoByRoom = await ctx.http.get(
241
+ `https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=${roomId}`,
242
+ { headers: commonHeaders }
243
+ );
244
+ uid = infoByRoom?.data?.room_info?.uid || uid;
245
+ anchorName = infoByRoom?.data?.anchor_info?.base_info?.uname || infoByRoom?.data?.anchor_info?.uname || anchorName;
246
+ } catch {
247
+ }
248
+ if (!uid || anchorName === "未知") {
249
+ try {
250
+ const initInfo = await ctx.http.get(
251
+ `https://api.live.bilibili.com/room/v1/Room/room_init?id=${roomId}`,
252
+ { headers: commonHeaders }
253
+ );
254
+ uid = initInfo?.data?.uid || uid;
255
+ const roomInfo2 = await ctx.http.get(
256
+ `https://api.live.bilibili.com/room/v1/Room/get_info?room_id=${initInfo?.data?.room_id || roomId}`,
257
+ { headers: commonHeaders }
258
+ );
259
+ anchorName = roomInfo2?.data?.uname || anchorName;
260
+ } catch {
261
+ }
262
+ }
240
263
  if (!uid) throw new Error("无法从房间信息中获取UID");
241
264
  const statsInfo = await ctx.http.get(
242
265
  `https://api.bilibili.com/x/space/navnum?mid=${uid}`,
@@ -250,7 +273,86 @@ async function fetchBilibiliInfo(ctx, roomId) {
250
273
  );
251
274
  const videoCount = statsInfo?.data?.video;
252
275
  if (videoCount === void 0) throw new Error("无法从空间信息中获取投稿数");
253
- return { videoCount };
276
+ try {
277
+ const anchorInfo = await ctx.http.get(
278
+ `https://api.bilibili.com/x/space/acc/info?mid=${uid}`,
279
+ {
280
+ headers: {
281
+ ...commonHeaders,
282
+ Origin: "https://space.bilibili.com",
283
+ Referer: `https://space.bilibili.com/${uid}`
284
+ }
285
+ }
286
+ );
287
+ anchorName = anchorInfo?.data?.name || anchorName;
288
+ } catch {
289
+ }
290
+ if (anchorName === "未知") {
291
+ try {
292
+ const liveUser = await ctx.http.get(
293
+ `https://api.live.bilibili.com/live_user/v3/UserInfo/get_info?uid=${uid}`,
294
+ {
295
+ headers: {
296
+ ...commonHeaders,
297
+ Origin: "https://live.bilibili.com",
298
+ Referer: `https://live.bilibili.com/${roomId}`
299
+ }
300
+ }
301
+ );
302
+ anchorName = liveUser?.data?.info?.uname || anchorName;
303
+ } catch {
304
+ }
305
+ }
306
+ if (anchorName === "未知") {
307
+ try {
308
+ const baseInfo = await ctx.http.get(
309
+ `https://api.live.bilibili.com/xlive/web-room/v1/index/getRoomBaseInfo?room_id=${roomId}`,
310
+ { headers: commonHeaders }
311
+ );
312
+ anchorName = baseInfo?.data?.anchor_info?.base_info?.uname || baseInfo?.data?.anchor_info?.uname || baseInfo?.data?.room_info?.uname || baseInfo?.data?.uname || anchorName;
313
+ } catch {
314
+ }
315
+ }
316
+ if (anchorName === "未知") {
317
+ await sleep(300);
318
+ try {
319
+ const infoByRoom2 = await ctx.http.get(
320
+ `https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=${roomId}`,
321
+ { headers: commonHeaders }
322
+ );
323
+ anchorName = infoByRoom2?.data?.anchor_info?.base_info?.uname || infoByRoom2?.data?.anchor_info?.uname || anchorName;
324
+ } catch {
325
+ }
326
+ try {
327
+ const anchorInfo2 = await ctx.http.get(
328
+ `https://api.bilibili.com/x/space/acc/info?mid=${uid}`,
329
+ {
330
+ headers: {
331
+ ...commonHeaders,
332
+ Origin: "https://space.bilibili.com",
333
+ Referer: `https://space.bilibili.com/${uid}`
334
+ }
335
+ }
336
+ );
337
+ anchorName = anchorInfo2?.data?.name || anchorName;
338
+ } catch {
339
+ }
340
+ try {
341
+ const liveUser2 = await ctx.http.get(
342
+ `https://api.live.bilibili.com/live_user/v3/UserInfo/get_info?uid=${uid}`,
343
+ {
344
+ headers: {
345
+ ...commonHeaders,
346
+ Origin: "https://live.bilibili.com",
347
+ Referer: `https://live.bilibili.com/${roomId}`
348
+ }
349
+ }
350
+ );
351
+ anchorName = liveUser2?.data?.info?.uname || anchorName;
352
+ } catch {
353
+ }
354
+ }
355
+ return { videoCount, anchorName };
254
356
  } catch (error) {
255
357
  const status = error?.response?.status;
256
358
  const dataMsg = error?.response?.data?.message;
@@ -293,7 +395,9 @@ function apply(ctx, config) {
293
395
  let helperMessageId;
294
396
  if (groupConfig.sendHelperMessages) {
295
397
  try {
398
+ const displayAnchor = biliInfo.anchorName;
296
399
  [helperMessageId] = await session.send(`直播间: ${roomId}
400
+ 主播: ${displayAnchor}
297
401
  投稿数: ${biliInfo.videoCount}`);
298
402
  } catch (e) {
299
403
  ctx.logger.warn("[消息] 发送辅助信息失败:", e);
@@ -305,9 +409,11 @@ function apply(ctx, config) {
305
409
  return;
306
410
  }
307
411
  try {
412
+ const displayAnchor = biliInfo.anchorName;
308
413
  const forwardMessage = `${session.content}
309
414
 
310
415
  ---
416
+ 主播: ${displayAnchor}
311
417
  投稿数: ${biliInfo.videoCount}`;
312
418
  const [forwardedMessageId] = config.isGroup ? await session.bot.sendMessage(config.targetQQ, forwardMessage) : await session.bot.sendPrivateMessage(config.targetQQ, forwardMessage);
313
419
  forwardedHistory.push({
@@ -369,6 +475,10 @@ function apply(ctx, config) {
369
475
  });
370
476
  }
371
477
  __name(apply, "apply");
478
+ function sleep(ms) {
479
+ return new Promise((resolve) => setTimeout(resolve, ms));
480
+ }
481
+ __name(sleep, "sleep");
372
482
  // Annotate the CommonJS export names for ESM import in node:
373
483
  0 && (module.exports = {
374
484
  Config,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-cat-raising",
3
3
  "description": "",
4
- "version": "1.3.0",
4
+ "version": "1.3.4",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [