koishi-plugin-prism 0.7.0 → 0.7.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
@@ -168,6 +168,11 @@ npm version minor --no-git-tag-version
168
168
 
169
169
  绑定支持 Koishi 提供的各适配器标识,身份值保持字符串,不限定数字格式。店员和账单私聊按「平台标识 + 身份值」认人,相同号码的其他平台不会继承权限或通知。管理员代操作使用目标平台标识,禁止通过当前适配器误操作另一个平台。`/list` 只用当前平台的身份查询昵称,缺失时使用后台名称。源码和 npm 发行包需要分别核对版本。
170
170
 
171
+ ### 0.7.1
172
+
173
+ - 定时播报支持一条规则配置多个发送时间;发送失败时 60 秒后只向失败的收件人重试一次。
174
+ - 支持平台播报中的图片消息段,用于店员核对自助礼物的转账截图。
175
+
171
176
  ### 0.7.0
172
177
 
173
178
  - `add <target:user> <free:number> [paid:number]` 支持在免费余额之外传入可选的付费余额;新增 `addx <target:user> <amount:number>` 仅增加付费余额。
@@ -221,10 +226,14 @@ Koishi 配置只剩 `baseUrl` 和 `integrationToken`(`shopCode` 可选、已
221
226
 
222
227
  ### Bot 定时播报
223
228
 
224
- 播报组支持定时发送固定文字,可选每天、每周指定星期或指定日期单次发送,时间使用店铺时区,收件人沿用播报组。每组最多 20 条,每条最多 2000 字。需使用支持 schedules 的新版 Koishi 插件。
229
+ 播报组支持定时发送固定文字,可选每天、每周指定星期或指定日期发送。每条规则可添加多个不同的发送时间,例如每天 09:00、13:00、18:00 各发一次。时间使用店铺时区,收件人沿用播报组。每组最多 20 条,每条最多 2000 字。需使用支持 schedules 的新版 Koishi 插件。
225
230
 
226
- 插件 hello 声明 schedules: true,平台返回 schedules 帧(timeZone、rules)。rules 为启用且有目的地的规则,包含 id、time(HH:mm)、text、to,可选 date(YYYY-MM-DD)或 weekdays(0 为周日)。平台在重新 hello 时下发全量配置,配置变化通过 config.changed 让插件重新 hello。旧插件继续工作。定时播报由插件本地按分钟检查,无需增加服务端 alarm 或轮询请求。
231
+ 插件 hello 声明 schedules: true,平台返回 schedules 帧(timeZone、rules)。rules 为启用且有目的地的规则,包含 id、首个时间 time(HH:mm)、text、to,其余时间在可选数组 additionalTimes 中。0.6.1 插件只读取首个时间,多时点和失败重试需配套更新插件。规则另可指定 date(YYYY-MM-DD)或 weekdays(0 为周日)。平台在重新 hello 时下发全量配置,配置变化通过 config.changed 让插件重新 hello。旧插件继续工作。定时播报由插件本地按分钟检查,无需增加服务端 alarm 或轮询请求。
227
232
 
228
- 断线、停用或配置更新会取消旧计时,重连后恢复。离线错过的分钟跳过,夏令时跳过的时间也跳过,重复的当地时间在同一进程内每天最多发一次。发送失败沿用播报错误日志,不自动补发。单个 Bot 凭据应只运行一个插件实例,多实例各自计时可能重复发送。进程重启后仅安排后续分钟。
233
+ 断线、停用或配置更新会取消旧计时,重连后恢复。离线错过的分钟跳过,夏令时跳过的时间也跳过,同一进程按当地日期、时间点及收件人去重。发送成功即完成;首次失败后等待 60 秒,只向失败的收件人重试一次。再次失败会记日志并将本次投递标为完成。断线或停用会取消待重试投递。单个 Bot 凭据应只运行一个插件实例,多实例各自计时可能重复发送。进程重启后仅安排后续分钟。
229
234
 
230
235
  0.6.1 增加由平台下发配置的本地定时播报,支持每天、每周和单次公告。
236
+ ## 自助商店凭证
237
+
238
+ 支持平台 `gift.purchased` 事后核账播报中的图片消息段。店家在平台播报组中将「自助礼物待核账」配置到店员群或私聊,插件会发送礼物订单说明和转账截图。此通知发生在发放之后,不承担支付验证或自动退款。
239
+
package/lib/index.d.ts CHANGED
@@ -132,6 +132,7 @@ type GuildMember = {
132
132
  type BotMessage = Array<{
133
133
  text?: string;
134
134
  player?: MessagePlayer;
135
+ image?: string;
135
136
  }>;
136
137
  /** `say`: something to say, the answer to a command (`replyTo`) or the shop's news. */
137
138
  export type SayFrame = {
@@ -182,6 +183,7 @@ export declare class PrismSocket {
182
183
  private pingTimer;
183
184
  private lastHeard;
184
185
  private scheduleTimer?;
186
+ private readonly pendingDeliveries;
185
187
  private readonly sentDate;
186
188
  private stopped;
187
189
  private sequence;
@@ -190,7 +192,7 @@ export declare class PrismSocket {
190
192
  /** What to say in hello besides that this bot sends commands. */
191
193
  hello: () => Record<string, unknown>,
192
194
  /** The shop's news: something to say that answers no command. */
193
- onNews: (say: SayFrame) => void, logger?: {
195
+ onNews: (say: SayFrame) => boolean | Promise<boolean>, logger?: {
194
196
  warn(...args: unknown[]): void;
195
197
  } | undefined,
196
198
  /**
@@ -210,6 +212,9 @@ export declare class PrismSocket {
210
212
  private greet;
211
213
  private reconnectLater;
212
214
  private failPending;
215
+ private cancelDelivery;
216
+ private stopSchedules;
217
+ private sendScheduled;
213
218
  private schedule;
214
219
  private receive;
215
220
  private nextId;
package/lib/index.js CHANGED
@@ -119,6 +119,7 @@ class PrismSocket {
119
119
  pingTimer = null;
120
120
  lastHeard = 0;
121
121
  scheduleTimer;
122
+ pendingDeliveries = new Map();
122
123
  sentDate = new Map();
123
124
  stopped = false;
124
125
  sequence = 0;
@@ -159,7 +160,7 @@ class PrismSocket {
159
160
  }
160
161
  stop() {
161
162
  this.stopped = true;
162
- clearTimeout(this.scheduleTimer);
163
+ this.stopSchedules();
163
164
  if (this.retryTimer)
164
165
  clearTimeout(this.retryTimer);
165
166
  if (this.pingTimer)
@@ -199,7 +200,7 @@ class PrismSocket {
199
200
  if (this.socket !== socket)
200
201
  return;
201
202
  this.socket = null;
202
- clearTimeout(this.scheduleTimer);
203
+ this.stopSchedules();
203
204
  if (this.pingTimer)
204
205
  clearInterval(this.pingTimer);
205
206
  this.failPending();
@@ -232,12 +233,55 @@ class PrismSocket {
232
233
  onSay(null);
233
234
  this.listening.clear();
234
235
  }
236
+ cancelDelivery(key) {
237
+ clearTimeout(this.pendingDeliveries.get(key)?.timer);
238
+ this.pendingDeliveries.delete(key);
239
+ }
240
+ stopSchedules() {
241
+ clearTimeout(this.scheduleTimer);
242
+ for (const key of this.pendingDeliveries.keys())
243
+ this.cancelDelivery(key);
244
+ }
245
+ sendScheduled(rule, to, date, time) {
246
+ const key = JSON.stringify([rule.id, to, time]);
247
+ if (this.sentDate.get(key) === date || this.pendingDeliveries.get(key)?.date === date)
248
+ return;
249
+ const pending = { date };
250
+ this.pendingDeliveries.set(key, pending);
251
+ const attempt = async (retry) => {
252
+ let delivered = false;
253
+ try {
254
+ delivered = await this.onNews({ type: "say", to, message: [{ text: rule.text }] });
255
+ }
256
+ catch (error) {
257
+ this.logger?.warn(`PRiSM announcement to ${to} failed`, error);
258
+ }
259
+ if (this.pendingDeliveries.get(key) !== pending)
260
+ return;
261
+ if (delivered || retry) {
262
+ this.sentDate.set(key, date);
263
+ this.pendingDeliveries.delete(key);
264
+ }
265
+ else {
266
+ pending.timer = setTimeout(() => void attempt(true), 60_000);
267
+ }
268
+ };
269
+ void attempt(false);
270
+ }
235
271
  schedule(rules, timeZone) {
236
272
  clearTimeout(this.scheduleTimer);
237
- const active = new Set(rules.map(rule => rule.id));
238
- for (const id of this.sentDate.keys())
239
- if (!active.has(id))
240
- this.sentDate.delete(id);
273
+ const active = new Map(rules.map(rule => [rule.id, rule]));
274
+ const stillScheduled = (key) => {
275
+ const [id, to, time] = JSON.parse(key);
276
+ const rule = active.get(id);
277
+ return rule?.to.includes(to) && (rule.time === time || rule.additionalTimes?.includes(time));
278
+ };
279
+ for (const key of this.pendingDeliveries.keys())
280
+ if (!stillScheduled(key))
281
+ this.cancelDelivery(key);
282
+ for (const key of this.sentDate.keys())
283
+ if (!stillScheduled(key))
284
+ this.sentDate.delete(key);
241
285
  if (!rules.length)
242
286
  return;
243
287
  const clock = new Intl.DateTimeFormat("en-CA", {
@@ -248,13 +292,13 @@ class PrismSocket {
248
292
  const parts = Object.fromEntries(clock.formatToParts(Date.now()).map(part => [part.type, part.value]));
249
293
  const date = `${parts.year}-${parts.month}-${parts.day}`;
250
294
  const weekday = new Date(date + "T00:00:00Z").getUTCDay();
295
+ const time = `${parts.hour}:${parts.minute}`;
251
296
  for (const rule of rules) {
252
- if (rule.time !== `${parts.hour}:${parts.minute}` || (rule.date && rule.date !== date)
253
- || (rule.weekdays && !rule.weekdays.includes(weekday)) || this.sentDate.get(rule.id) === date)
297
+ if ((rule.time !== time && !rule.additionalTimes?.includes(time)) || (rule.date && rule.date !== date)
298
+ || (rule.weekdays && !rule.weekdays.includes(weekday)))
254
299
  continue;
255
- this.sentDate.set(rule.id, date);
256
300
  for (const to of rule.to)
257
- this.onNews({ type: "say", to, message: [{ text: rule.text }] });
301
+ this.sendScheduled(rule, to, date, time);
258
302
  }
259
303
  arm();
260
304
  };
@@ -433,9 +477,12 @@ class PrismKoishiService {
433
477
  this.socket = new PrismSocket(open, platformSocketUrl(options.baseUrl), options.integrationToken,
434
478
  // Saying it can watch is what makes the platform consider this bot; the platform decides.
435
479
  () => (this.watcher ? { ha: true } : {}), (say) => {
436
- this.announcing = this.announcing
437
- .then(() => this.announce(say))
438
- .catch((error) => this.logger?.warn("PRiSM announcement failed", error));
480
+ const delivery = this.announcing.then(() => this.announce(say)).catch(error => {
481
+ this.logger?.warn("PRiSM announcement failed", error);
482
+ return false;
483
+ });
484
+ this.announcing = delivery.then(() => { });
485
+ return delivery;
439
486
  }, this.logger, this.watcher
440
487
  ? { onWatch: (watch) => (watch ? this.watcher.watch(watch) : this.watcher.stop()), onClose: () => this.watcher.stop() }
441
488
  : undefined);
@@ -601,16 +648,18 @@ class PrismKoishiService {
601
648
  await bot.sendPrivateMessage(channel.slice(8), text);
602
649
  else
603
650
  await bot.sendMessage(channel, text);
651
+ return true;
604
652
  }
605
653
  catch (error) {
606
654
  this.logger?.warn(`PRiSM announcement to ${target} failed`, error);
655
+ return false;
607
656
  }
608
657
  }
609
658
  /** The shop's news, said where the platform says. */
610
659
  async announce(say) {
611
660
  if (!say.to)
612
- return;
613
- await this.sendTo(say.to, await this.render(say.message));
661
+ return false;
662
+ return this.sendTo(say.to, await this.render(say.message));
614
663
  }
615
664
  /**
616
665
  * The platform's message as this chat says it: each player by the name the
@@ -624,6 +673,8 @@ class PrismKoishiService {
624
673
  parts.push(await this.playerName(player, provider));
625
674
  else if (typeof segment?.text === "string")
626
675
  parts.push(segment.text);
676
+ else if (typeof segment?.image === "string")
677
+ parts.push(koishi_1.h.image(segment.image).toString());
627
678
  }
628
679
  return parts.join("");
629
680
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "PRiSM Next 计费与设备管理系统的 Koishi 机器人集成插件",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {