koishi-plugin-prism 0.7.0 → 0.8.0

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
@@ -34,9 +34,9 @@ Koishi 里只填怎么连到店铺:
34
34
  | :--- | :--- |
35
35
  | 名字 | Bot 的资源名,也是设备页「HA 监听器」里显示的名字。同一家店有多个 Bot 时请分别起名。 |
36
36
  | 启用 | 停用后 Bot 对所有命令只回复「Bot 已被店铺停用」,不再做任何事;重新启用后立即恢复。 |
37
- | 玩家可用的命令 | `全部` 或 `只读`。只读时玩家只能绑定身份(`bind`)、查询(`wallet`、`billing`、`history`、`items`、`list`、`show`、`麻将列表`、`versions`、`api测速`、`ws测速`)、兑换(`redeem`)和延迟关机(`keep`);入场、结账、上下桌和设备操作(`login`/`入场`、`logout`、`mahjong`/`上桌`/`下桌`、`lock`、`on`、`off`、`coin`、`scan`)只有店员能用,包括替其他玩家操作。 |
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`、`addx`、`del`、`overwrite`。 |
39
+ | 店员命令 | 店员可以替其他玩家入场、结账、查询,以及 `add`、`addx`、`del`、`overwrite`、`补货`。 |
40
40
  | 开关机失败的原因 | 只说玩家该知道的:没有这个设备、设备未连接、设备已关机、设备没有响应;其余情况统一回「操作没有完成,请找店员」,内部原因只写日志。 |
41
41
  | 关机仅限店员 | `off` 只允许店员使用(以店员身份越过入场要求);`on` 始终只允许已入场玩家使用。 |
42
42
  | 账单私聊店员 | 玩家退场或店员覆盖结账后,把同一份账单私聊给同一聊天平台上的店员。 |
@@ -104,6 +104,7 @@ Bot 设置开启「账单私聊店员」时,玩家退场或店员覆盖结账
104
104
  * `mahjong <桌号>` / `上桌 [桌号]` - 按桌名或别名(不区分大小写)坐上麻将桌;`/上桌` 未提供桌号时会引导查看 `/麻将列表`。不需要先入场:坐满时平台为全桌开始计费,已开局的桌补位后立即开始计费。直接加入另一桌即为换桌,无需先下桌;相同方案保持原计时,目标满桌或计费失败保留原桌。平台拒绝计费(例如不在麻将计费时段)时回复原因。
105
105
  * `下桌 [桌号]` - 离开当前所在的麻将桌(也可以指明哪一桌),正在计费的会停止计费,并显示该桌剩余人数。
106
106
  * `麻将列表` - 查看店里麻将桌的桌名、别名,以及空闲、等位或游玩中状态。
107
+ * `库存 [小卖铺]` - 查看各台小卖铺每件商品的剩余数量(不限量、已售罄会写明),停用的小卖铺标「已停用」;带名称或别名时只看那一台。
107
108
  * `api测速 [次数]` - 走 HTTP 连续查询自己的钱包:首次(含建立连接)单独列出,之后显示最小、平均与最大延迟,并给出服务端处理时间和经过的 Cloudflare 节点(默认 5 次,最多 10 次)。
108
109
  * `ws测速 [次数]` - 走 Bot 的 WebSocket 连接:心跳往返(纯网络)和同样的钱包查询,并给出服务端处理时间(默认 5 次,最多 10 次)。两者对比可以看出延迟在网络还是在服务端,以及长连接能省下多少。
109
110
  * `versions` - 显示当前 Bot npm 包版本,以及后端是哪次部署(Worker 名称和提交号的前 12 位,如 `prism-link-beta (df66f121da54)`);后端不可达时仍会显示 Bot 版本。
@@ -119,6 +120,7 @@ Bot 设置开启「店员命令」后,店员(在网页绑定了当前聊天
119
120
  * `addx <target:user> <amount:number>` - 为目标用户增加付费余额。
120
121
  * `del <target:user> <amount:number>` - 从目标用户扣除余额。
121
122
  * `overwrite <target:user> <amount:number> [reason:text]` - 覆盖目标用户本次结账金额,并立即执行结账;未填写原因时使用默认管理员调价原因。
123
+ * `补货 [小卖铺] <商品> <数量>` - 给小卖铺的一件商品增加库存,如 `补货 可乐 12`。几台小卖铺有同名商品时要写上小卖铺名或别名,如 `补货 前台 可乐 12`;商品名里可以有空格。只能增加正整数,且只用于已设置库存数量的商品(不限量的先在 PRiSM 商家后台设置);网页上的只读员工不能补货。减少或盘点请在商家后台操作。
122
124
 
123
125
  ## 🛠️ 本地开发与构建
124
126
 
@@ -168,6 +170,16 @@ npm version minor --no-git-tag-version
168
170
 
169
171
  绑定支持 Koishi 提供的各适配器标识,身份值保持字符串,不限定数字格式。店员和账单私聊按「平台标识 + 身份值」认人,相同号码的其他平台不会继承权限或通知。管理员代操作使用目标平台标识,禁止通过当前适配器误操作另一个平台。`/list` 只用当前平台的身份查询昵称,缺失时使用后台名称。源码和 npm 发行包需要分别核对版本。
170
172
 
173
+ ### 0.8.0
174
+
175
+ - 新增 `库存 [小卖铺]` 查看小卖铺库存、`补货 [小卖铺] <商品> <数量>` 给商品补货(店员)。
176
+ - 需要配套支持小卖铺库存的 PRiSM 后端。
177
+
178
+ ### 0.7.1
179
+
180
+ - 定时播报支持一条规则配置多个发送时间;发送失败时 60 秒后只向失败的收件人重试一次。
181
+ - 支持平台播报中的图片消息段,用于店员核对自助礼物的转账截图。
182
+
171
183
  ### 0.7.0
172
184
 
173
185
  - `add <target:user> <free:number> [paid:number]` 支持在免费余额之外传入可选的付费余额;新增 `addx <target:user> <amount:number>` 仅增加付费余额。
@@ -221,10 +233,14 @@ Koishi 配置只剩 `baseUrl` 和 `integrationToken`(`shopCode` 可选、已
221
233
 
222
234
  ### Bot 定时播报
223
235
 
224
- 播报组支持定时发送固定文字,可选每天、每周指定星期或指定日期单次发送,时间使用店铺时区,收件人沿用播报组。每组最多 20 条,每条最多 2000 字。需使用支持 schedules 的新版 Koishi 插件。
236
+ 播报组支持定时发送固定文字,可选每天、每周指定星期或指定日期发送。每条规则可添加多个不同的发送时间,例如每天 09:00、13:00、18:00 各发一次。时间使用店铺时区,收件人沿用播报组。每组最多 20 条,每条最多 2000 字。需使用支持 schedules 的新版 Koishi 插件。
225
237
 
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 或轮询请求。
238
+ 插件 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
239
 
228
- 断线、停用或配置更新会取消旧计时,重连后恢复。离线错过的分钟跳过,夏令时跳过的时间也跳过,重复的当地时间在同一进程内每天最多发一次。发送失败沿用播报错误日志,不自动补发。单个 Bot 凭据应只运行一个插件实例,多实例各自计时可能重复发送。进程重启后仅安排后续分钟。
240
+ 断线、停用或配置更新会取消旧计时,重连后恢复。离线错过的分钟跳过,夏令时跳过的时间也跳过,同一进程按当地日期、时间点及收件人去重。发送成功即完成;首次失败后等待 60 秒,只向失败的收件人重试一次。再次失败会记日志并将本次投递标为完成。断线或停用会取消待重试投递。单个 Bot 凭据应只运行一个插件实例,多实例各自计时可能重复发送。进程重启后仅安排后续分钟。
229
241
 
230
242
  0.6.1 增加由平台下发配置的本地定时播报,支持每天、每周和单次公告。
243
+ ## 自助商店凭证
244
+
245
+ 支持平台 `gift.purchased` 事后核账播报中的图片消息段。店家在平台播报组中将「自助礼物待核账」配置到店员群或私聊,插件会发送礼物订单说明和转账截图。此通知发生在发放之后,不承担支付验证或自动退款。
246
+
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
@@ -55,6 +55,9 @@ const COMMANDS = [
55
55
  ["上桌 [tableId] [target:user]", "加入指定麻将桌"],
56
56
  ["下桌 [tableId] [target:user]", "离开当前所在麻将桌"],
57
57
  ["麻将列表", "查看麻将机状态与别名"],
58
+ ["库存 [shop:text]", "查看小卖铺库存,可指定一台小卖铺"],
59
+ // Names may hold spaces, so the platform reads the whole line.
60
+ ["补货 [input:text]", "给小卖铺商品补货(店员):补货 [小卖铺] 商品 数量"],
58
61
  ["logout [target:user]", "结算玩家计费场次"],
59
62
  ["billing [target:user]", "预览玩家结账费用"],
60
63
  ["wallet [target:user]", "查看玩家钱包"],
@@ -119,6 +122,7 @@ class PrismSocket {
119
122
  pingTimer = null;
120
123
  lastHeard = 0;
121
124
  scheduleTimer;
125
+ pendingDeliveries = new Map();
122
126
  sentDate = new Map();
123
127
  stopped = false;
124
128
  sequence = 0;
@@ -159,7 +163,7 @@ class PrismSocket {
159
163
  }
160
164
  stop() {
161
165
  this.stopped = true;
162
- clearTimeout(this.scheduleTimer);
166
+ this.stopSchedules();
163
167
  if (this.retryTimer)
164
168
  clearTimeout(this.retryTimer);
165
169
  if (this.pingTimer)
@@ -199,7 +203,7 @@ class PrismSocket {
199
203
  if (this.socket !== socket)
200
204
  return;
201
205
  this.socket = null;
202
- clearTimeout(this.scheduleTimer);
206
+ this.stopSchedules();
203
207
  if (this.pingTimer)
204
208
  clearInterval(this.pingTimer);
205
209
  this.failPending();
@@ -232,12 +236,55 @@ class PrismSocket {
232
236
  onSay(null);
233
237
  this.listening.clear();
234
238
  }
239
+ cancelDelivery(key) {
240
+ clearTimeout(this.pendingDeliveries.get(key)?.timer);
241
+ this.pendingDeliveries.delete(key);
242
+ }
243
+ stopSchedules() {
244
+ clearTimeout(this.scheduleTimer);
245
+ for (const key of this.pendingDeliveries.keys())
246
+ this.cancelDelivery(key);
247
+ }
248
+ sendScheduled(rule, to, date, time) {
249
+ const key = JSON.stringify([rule.id, to, time]);
250
+ if (this.sentDate.get(key) === date || this.pendingDeliveries.get(key)?.date === date)
251
+ return;
252
+ const pending = { date };
253
+ this.pendingDeliveries.set(key, pending);
254
+ const attempt = async (retry) => {
255
+ let delivered = false;
256
+ try {
257
+ delivered = await this.onNews({ type: "say", to, message: [{ text: rule.text }] });
258
+ }
259
+ catch (error) {
260
+ this.logger?.warn(`PRiSM announcement to ${to} failed`, error);
261
+ }
262
+ if (this.pendingDeliveries.get(key) !== pending)
263
+ return;
264
+ if (delivered || retry) {
265
+ this.sentDate.set(key, date);
266
+ this.pendingDeliveries.delete(key);
267
+ }
268
+ else {
269
+ pending.timer = setTimeout(() => void attempt(true), 60_000);
270
+ }
271
+ };
272
+ void attempt(false);
273
+ }
235
274
  schedule(rules, timeZone) {
236
275
  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);
276
+ const active = new Map(rules.map(rule => [rule.id, rule]));
277
+ const stillScheduled = (key) => {
278
+ const [id, to, time] = JSON.parse(key);
279
+ const rule = active.get(id);
280
+ return rule?.to.includes(to) && (rule.time === time || rule.additionalTimes?.includes(time));
281
+ };
282
+ for (const key of this.pendingDeliveries.keys())
283
+ if (!stillScheduled(key))
284
+ this.cancelDelivery(key);
285
+ for (const key of this.sentDate.keys())
286
+ if (!stillScheduled(key))
287
+ this.sentDate.delete(key);
241
288
  if (!rules.length)
242
289
  return;
243
290
  const clock = new Intl.DateTimeFormat("en-CA", {
@@ -248,13 +295,13 @@ class PrismSocket {
248
295
  const parts = Object.fromEntries(clock.formatToParts(Date.now()).map(part => [part.type, part.value]));
249
296
  const date = `${parts.year}-${parts.month}-${parts.day}`;
250
297
  const weekday = new Date(date + "T00:00:00Z").getUTCDay();
298
+ const time = `${parts.hour}:${parts.minute}`;
251
299
  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)
300
+ if ((rule.time !== time && !rule.additionalTimes?.includes(time)) || (rule.date && rule.date !== date)
301
+ || (rule.weekdays && !rule.weekdays.includes(weekday)))
254
302
  continue;
255
- this.sentDate.set(rule.id, date);
256
303
  for (const to of rule.to)
257
- this.onNews({ type: "say", to, message: [{ text: rule.text }] });
304
+ this.sendScheduled(rule, to, date, time);
258
305
  }
259
306
  arm();
260
307
  };
@@ -433,9 +480,12 @@ class PrismKoishiService {
433
480
  this.socket = new PrismSocket(open, platformSocketUrl(options.baseUrl), options.integrationToken,
434
481
  // Saying it can watch is what makes the platform consider this bot; the platform decides.
435
482
  () => (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));
483
+ const delivery = this.announcing.then(() => this.announce(say)).catch(error => {
484
+ this.logger?.warn("PRiSM announcement failed", error);
485
+ return false;
486
+ });
487
+ this.announcing = delivery.then(() => { });
488
+ return delivery;
439
489
  }, this.logger, this.watcher
440
490
  ? { onWatch: (watch) => (watch ? this.watcher.watch(watch) : this.watcher.stop()), onClose: () => this.watcher.stop() }
441
491
  : undefined);
@@ -601,16 +651,18 @@ class PrismKoishiService {
601
651
  await bot.sendPrivateMessage(channel.slice(8), text);
602
652
  else
603
653
  await bot.sendMessage(channel, text);
654
+ return true;
604
655
  }
605
656
  catch (error) {
606
657
  this.logger?.warn(`PRiSM announcement to ${target} failed`, error);
658
+ return false;
607
659
  }
608
660
  }
609
661
  /** The shop's news, said where the platform says. */
610
662
  async announce(say) {
611
663
  if (!say.to)
612
- return;
613
- await this.sendTo(say.to, await this.render(say.message));
664
+ return false;
665
+ return this.sendTo(say.to, await this.render(say.message));
614
666
  }
615
667
  /**
616
668
  * The platform's message as this chat says it: each player by the name the
@@ -624,6 +676,8 @@ class PrismKoishiService {
624
676
  parts.push(await this.playerName(player, provider));
625
677
  else if (typeof segment?.text === "string")
626
678
  parts.push(segment.text);
679
+ else if (typeof segment?.image === "string")
680
+ parts.push(koishi_1.h.image(segment.image).toString());
627
681
  }
628
682
  return parts.join("");
629
683
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "PRiSM Next 计费与设备管理系统的 Koishi 机器人集成插件",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {