koishi-plugin-rina-llevents 0.0.1 → 0.0.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.
package/lib/index.js CHANGED
@@ -624,6 +624,17 @@ __name(extendModel, "extendModel");
624
624
 
625
625
  // src/scheduler.ts
626
626
  var import_koishi3 = require("koishi");
627
+ function describeError(error) {
628
+ const message = error instanceof Error ? error.message : String(error);
629
+ const safe = message.replace(/(?:base64:\/\/|data:image\/[^;,]+;base64,)[A-Za-z0-9+/=]+/g, "[image omitted]").slice(0, 500);
630
+ const code = error && typeof error === "object" && "code" in error ? error.code : void 0;
631
+ return code === void 0 ? safe : `${safe} (code=${String(code)})`;
632
+ }
633
+ __name(describeError, "describeError");
634
+ function isUncertainMessageResult(error) {
635
+ return !!error && typeof error === "object" && "url" in error && ["send_msg", "send_group_msg", "send_private_msg"].includes(String(error.url));
636
+ }
637
+ __name(isUncertainMessageResult, "isUncertainMessageResult");
627
638
  var Scheduler = class {
628
639
  constructor(ctx, logger, service, config) {
629
640
  this.ctx = ctx;
@@ -635,21 +646,35 @@ var Scheduler = class {
635
646
  __name(this, "Scheduler");
636
647
  }
637
648
  running = false;
649
+ remindersRunning = false;
638
650
  start() {
639
651
  this.ctx.on("ready", async () => {
652
+ this.ctx.cron("* * * * *", () => this.tick());
640
653
  if (this.config.syncOnStart && this.activeTargets().length) {
641
- await this.service.syncIfDue(true).catch((error) => this.logger.warn(`初始活动同步失败:${String(error)}`));
654
+ await this.service.syncIfDue(true).catch((error) => this.logger.warn(`初始活动同步失败:${describeError(error)}`));
642
655
  }
643
- this.ctx.cron("* * * * *", () => void this.run());
644
656
  });
645
657
  }
658
+ async tick() {
659
+ await Promise.all([this.runReminders(), this.run()]);
660
+ }
661
+ async runReminders() {
662
+ if (this.remindersRunning) return;
663
+ this.remindersRunning = true;
664
+ try {
665
+ await this.sendReminders();
666
+ } catch (error) {
667
+ this.logger.warn(`检查开演提醒失败:${describeError(error)}`);
668
+ } finally {
669
+ this.remindersRunning = false;
670
+ }
671
+ }
646
672
  async run() {
647
673
  if (this.running) return;
648
674
  this.running = true;
649
675
  try {
650
676
  if (!this.activeTargets().length) return;
651
- await this.service.syncIfDue().catch((error) => this.logger.warn(`活动列表同步失败:${String(error)}`));
652
- await this.sendReminders();
677
+ await this.service.syncIfDue().catch((error) => this.logger.warn(`活动列表同步失败:${describeError(error)}`));
653
678
  await this.pollSetlists();
654
679
  } finally {
655
680
  this.running = false;
@@ -661,22 +686,44 @@ var Scheduler = class {
661
686
  targetKey(target) {
662
687
  return `${target.platform}:${target.selfId}:${target.channelId}`;
663
688
  }
664
- async send(target, content, image) {
689
+ async send(target, content) {
665
690
  const bot = this.ctx.bots.find((bot2) => bot2.platform === target.platform && bot2.selfId === target.selfId);
666
691
  if (!bot) throw new Error(`找不到机器人 ${target.platform}:${target.selfId}`);
667
692
  await bot.sendMessage(target.channelId, content);
668
- if (image) await bot.sendMessage(target.channelId, import_koishi3.h.image(image, "image/png"));
669
693
  }
670
694
  async sendOnce(target, performanceId, kind, contentHash, content, image) {
671
695
  const targetKey = this.targetKey(target);
672
696
  const id = `${performanceId}:${targetKey}:${kind}`;
673
697
  const old = (await this.ctx.database.get("rina_llevent_delivery", { id }))[0];
674
- if (old?.contentHash === contentHash) return false;
675
- await this.send(target, content, image);
676
- if (old) await this.ctx.database.set("rina_llevent_delivery", id, { contentHash, sentAt: /* @__PURE__ */ new Date() });
677
- else await this.ctx.database.create("rina_llevent_delivery", { id, contentHash, sentAt: /* @__PURE__ */ new Date() });
698
+ const previous = kind === "setlist" ? await this.setlistDelivery(target, performanceId) : old;
699
+ if (previous && (kind === "setlist" || kind === "setlist-caption" || previous.contentHash === contentHash)) return false;
700
+ if (image) {
701
+ await this.sendOnce(target, performanceId, `${kind}-caption`, contentHash, content);
702
+ try {
703
+ await this.send(target, import_koishi3.h.image(image, "image/png"));
704
+ } catch (error) {
705
+ if (!isUncertainMessageResult(error)) throw error;
706
+ await this.recordDelivery(`${id}-image-uncertain`, contentHash);
707
+ this.logger.warn(`歌单图片发送结果未确认(${performanceId},${targetKey}),已保留文字链接并停止此场次图片自动重试:${describeError(error)}`);
708
+ return false;
709
+ }
710
+ } else {
711
+ await this.send(target, content);
712
+ }
713
+ await this.recordDelivery(id, contentHash);
678
714
  return true;
679
715
  }
716
+ async recordDelivery(id, contentHash) {
717
+ const old = (await this.ctx.database.get("rina_llevent_delivery", { id }))[0];
718
+ const sentAt = /* @__PURE__ */ new Date();
719
+ if (old) await this.ctx.database.set("rina_llevent_delivery", id, { contentHash, sentAt });
720
+ else await this.ctx.database.create("rina_llevent_delivery", { id, contentHash, sentAt });
721
+ }
722
+ async setlistDelivery(target, performanceId) {
723
+ const id = `${performanceId}:${this.targetKey(target)}:setlist`;
724
+ const deliveries = await this.ctx.database.get("rina_llevent_delivery", { id: [id, `${id}-update`, `${id}-image-uncertain`] });
725
+ return deliveries[0];
726
+ }
680
727
  async maps() {
681
728
  const [events, concerts] = await Promise.all([
682
729
  this.ctx.database.get("rina_llevent_event", {}),
@@ -690,20 +737,21 @@ var Scheduler = class {
690
737
  async sendReminders() {
691
738
  const targets = this.activeTargets();
692
739
  if (!targets.length) return;
693
- const now = /* @__PURE__ */ new Date();
694
740
  const performances = await this.ctx.database.get("rina_llevent_performance", { canceled: false });
695
741
  const { events, concerts } = await this.maps();
696
742
  for (const performance of performances) {
697
743
  const start = performanceStart(performance);
698
744
  if (!start) continue;
745
+ const now = Date.now();
699
746
  const due = start.getTime() - this.config.reminderMinutes * 6e4;
700
- if (now.getTime() < due || now.getTime() >= due + 2 * 6e4) continue;
701
- const content = formatReminder(events.get(performance.eventId), concerts.get(performance.concertId), performance, start, this.config.reminderMinutes);
747
+ if (now < due || now >= start.getTime()) continue;
748
+ const minutes = Math.ceil((start.getTime() - now) / 6e4);
749
+ const content = formatReminder(events.get(performance.eventId), concerts.get(performance.concertId), performance, start, minutes);
702
750
  for (const target of targets) {
703
751
  try {
704
752
  await this.sendOnce(target, performance.id, "reminder", String(due), content);
705
753
  } catch (error) {
706
- this.logger.warn(`发送开演提醒失败:${String(error)}`);
754
+ this.logger.warn(`发送开演提醒失败:${describeError(error)}`);
707
755
  }
708
756
  }
709
757
  }
@@ -722,18 +770,26 @@ var Scheduler = class {
722
770
  const last = performance.setlistCheckedAt?.getTime() || 0;
723
771
  if (now.getTime() < pollStart || now.getTime() > pollEnd || now.getTime() - last < this.config.setlistPollIntervalMinutes * 6e4) continue;
724
772
  try {
725
- const oldHash = performance.setlistHash;
773
+ const deliveries = await Promise.all(targets.map(async (target) => ({
774
+ target,
775
+ previous: await this.setlistDelivery(target, performance.id)
776
+ })));
777
+ const pending = deliveries.filter(({ previous }) => !previous);
778
+ if (!pending.length) continue;
726
779
  const result = await this.service.refreshSetlist(performance);
727
780
  if (!result?.setlist.length) continue;
728
781
  const refreshed = result.performance;
729
- const updated = oldHash !== refreshed.setlistHash && oldHash !== EMPTY_SETLIST_HASH;
730
- const content = formatSetlistCaption(events.get(refreshed.eventId), concerts.get(refreshed.concertId), refreshed, updated);
731
782
  const image = await captureSetlistPage(this.ctx, canonicalUrl(refreshed.eventId, refreshed.concertId, refreshed.id));
732
- for (const target of targets) {
733
- await this.sendOnce(target, refreshed.id, updated ? "setlist-update" : "setlist", refreshed.setlistHash, content, image);
783
+ for (const { target } of pending) {
784
+ try {
785
+ const content = formatSetlistCaption(events.get(refreshed.eventId), concerts.get(refreshed.concertId), refreshed, false);
786
+ await this.sendOnce(target, refreshed.id, "setlist", refreshed.setlistHash, content, image);
787
+ } catch (error) {
788
+ this.logger.warn(`发送歌单失败(${refreshed.id},${this.targetKey(target)}):${describeError(error)}`);
789
+ }
734
790
  }
735
791
  } catch (error) {
736
- this.logger.warn(`轮询歌单失败(${performance.id}):${String(error)}`);
792
+ this.logger.warn(`轮询歌单失败(${performance.id}):${describeError(error)}`);
737
793
  }
738
794
  }
739
795
  }
@@ -7,13 +7,18 @@ export declare class Scheduler {
7
7
  private service;
8
8
  private config;
9
9
  private running;
10
+ private remindersRunning;
10
11
  constructor(ctx: Context, logger: Logger, service: LlEventService, config: Config);
11
12
  start(): void;
13
+ private tick;
14
+ private runReminders;
12
15
  private run;
13
16
  private activeTargets;
14
17
  private targetKey;
15
18
  private send;
16
19
  private sendOnce;
20
+ private recordDelivery;
21
+ private setlistDelivery;
17
22
  private maps;
18
23
  private sendReminders;
19
24
  private pollSetlists;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-rina-llevents",
3
3
  "description": "LoveLive活动信息订阅",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -14,6 +14,11 @@
14
14
  "koishi",
15
15
  "plugin"
16
16
  ],
17
+ "homepage": "https://github.com/738NGX/rinachanbot",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/738NGX/rinachanbot.git"
21
+ },
17
22
  "peerDependencies": {
18
23
  "koishi": "^4.18.7"
19
24
  },
package/readme.md CHANGED
@@ -18,8 +18,10 @@ https://ll-fans.jp/data/event/288?concert=427&performance=731
18
18
 
19
19
  - 定时轮询活动列表,发现新增、名称或日期变动,及近期一年内的活动详情变动。
20
20
  - 仅缓存未来一年内的详情;越接近开演检查越频繁。
21
- - 在 `performance.date + startTime`(日本时区)前 10 分钟发送一次提醒。
22
- - 从预计结束时刻开始轮询该场次歌单;首次出现时推送,随后变化时发送更新。
21
+ - 在 `performance.date + startTime`(日本时区)前 10 分钟发送一次提醒;提醒检查独立于列表同步与歌单截图,延迟时会在开演前补发并显示实际剩余分钟数。
22
+ - 从预计结束时刻开始等待该场次歌单;首次收录时,每个推送目标只发送一次“演出歌单”,后续歌单变化不再自动推送。所有目标均已处理后,停止该场次的歌单轮询与截图。
23
+ - 按场次和推送目标去重,兼容旧版首次推送及更新记录;已推送过的场次在升级或重启后不会再次发送。普通图片发送异常时仅重试图片,不重复发送已成功的文字。
24
+ - OneBot 图片发送请求报错或超时时,结果可能是“实际送达但回执失败”。插件会单独记为“发送结果未确认”,保留已发送的文字链接并停止此场次图片自动重试,避免重复刷屏;如图片确实未到达或需要最新歌单,可用 `ll歌单` 手动查询。
23
25
  - 歌单正文不入库;推送和 `ll歌单` 都使用 Puppeteer 截取 LL-Fans 原页面。
24
26
  - `performance.canceled` 会阻止提醒和歌单轮询;未来活动从列表消失只标为“待确认”,不会误判为取消。
25
27