koishi-plugin-toram 4.2.0 → 4.2.1

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
@@ -178,7 +178,7 @@ var JsonMgr = _JsonMgr;
178
178
 
179
179
  // src/script/botFunction/monthlyCardReminder.ts
180
180
  var MONTHLY_CARD_REMINDER_TYPE = "monthly-card-reminder";
181
- function scheduleMonthlyCardReminders(groupId, hours) {
181
+ async function scheduleMonthlyCardReminders(groupId, hours) {
182
182
  const now = /* @__PURE__ */ new Date();
183
183
  for (let i = 0; i < hours.length; i++) {
184
184
  const hour = hours[i];
@@ -190,12 +190,12 @@ function scheduleMonthlyCardReminders(groupId, hours) {
190
190
  (x) => x.type === MONTHLY_CARD_REMINDER_TYPE && x.payload?.groupId === groupId && x.payload?.timeIndex === i
191
191
  );
192
192
  if (!exists) {
193
- BotTodoMgr.Inst().addTodo(ts, MONTHLY_CARD_REMINDER_TYPE, { groupId, timeIndex: i });
193
+ await BotTodoMgr.Inst().addTodo(ts, MONTHLY_CARD_REMINDER_TYPE, { groupId, timeIndex: i });
194
194
  }
195
195
  }
196
196
  }
197
197
  __name(scheduleMonthlyCardReminders, "scheduleMonthlyCardReminders");
198
- async function executeMonthlyCardReminder(item) {
198
+ function executeMonthlyCardReminder(item) {
199
199
  const payload = item.payload || {};
200
200
  const groupId = payload.groupId;
201
201
  const timeIndex = payload.timeIndex;
@@ -687,19 +687,18 @@ var _BotTodoMgr = class _BotTodoMgr {
687
687
  });
688
688
  }
689
689
  // 持久化内存中的 todo
690
- saveTodos() {
691
- const data = [];
692
- this._todos.forEach((list) => data.push(...list));
693
- this.ctx.database.upsert("toram_todo", data);
690
+ async saveTodos() {
691
+ const data = Array.from(this._todos.values()).flat();
692
+ await this.ctx.database.upsert("toram_todo", data);
694
693
  }
695
694
  // 新增一条 todo,返回 id
696
- addTodo(ts, type, payload) {
695
+ async addTodo(ts, type, payload) {
697
696
  const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
698
697
  const todo = { id, ts, type, payload };
699
698
  const list = this._todos.get(ts) || [];
700
699
  list.push(todo);
701
700
  this._todos.set(ts, list);
702
- this.saveTodos();
701
+ await this.saveTodos();
703
702
  if (this._started) {
704
703
  if (this._nextTs === void 0 || ts < this._nextTs) {
705
704
  this.scheduleNext();
@@ -713,22 +712,22 @@ var _BotTodoMgr = class _BotTodoMgr {
713
712
  return list.map((x) => ({ ...x }));
714
713
  }
715
714
  // 删除某个时间戳下的指定 todo
716
- removeTodo(ts, id) {
715
+ async removeTodo(ts, id) {
717
716
  const list = this._todos.get(ts);
718
717
  if (!list) return;
719
718
  const next = list.filter((x) => x.id !== id);
720
719
  if (next.length > 0) this._todos.set(ts, next);
721
720
  else this._todos.delete(ts);
722
- this.saveTodos();
721
+ await this.saveTodos();
723
722
  }
724
723
  // 删除某个时间戳下的所有 todo
725
- removeTodosAt(ts) {
724
+ async removeTodosAt(ts) {
726
725
  if (!this._todos.has(ts)) return;
727
726
  this._todos.delete(ts);
728
- this.saveTodos();
727
+ await this.saveTodos();
729
728
  }
730
729
  // 取出并删除“到期”的 todo 列表(ts <= now)
731
- popDueTodos(now) {
730
+ async popDueTodos(now) {
732
731
  const result = [];
733
732
  const keys = Array.from(this._todos.keys()).filter((ts) => ts <= now).sort((a, b) => a - b);
734
733
  keys.forEach((ts) => {
@@ -736,7 +735,7 @@ var _BotTodoMgr = class _BotTodoMgr {
736
735
  if (list && list.length) result.push(...list);
737
736
  this._todos.delete(ts);
738
737
  });
739
- if (keys.length > 0) this.saveTodos();
738
+ if (keys.length > 0) await this.saveTodos();
740
739
  return result;
741
740
  }
742
741
  // 下一个最近的时间戳(若不存在返回 undefined)
@@ -764,10 +763,10 @@ var _BotTodoMgr = class _BotTodoMgr {
764
763
  this._nextTs = void 0;
765
764
  }
766
765
  // 立即重新计算下一个调度点
767
- scheduleNext() {
766
+ async scheduleNext() {
768
767
  if (!this._started) return;
769
768
  try {
770
- const dropped = this.popDueTodos(Date.now());
769
+ const dropped = await this.popDueTodos(Date.now());
771
770
  if (dropped.length) {
772
771
  console.warn("BotTodoMgr 清理过期任务数量: ", dropped.length);
773
772
  }
@@ -797,7 +796,7 @@ var _BotTodoMgr = class _BotTodoMgr {
797
796
  const list = this._todos.get(scheduledTs) || [];
798
797
  if (list.length) {
799
798
  this._todos.delete(scheduledTs);
800
- this.saveTodos();
799
+ await this.saveTodos();
801
800
  await this.executeBatch(list);
802
801
  }
803
802
  } catch (e) {
@@ -889,7 +888,7 @@ async function apply(ctx, config) {
889
888
  TalkMgr.Inst().setTalks();
890
889
  await UserMgr.Inst().setUsers(ctx);
891
890
  MonsterMgr.Inst().setMonsters();
892
- BotTodoMgr.Inst().setTodos(ctx);
891
+ await BotTodoMgr.Inst().setTodos(ctx);
893
892
  registerBotTodoHandlers();
894
893
  BotTodoMgr.Inst().start();
895
894
  scheduleMonthlyCardReminders(config.groupId, [
@@ -21,16 +21,16 @@ export declare class BotTodoMgr {
21
21
  private _started;
22
22
  setTodos(ctx: Context): Promise<void>;
23
23
  private saveTodos;
24
- addTodo(ts: number, type: string, payload?: any): string;
24
+ addTodo(ts: number, type: string, payload?: any): Promise<string>;
25
25
  getTodosAt(ts: number): BotTodoItem[];
26
- removeTodo(ts: number, id: string): void;
27
- removeTodosAt(ts: number): void;
28
- popDueTodos(now: number): BotTodoItem[];
26
+ removeTodo(ts: number, id: string): Promise<void>;
27
+ removeTodosAt(ts: number): Promise<void>;
28
+ popDueTodos(now: number): Promise<BotTodoItem[]>;
29
29
  getNextTimestamp(): number | undefined;
30
30
  on(type: string, handler: (item: BotTodoItem) => any | Promise<any>): void;
31
31
  start(): void;
32
32
  stop(): void;
33
- scheduleNext(): void;
33
+ scheduleNext(): Promise<void>;
34
34
  private internalPlanNext;
35
35
  private executeBatch;
36
36
  }
@@ -1,4 +1,4 @@
1
1
  import { BotTodoItem } from "../BotTodoMgr";
2
2
  export declare const MONTHLY_CARD_REMINDER_TYPE = "monthly-card-reminder";
3
- export declare function scheduleMonthlyCardReminders(groupId: string, hours: number[]): void;
4
- export declare function executeMonthlyCardReminder(item: BotTodoItem): Promise<void>;
3
+ export declare function scheduleMonthlyCardReminders(groupId: string, hours: number[]): Promise<void>;
4
+ export declare function executeMonthlyCardReminder(item: BotTodoItem): void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-toram",
3
3
  "description": "托拉姆物语小工具",
4
- "version": "4.2.0",
4
+ "version": "4.2.1",
5
5
  "contributors": [
6
6
  "青灯 <1874053520@qq.com>"
7
7
  ],