koishi-plugin-prism 0.6.0 → 0.6.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/README.md CHANGED
@@ -212,3 +212,13 @@ Koishi 配置只剩 `baseUrl` 和 `integrationToken`(`shopCode` 可选、已
212
212
  ## 时间展示
213
213
 
214
214
  账单、历史与资产到期的时间由平台按店铺时区写好,例如「⏰ 游玩时间:18:00–19:00(UTC+08:00)」,不依赖 Bot 服务器时区。
215
+
216
+ ### Bot 定时播报
217
+
218
+ 播报组支持定时发送固定文字,可选每天、每周指定星期或指定日期单次发送,时间使用店铺时区,收件人沿用播报组。每组最多 20 条,每条最多 2000 字。需使用支持 schedules 的新版 Koishi 插件。
219
+
220
+ 插件 hello 声明 schedules: true,平台返回 schedules 帧(timeZone、rules)。rules 为启用且有目的地的规则,包含 id、time(HH:mm)、text、to,可选 date(YYYY-MM-DD)或 weekdays(0 为周日)。平台在重新 hello 时下发全量配置,配置变化通过 config.changed 让插件重新 hello。旧插件继续工作。定时播报由插件本地按分钟检查,无需增加服务端 alarm 或轮询请求。
221
+
222
+ 断线、停用或配置更新会取消旧计时,重连后恢复。离线错过的分钟跳过,夏令时跳过的时间也跳过,重复的当地时间在同一进程内每天最多发一次。发送失败沿用播报错误日志,不自动补发。单个 Bot 凭据应只运行一个插件实例,多实例各自计时可能重复发送。进程重启后仅安排后续分钟。
223
+
224
+ 0.6.1 增加由平台下发配置的本地定时播报,支持每天、每周和单次公告。
package/lib/index.d.ts CHANGED
@@ -159,12 +159,6 @@ type SocketReply = {
159
159
  body: any;
160
160
  serverMs?: number | null;
161
161
  };
162
- /**
163
- * The one connection to PRiSM. Commands go out as `command` frames and the
164
- * platform answers with `say` frames naming them; the shop's news comes as
165
- * `say` frames naming none. While it is down a command fails at once:
166
- * queueing a checkout or a top-up to run later would make players repeat it.
167
- */
168
162
  export declare class PrismSocket {
169
163
  private readonly open;
170
164
  private readonly url;
@@ -187,6 +181,8 @@ export declare class PrismSocket {
187
181
  private retryTimer;
188
182
  private pingTimer;
189
183
  private lastHeard;
184
+ private scheduleTimer?;
185
+ private readonly sentDate;
190
186
  private stopped;
191
187
  private sequence;
192
188
  private readonly pongWaiters;
@@ -214,6 +210,7 @@ export declare class PrismSocket {
214
210
  private greet;
215
211
  private reconnectLater;
216
212
  private failPending;
213
+ private schedule;
217
214
  private receive;
218
215
  private nextId;
219
216
  /**
package/lib/index.js CHANGED
@@ -101,12 +101,6 @@ const NAME_SYNC_MS = 6 * 60 * 60_000;
101
101
  const NAME_SYNC_FIRST_MS = 60_000;
102
102
  /** Names per report; the platform takes at most 500. */
103
103
  const NAMES_PER_REPORT = 200;
104
- /**
105
- * The one connection to PRiSM. Commands go out as `command` frames and the
106
- * platform answers with `say` frames naming them; the shop's news comes as
107
- * `say` frames naming none. While it is down a command fails at once:
108
- * queueing a checkout or a top-up to run later would make players repeat it.
109
- */
110
104
  class PrismSocket {
111
105
  open;
112
106
  url;
@@ -123,6 +117,8 @@ class PrismSocket {
123
117
  retryTimer = null;
124
118
  pingTimer = null;
125
119
  lastHeard = 0;
120
+ scheduleTimer;
121
+ sentDate = new Map();
126
122
  stopped = false;
127
123
  sequence = 0;
128
124
  pongWaiters = [];
@@ -162,6 +158,7 @@ class PrismSocket {
162
158
  }
163
159
  stop() {
164
160
  this.stopped = true;
161
+ clearTimeout(this.scheduleTimer);
165
162
  if (this.retryTimer)
166
163
  clearTimeout(this.retryTimer);
167
164
  if (this.pingTimer)
@@ -195,11 +192,13 @@ class PrismSocket {
195
192
  socket.send("ping");
196
193
  }, PING_MS);
197
194
  });
198
- socket.addEventListener("message", (event) => this.receive(String(event?.data ?? "")));
195
+ socket.addEventListener("message", (event) => { if (this.socket === socket)
196
+ this.receive(String(event?.data ?? "")); });
199
197
  socket.addEventListener("close", () => {
200
198
  if (this.socket !== socket)
201
199
  return;
202
200
  this.socket = null;
201
+ clearTimeout(this.scheduleTimer);
203
202
  if (this.pingTimer)
204
203
  clearInterval(this.pingTimer);
205
204
  this.failPending();
@@ -211,7 +210,8 @@ class PrismSocket {
211
210
  }
212
211
  /** Hello: on connecting, and again when the platform says the bot's settings changed, so it rereads them. */
213
212
  greet() {
214
- this.send({ type: "hello", commands: 1, ...this.hello() });
213
+ clearTimeout(this.scheduleTimer);
214
+ this.send({ type: "hello", commands: 1, schedules: true, ...this.hello() });
215
215
  }
216
216
  reconnectLater() {
217
217
  if (this.stopped)
@@ -231,6 +231,35 @@ class PrismSocket {
231
231
  onSay(null);
232
232
  this.listening.clear();
233
233
  }
234
+ schedule(rules, timeZone) {
235
+ clearTimeout(this.scheduleTimer);
236
+ const active = new Set(rules.map(rule => rule.id));
237
+ for (const id of this.sentDate.keys())
238
+ if (!active.has(id))
239
+ this.sentDate.delete(id);
240
+ if (!rules.length)
241
+ return;
242
+ const clock = new Intl.DateTimeFormat("en-CA", {
243
+ timeZone, year: "numeric", month: "2-digit", day: "2-digit",
244
+ hourCycle: "h23", hour: "2-digit", minute: "2-digit",
245
+ });
246
+ const tick = () => {
247
+ const parts = Object.fromEntries(clock.formatToParts(Date.now()).map(part => [part.type, part.value]));
248
+ const date = `${parts.year}-${parts.month}-${parts.day}`;
249
+ const weekday = new Date(date + "T00:00:00Z").getUTCDay();
250
+ for (const rule of rules) {
251
+ if (rule.time !== `${parts.hour}:${parts.minute}` || (rule.date && rule.date !== date)
252
+ || (rule.weekdays && !rule.weekdays.includes(weekday)) || this.sentDate.get(rule.id) === date)
253
+ continue;
254
+ this.sentDate.set(rule.id, date);
255
+ for (const to of rule.to)
256
+ this.onNews({ type: "say", to, message: [{ text: rule.text }] });
257
+ }
258
+ arm();
259
+ };
260
+ const arm = () => { this.scheduleTimer = setTimeout(tick, 60000 - Date.now() % 60000); };
261
+ arm();
262
+ }
234
263
  receive(raw) {
235
264
  this.lastHeard = Date.now();
236
265
  if (raw === "pong") {
@@ -244,6 +273,10 @@ class PrismSocket {
244
273
  catch {
245
274
  return;
246
275
  }
276
+ if (frame?.type === "schedules" && Array.isArray(frame.rules)) {
277
+ this.schedule(frame.rules, frame.timeZone);
278
+ return;
279
+ }
247
280
  if (frame?.type === "say" && Array.isArray(frame.message)) {
248
281
  const say = frame;
249
282
  if (typeof say.replyTo !== "string") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "PRiSM Next 计费与设备管理系统的 Koishi 机器人集成插件",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {