koishi-plugin-prism 0.6.1 → 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
@@ -36,7 +36,7 @@ Koishi 里只填怎么连到店铺:
36
36
  | 启用 | 停用后 Bot 对所有命令只回复「Bot 已被店铺停用」,不再做任何事;重新启用后立即恢复。 |
37
37
  | 玩家可用的命令 | `全部` 或 `只读`。只读时玩家只能绑定身份(`bind`)、查询(`wallet`、`billing`、`history`、`items`、`list`、`show`、`麻将列表`、`versions`、`api测速`、`ws测速`)、兑换(`redeem`)和延迟关机(`keep`);入场、结账、上下桌和设备操作(`login`/`入场`、`logout`、`mahjong`/`上桌`/`下桌`、`lock`、`on`、`off`、`coin`、`scan`)只有店员能用,包括替其他玩家操作。 |
38
38
  | 只读提示 | 只读模式下玩家使用受限命令时 Bot 的回复。 |
39
- | 店员命令 | 店员可以替其他玩家入场、结账、查询,以及 `add`、`del`、`overwrite`。 |
39
+ | 店员命令 | 店员可以替其他玩家入场、结账、查询,以及 `add`、`addx`、`del`、`overwrite`。 |
40
40
  | 开关机失败的原因 | 只说玩家该知道的:没有这个设备、设备未连接、设备已关机、设备没有响应;其余情况统一回「操作没有完成,请找店员」,内部原因只写日志。 |
41
41
  | 关机仅限店员 | `off` 只允许店员使用(以店员身份越过入场要求);`on` 始终只允许已入场玩家使用。 |
42
42
  | 账单私聊店员 | 玩家退场或店员覆盖结账后,把同一份账单私聊给同一聊天平台上的店员。 |
@@ -86,7 +86,7 @@ Bot 一直在线,所以平台会优先让 Bot 当店铺的 Home Assistant **
86
86
  Bot 设置开启「账单私聊店员」时,玩家退场或店员覆盖结账成功后,机器人会把同一份账单私聊给同一聊天平台上的店员,账单会明确显示结账玩家身份。
87
87
  同一玩家在前一次退场结账尚未完成时重复发送 `/logout` 或 `/退场`,机器人会复用同一次结账请求与账单,避免重复扣款。
88
88
  账单和管理员代操作回执均使用“平台昵称(平台标识:身份值)”称呼玩家;平台暂时无法提供昵称时显示“未知昵称(平台标识:身份值)”,不会显示内部玩家 ID。
89
- 管理员 `/add` 增加免费余额;`/del` 按结账相同的顺序从可用免费余额、再从付费余额扣除,余额不足时会返回余额不足提示。
89
+ 管理员 `/add <玩家> <免费金额> [付费金额]` 增加免费余额及可选付费余额;`/addx <玩家> <付费金额>` 增加付费余额;`/del` 按结账相同的顺序从可用免费余额、再从付费余额扣除,余额不足时会返回余额不足提示。
90
90
  * `billing` - 预览当前玩家本场计费的消费费用。
91
91
  * `wallet` - 查看当前玩家的钱包余额。
92
92
  * `items` - 查看当前玩家持有的道具或资产。
@@ -115,7 +115,8 @@ Bot 版本直接读取本 npm 包的 `package.json`,后端的部署读取无
115
115
  支持 `/上桌 a @玩家`、`/下桌 a @玩家`(`mahjong a @玩家` 同上桌),可直接代玩家上下桌或换桌,无需玩家先入场。与其他代操作相同,须开启店员命令且发送者是本店绑定了当前平台身份的店员。回复显示目标玩家。普通玩家省略 `@玩家` 操作自己。计费与座位由平台原子提交,Bot 不维护另一份桌位或计费状态。
116
116
 
117
117
  Bot 设置开启「店员命令」后,店员(在网页绑定了当前聊天平台身份的店员)可使用:
118
- * `add <target:user> <amount:number>` - 为目标用户增加余额。
118
+ * `add <target:user> <free:number> [paid:number]` - 为目标用户增加免费余额,以及可选的付费余额。
119
+ * `addx <target:user> <amount:number>` - 为目标用户增加付费余额。
119
120
  * `del <target:user> <amount:number>` - 从目标用户扣除余额。
120
121
  * `overwrite <target:user> <amount:number> [reason:text]` - 覆盖目标用户本次结账金额,并立即执行结账;未填写原因时使用默认管理员调价原因。
121
122
 
@@ -167,6 +168,16 @@ npm version minor --no-git-tag-version
167
168
 
168
169
  绑定支持 Koishi 提供的各适配器标识,身份值保持字符串,不限定数字格式。店员和账单私聊按「平台标识 + 身份值」认人,相同号码的其他平台不会继承权限或通知。管理员代操作使用目标平台标识,禁止通过当前适配器误操作另一个平台。`/list` 只用当前平台的身份查询昵称,缺失时使用后台名称。源码和 npm 发行包需要分别核对版本。
169
170
 
171
+ ### 0.7.1
172
+
173
+ - 定时播报支持一条规则配置多个发送时间;发送失败时 60 秒后只向失败的收件人重试一次。
174
+ - 支持平台播报中的图片消息段,用于店员核对自助礼物的转账截图。
175
+
176
+ ### 0.7.0
177
+
178
+ - `add <target:user> <free:number> [paid:number]` 支持在免费余额之外传入可选的付费余额;新增 `addx <target:user> <amount:number>` 仅增加付费余额。
179
+ - 需要配套支持新 `add` 参数与 `addx` 命令的 PRiSM 后端。
180
+
170
181
  ### 0.6.0
171
182
 
172
183
  - 玩家的名字按平台分开存:说到一个玩家时用他在本平台的名字(平台在每个身份上保存的称呼),一个人绑了两个平台也不会被来回改名。
@@ -215,10 +226,14 @@ Koishi 配置只剩 `baseUrl` 和 `integrationToken`(`shopCode` 可选、已
215
226
 
216
227
  ### Bot 定时播报
217
228
 
218
- 播报组支持定时发送固定文字,可选每天、每周指定星期或指定日期单次发送,时间使用店铺时区,收件人沿用播报组。每组最多 20 条,每条最多 2000 字。需使用支持 schedules 的新版 Koishi 插件。
229
+ 播报组支持定时发送固定文字,可选每天、每周指定星期或指定日期发送。每条规则可添加多个不同的发送时间,例如每天 09:00、13:00、18:00 各发一次。时间使用店铺时区,收件人沿用播报组。每组最多 20 条,每条最多 2000 字。需使用支持 schedules 的新版 Koishi 插件。
219
230
 
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 或轮询请求。
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 或轮询请求。
221
232
 
222
- 断线、停用或配置更新会取消旧计时,重连后恢复。离线错过的分钟跳过,夏令时跳过的时间也跳过,重复的当地时间在同一进程内每天最多发一次。发送失败沿用播报错误日志,不自动补发。单个 Bot 凭据应只运行一个插件实例,多实例各自计时可能重复发送。进程重启后仅安排后续分钟。
233
+ 断线、停用或配置更新会取消旧计时,重连后恢复。离线错过的分钟跳过,夏令时跳过的时间也跳过,同一进程按当地日期、时间点及收件人去重。发送成功即完成;首次失败后等待 60 秒,只向失败的收件人重试一次。再次失败会记日志并将本次投递标为完成。断线或停用会取消待重试投递。单个 Bot 凭据应只运行一个插件实例,多实例各自计时可能重复发送。进程重启后仅安排后续分钟。
223
234
 
224
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
@@ -69,7 +69,8 @@ const COMMANDS = [
69
69
  ["coin <deviceRef> [count]", "请求向指定设备投币"],
70
70
  ["scan <deviceRef>", "使用当前玩家绑定的 Aime 卡刷卡"],
71
71
  ["redeem <code>", "兑换 PRiSM 礼物码"],
72
- ["add <target:user> <amount:number>", "增加玩家余额"],
72
+ ["add <target:user> <free:number> [paid:number]", "增加玩家余额"],
73
+ ["addx <target:user> <amount:number>", "增加玩家付费余额"],
73
74
  ["del <target:user> <amount:number>", "扣除玩家余额"],
74
75
  ["overwrite <target:user> <amount:number> [reason:text]", "覆盖结账金额并立即结账"],
75
76
  ];
@@ -118,6 +119,7 @@ class PrismSocket {
118
119
  pingTimer = null;
119
120
  lastHeard = 0;
120
121
  scheduleTimer;
122
+ pendingDeliveries = new Map();
121
123
  sentDate = new Map();
122
124
  stopped = false;
123
125
  sequence = 0;
@@ -158,7 +160,7 @@ class PrismSocket {
158
160
  }
159
161
  stop() {
160
162
  this.stopped = true;
161
- clearTimeout(this.scheduleTimer);
163
+ this.stopSchedules();
162
164
  if (this.retryTimer)
163
165
  clearTimeout(this.retryTimer);
164
166
  if (this.pingTimer)
@@ -198,7 +200,7 @@ class PrismSocket {
198
200
  if (this.socket !== socket)
199
201
  return;
200
202
  this.socket = null;
201
- clearTimeout(this.scheduleTimer);
203
+ this.stopSchedules();
202
204
  if (this.pingTimer)
203
205
  clearInterval(this.pingTimer);
204
206
  this.failPending();
@@ -231,12 +233,55 @@ class PrismSocket {
231
233
  onSay(null);
232
234
  this.listening.clear();
233
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
+ }
234
271
  schedule(rules, timeZone) {
235
272
  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);
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);
240
285
  if (!rules.length)
241
286
  return;
242
287
  const clock = new Intl.DateTimeFormat("en-CA", {
@@ -247,13 +292,13 @@ class PrismSocket {
247
292
  const parts = Object.fromEntries(clock.formatToParts(Date.now()).map(part => [part.type, part.value]));
248
293
  const date = `${parts.year}-${parts.month}-${parts.day}`;
249
294
  const weekday = new Date(date + "T00:00:00Z").getUTCDay();
295
+ const time = `${parts.hour}:${parts.minute}`;
250
296
  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)
297
+ if ((rule.time !== time && !rule.additionalTimes?.includes(time)) || (rule.date && rule.date !== date)
298
+ || (rule.weekdays && !rule.weekdays.includes(weekday)))
253
299
  continue;
254
- this.sentDate.set(rule.id, date);
255
300
  for (const to of rule.to)
256
- this.onNews({ type: "say", to, message: [{ text: rule.text }] });
301
+ this.sendScheduled(rule, to, date, time);
257
302
  }
258
303
  arm();
259
304
  };
@@ -432,9 +477,12 @@ class PrismKoishiService {
432
477
  this.socket = new PrismSocket(open, platformSocketUrl(options.baseUrl), options.integrationToken,
433
478
  // Saying it can watch is what makes the platform consider this bot; the platform decides.
434
479
  () => (this.watcher ? { ha: true } : {}), (say) => {
435
- this.announcing = this.announcing
436
- .then(() => this.announce(say))
437
- .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;
438
486
  }, this.logger, this.watcher
439
487
  ? { onWatch: (watch) => (watch ? this.watcher.watch(watch) : this.watcher.stop()), onClose: () => this.watcher.stop() }
440
488
  : undefined);
@@ -600,16 +648,18 @@ class PrismKoishiService {
600
648
  await bot.sendPrivateMessage(channel.slice(8), text);
601
649
  else
602
650
  await bot.sendMessage(channel, text);
651
+ return true;
603
652
  }
604
653
  catch (error) {
605
654
  this.logger?.warn(`PRiSM announcement to ${target} failed`, error);
655
+ return false;
606
656
  }
607
657
  }
608
658
  /** The shop's news, said where the platform says. */
609
659
  async announce(say) {
610
660
  if (!say.to)
611
- return;
612
- await this.sendTo(say.to, await this.render(say.message));
661
+ return false;
662
+ return this.sendTo(say.to, await this.render(say.message));
613
663
  }
614
664
  /**
615
665
  * The platform's message as this chat says it: each player by the name the
@@ -623,6 +673,8 @@ class PrismKoishiService {
623
673
  parts.push(await this.playerName(player, provider));
624
674
  else if (typeof segment?.text === "string")
625
675
  parts.push(segment.text);
676
+ else if (typeof segment?.image === "string")
677
+ parts.push(koishi_1.h.image(segment.image).toString());
626
678
  }
627
679
  return parts.join("");
628
680
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism",
3
- "version": "0.6.1",
3
+ "version": "0.7.1",
4
4
  "description": "PRiSM Next 计费与设备管理系统的 Koishi 机器人集成插件",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {