koishi-plugin-ccb-plus 0.1.5 → 0.1.6

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 +65 -18
  2. package/package.json +2 -1
package/lib/index.js CHANGED
@@ -54,6 +54,8 @@ function apply(ctx, config) {
54
54
  const LOG_FILE = path.join(ctx.baseDir, "data", "ccb_log.json");
55
55
  const actionTimes = {};
56
56
  const banList = {};
57
+ const nicknameCache = /* @__PURE__ */ new Map();
58
+ const CACHE_DURATION = 5 * 60 * 1e3;
57
59
  function getAvatar(userId) {
58
60
  return `https://q4.qlogo.cn/headimg_dl?dst_uin=${userId}&spec=640`;
59
61
  }
@@ -116,15 +118,27 @@ function apply(ctx, config) {
116
118
  }
117
119
  __name(appendLog, "appendLog");
118
120
  async function getUserNickname(session, userId) {
121
+ const cacheKey = `${session.guildId}:${userId}`;
122
+ const cached = nicknameCache.get(cacheKey);
123
+ const now = Date.now();
124
+ if (cached && now - cached.timestamp < CACHE_DURATION) {
125
+ return cached.name;
126
+ }
119
127
  try {
120
- const userInfo = await session.bot.getUser(userId);
121
- return userInfo?.name || userId;
128
+ const memberInfo = await session.bot.getGuildMember(session.guildId, userId);
129
+ const nickname = memberInfo?.nick || memberInfo?.name || `用户${userId}`;
130
+ nicknameCache.set(cacheKey, { name: nickname, timestamp: now });
131
+ return nickname;
122
132
  } catch (error) {
123
133
  try {
124
- const memberInfo = await session.bot.getGuildMember(session.guildId, userId);
125
- return memberInfo?.nick || userId;
134
+ const userInfo = await session.bot.getUser(userId);
135
+ const nickname = userInfo?.name || `用户${userId}`;
136
+ nicknameCache.set(cacheKey, { name: nickname, timestamp: now });
137
+ return nickname;
126
138
  } catch (e) {
127
- return userId;
139
+ const friendlyName = `用户${userId}`;
140
+ nicknameCache.set(cacheKey, { name: friendlyName, timestamp: now });
141
+ return friendlyName;
128
142
  }
129
143
  }
130
144
  }
@@ -204,7 +218,7 @@ function apply(ctx, config) {
204
218
  const recordIndex = groupData.findIndex((item) => item.id === targetUserId);
205
219
  if (recordIndex !== -1) {
206
220
  const item = groupData[recordIndex];
207
- const nickname = await getUserNickname(session, targetUserId) || targetUserId;
221
+ const nickname = await getUserNickname(session, targetUserId);
208
222
  item.num = (item.num || 0) + 1;
209
223
  item.vol = parseFloat((item.vol + V).toFixed(2));
210
224
  let ccb_by = item.ccb_by || {};
@@ -286,7 +300,7 @@ function apply(ctx, config) {
286
300
  }
287
301
  } else {
288
302
  try {
289
- const nickname = await getUserNickname(session, targetUserId) || targetUserId;
303
+ const nickname = await getUserNickname(session, targetUserId);
290
304
  const resultMessage = `你和${nickname}发生了${duration}min长的ccb行为,向ta注入了${V.toFixed(2)}ml的生命因子`;
291
305
  const message = [
292
306
  resultMessage,
@@ -332,11 +346,12 @@ function apply(ctx, config) {
332
346
  return "当前群暂无ccb记录。";
333
347
  }
334
348
  const top5 = groupData.sort((a, b) => (b.num || 0) - (a.num || 0)).slice(0, 5);
349
+ const nicknamePromises = top5.map((r) => getUserNickname(session, r.id));
350
+ const nicknames = await Promise.all(nicknamePromises);
335
351
  let msg = "被ccb排行榜 TOP5:\n";
336
352
  for (let i = 0; i < top5.length; i++) {
337
353
  const r = top5[i];
338
- const uid = r.id;
339
- const nick = await getUserNickname(session, uid) || uid;
354
+ const nick = nicknames[i] || r.id;
340
355
  msg += `${i + 1}. ${nick} - 次数:${r.num}
341
356
  `;
342
357
  }
@@ -352,11 +367,12 @@ function apply(ctx, config) {
352
367
  return "当前群暂无ccb记录。";
353
368
  }
354
369
  const top5 = groupData.sort((a, b) => (b.vol || 0) - (a.vol || 0)).slice(0, 5);
370
+ const nicknamePromises = top5.map((r) => getUserNickname(session, r.id));
371
+ const nicknames = await Promise.all(nicknamePromises);
355
372
  let msg = "被注入量排行榜 TOP5:\n";
356
373
  for (let i = 0; i < top5.length; i++) {
357
374
  const r = top5[i];
358
- const uid = r.id;
359
- const nick = await getUserNickname(session, uid) || uid;
375
+ const nick = nicknames[i] || r.id;
360
376
  msg += `${i + 1}. ${nick} - 累计注入:${r.vol.toFixed(2)}ml
361
377
  `;
362
378
  }
@@ -391,10 +407,12 @@ function apply(ctx, config) {
391
407
  }
392
408
  entries.sort((a, b) => b[1] - a[1]);
393
409
  const top5 = entries.slice(0, 5);
394
- let msg = "单次最大注入排行榜 TOP5:\n";
410
+ const userIds = [];
411
+ const producerIds = [];
395
412
  for (let i = 0; i < top5.length; i++) {
396
- const [r, max_val] = top5[i];
413
+ const [r] = top5[i];
397
414
  const uid = r.id;
415
+ userIds.push(uid);
398
416
  let producer_id = null;
399
417
  const ccb_by = r.ccb_by || {};
400
418
  for (const actor_id in ccb_by) {
@@ -414,8 +432,24 @@ function apply(ctx, config) {
414
432
  producer_id = null;
415
433
  }
416
434
  }
417
- const nick = await getUserNickname(session, uid) || uid;
418
- const producer_nick = producer_id ? await getUserNickname(session, producer_id) : "未知";
435
+ producerIds.push(producer_id);
436
+ if (producer_id) {
437
+ userIds.push(producer_id);
438
+ }
439
+ }
440
+ const allNicknames = /* @__PURE__ */ new Map();
441
+ const uniqueUserIds = [...new Set(userIds)];
442
+ const nicknamePromises = uniqueUserIds.map((uid) => getUserNickname(session, uid));
443
+ const nicknameResults = await Promise.all(nicknamePromises);
444
+ for (let i = 0; i < uniqueUserIds.length; i++) {
445
+ allNicknames.set(uniqueUserIds[i], nicknameResults[i] || uniqueUserIds[i]);
446
+ }
447
+ let msg = "单次最大注入排行榜 TOP5:\n";
448
+ for (let i = 0; i < top5.length; i++) {
449
+ const [r, max_val] = top5[i];
450
+ const uid = r.id;
451
+ const nick = allNicknames.get(uid) || uid;
452
+ const producer_nick = producerIds[i] ? allNicknames.get(producerIds[i]) || "未知" : "未知";
419
453
  msg += `${i + 1}. ${nick} - 单次最大:${max_val.toFixed(2)}ml(${producer_nick})
420
454
  `;
421
455
  }
@@ -473,11 +507,13 @@ function apply(ctx, config) {
473
507
  } catch (error) {
474
508
  cb_total = 0;
475
509
  }
510
+ const userIds = [targetUserId];
476
511
  let first_actor = null;
477
512
  const ccb_by = record.ccb_by || {};
478
513
  for (const actor_id in ccb_by) {
479
514
  if (ccb_by[actor_id].first) {
480
515
  first_actor = actor_id;
516
+ userIds.push(first_actor);
481
517
  break;
482
518
  }
483
519
  }
@@ -487,9 +523,17 @@ function apply(ctx, config) {
487
523
  ([maxId, maxInfo], [currentId, currentInfo]) => (maxInfo?.count || 0) > (currentInfo?.count || 0) ? [maxId, maxInfo] : [currentId, currentInfo]
488
524
  );
489
525
  first_actor = maxEntry[0];
526
+ userIds.push(first_actor);
527
+ }
528
+ const allNicknames = /* @__PURE__ */ new Map();
529
+ const uniqueUserIds = [...new Set(userIds)];
530
+ const nicknamePromises = uniqueUserIds.map((uid) => getUserNickname(session, uid));
531
+ const nicknameResults = await Promise.all(nicknamePromises);
532
+ for (let i = 0; i < uniqueUserIds.length; i++) {
533
+ allNicknames.set(uniqueUserIds[i], nicknameResults[i] || uniqueUserIds[i]);
490
534
  }
491
- const first_nick = first_actor ? await getUserNickname(session, first_actor) : "未知";
492
- const target_nick = await getUserNickname(session, targetUserId) || targetUserId;
535
+ const first_nick = first_actor ? allNicknames.get(first_actor) || "未知" : "未知";
536
+ const target_nick = allNicknames.get(targetUserId) || targetUserId;
493
537
  const msg = [
494
538
  `【${target_nick} 】`,
495
539
  `• 破壁人:${first_nick || "未知"}`,
@@ -531,10 +575,13 @@ function apply(ctx, config) {
531
575
  }
532
576
  ranking.sort((a, b) => b[1] - a[1]);
533
577
  const top5 = ranking.slice(0, 5);
578
+ const userIds = top5.map((item) => item[0]);
579
+ const nicknamePromises = userIds.map((uid) => getUserNickname(session, uid));
580
+ const nicknames = await Promise.all(nicknamePromises);
534
581
  let msg = "💎 小南梁 TOP5 💎\n";
535
582
  for (let i = 0; i < top5.length; i++) {
536
583
  const [uid, xnn_val] = top5[i];
537
- const nick = await getUserNickname(session, uid) || uid;
584
+ const nick = nicknames[i] || uid;
538
585
  msg += `${i + 1}. ${nick} - XNN值:${xnn_val.toFixed(2)}
539
586
  `;
540
587
  }
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "koishi-plugin-ccb-plus",
3
3
  "description": "Koishi插件,与群友发生ccb行为。(移植自astrbot_plugin_ccb_plus)",
4
- "version": "0.1.5",
4
+ "version": "0.1.6",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
8
8
  "lib",
9
9
  "dist"
10
10
  ],
11
+ "homepage": "https://github.com/muyni233/koishi-plugin-ccb-plus",
11
12
  "license": "AGPL-3.0-only",
12
13
  "keywords": [
13
14
  "chatbot",