koishi-plugin-prism 0.1.37 → 0.1.38

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.
Files changed (3) hide show
  1. package/README.md +4 -4
  2. package/lib/index.js +106 -41
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -33,7 +33,7 @@
33
33
  | `resolveDisplayName` | `function` | - | 可选。自定义用于获取群内昵称作为玩家注册名的异步逻辑。 |
34
34
  | `enableStaffCommands` | `boolean` | `false` | 是否开启管理员快捷指令。 |
35
35
  | `staffUserIds` | `string[]` | `[]` | 允许执行管理员快捷指令的平台用户 ID(如 QQ 号)白名单。空列表不授予目标用户操作权限。 |
36
- | `powerCommandsAdminOnly` | `boolean` | `false` | 开关机是否仅允许 `staffUserIds` 中的管理员使用。关闭时,`/on` 和 `/off` 仍只允许已入场玩家使用。 |
36
+ | `powerCommandsAdminOnly` | `boolean` | `false` | 关机是否仅允许 `staffUserIds` 中的管理员使用。`/on` 始终只允许已入场玩家使用。 |
37
37
  | `logoutNotifyUserIds` | `string[]` | `[]` | 结账成功后额外私聊完整账单的平台用户 ID。通知收件人为该列表与 `staffUserIds` 的去重并集。 |
38
38
  | `mahjongTableConfigs` | `object[]` | `[]` | 推荐的结构化麻将桌列表。每项填写显示名称、命令别名列表与计费方案 ID 列表。显示名称同时作为内部桌位锚点与 session 标签。 |
39
39
 
@@ -71,11 +71,11 @@ pricingConfigIds: [pricing-mahjong-a]
71
71
  * `show [deviceId]` - 查看设备电源与连接状态。
72
72
  设备状态按后端的普通字符串契约显示;插件仍兼容历史数据库中的 JSON 字符串状态,避免旧记录显示为 `unknown`。
73
73
  * `history` - 查看自己的历史游玩记录。
74
- * `lock` - 发送开门指令。
74
+ * `lock` - 为默认 TTLock 门锁生成一个 8 位随机临时密码(有效期 3 分钟)并回复密码。
75
75
  * `on <deviceRef>` - 使用后台设备名称、别名或 `all` 请求启动电源;仅已通过 `login`/`入场` 开启活跃计费 session 的玩家可用。不接受 Home Assistant entity ID,成功回复使用后端返回的设备名称。
76
76
  * `off <deviceRef>` - 使用后台设备名称、别名或 `all` 请求关闭电源;与 `/on` 一样仅允许已入场玩家使用,不接受 Home Assistant entity ID,`all` 显示为“所有设备”。
77
- * `coin <deviceId> [count]` - 请求向指定设备投币指定枚数。
78
- * `scan <deviceId> <subject>` - 请求向设备发送模拟刷卡。
77
+ * `coin <设备名或别名> [count]` - 请求向后台配置的 Hinata IO 设备投币;需要玩家已入场。
78
+ * `scan <设备名或别名>` - 使用当前玩家后台绑定的 Aime 卡向设备刷卡,不在群聊中输入或显示卡号。
79
79
  * `redeem <code>` - 兑换礼物码。
80
80
  * `mahjong <tableId>` / `上桌 [tableId]` - 加入指定麻将桌;`/上桌` 未提供桌号时会引导查看 `/麻将列表`。仅允许已通过 `login`/`入场` 开启默认入场会话的玩家使用。支持在桌位未满但已开局时进行中途补位(即直接开始计费上桌)。
81
81
  * `下桌` - 自动离开当前所在麻将桌,下桌后会停止计费,并显示该麻将桌的剩余游玩人数。
package/lib/index.js CHANGED
@@ -24,7 +24,7 @@ exports.Config = koishi_1.Schema.object({
24
24
  loginSessionLabel: koishi_1.Schema.string().default("音游区间").description("默认入场场次标签 (防重复入场)"),
25
25
  enableStaffCommands: koishi_1.Schema.boolean().default(false).description("是否启用管理员指令"),
26
26
  staffUserIds: koishi_1.Schema.array(koishi_1.Schema.string()).default([]).description("允许执行管理员指令的平台用户ID列表"),
27
- powerCommandsAdminOnly: koishi_1.Schema.boolean().default(false).description("开关机是否仅允许管理员使用;关闭时仅已入场玩家可用"),
27
+ powerCommandsAdminOnly: koishi_1.Schema.boolean().default(false).description("关机是否仅允许管理员使用;开机始终仅已入场玩家可用"),
28
28
  logoutNotifyUserIds: koishi_1.Schema.array(koishi_1.Schema.string()).default([]).description("结账账单私聊通知的平台用户ID列表"),
29
29
  powerOffInterval: koishi_1.Schema.number().default(0).description("无人自动关机等待秒数 (0为禁用)"),
30
30
  mahjongTableConfigs: koishi_1.Schema.array(koishi_1.Schema.object({
@@ -60,8 +60,8 @@ const USAGE = {
60
60
  api_benchmark: "/api测速 [次数]",
61
61
  prism_on: "/prism on <设备名|别名|all>",
62
62
  prism_off: "/prism off <设备名|别名|all>",
63
- prism_coin: "/prism coin <设备ID> [数量]",
64
- prism_scan: "/prism scan <设备ID> <卡号>",
63
+ prism_coin: "/prism coin <设备名或别名> [数量]",
64
+ prism_scan: "/prism scan <设备名或别名>",
65
65
  prism_redeem: "/prism redeem <兑换码>",
66
66
  list: "/list",
67
67
  show: "/show [设备ID]",
@@ -97,8 +97,8 @@ function applyPrismKoishiPlugin(ctx, config) {
97
97
  ctx.command("lock", "向默认门锁设备发送开门指令").action(wrap(async (context) => service.lock(await service.sender(context))));
98
98
  ctx.command("on <deviceRef>", "请求启动指定设备电源").action(wrap(async (context, deviceRef) => service.powerOn(await service.sender(context), deviceRef)));
99
99
  ctx.command("off <deviceRef>", "请求关闭指定设备电源").action(wrap(async (context, deviceRef) => service.powerOff(await service.sender(context), deviceRef)));
100
- ctx.command("coin <deviceId> [count]", "请求向指定设备投币").action(wrap(async (context, deviceId, count) => service.coin(await service.sender(context), deviceId, count)));
101
- ctx.command("scan <deviceId> <subject>", "请求指定设备模拟刷卡").action(wrap(async (context, deviceId, subject) => service.scan(await service.sender(context), deviceId, subject)));
100
+ ctx.command("coin <deviceRef> [count]", "请求向指定设备投币").action(wrap(async (context, deviceRef, count) => service.coin(await service.sender(context), deviceRef, count)));
101
+ ctx.command("scan <deviceRef>", "使用当前玩家绑定的 Aime 卡刷卡").action(wrap(async (context, deviceRef) => service.scan(await service.sender(context), deviceRef)));
102
102
  ctx.command("redeem <code>", "兑换 PRiSM 礼物码").action(wrap(async (context, code) => service.redeem(await service.sender(context), code)));
103
103
  ctx.command("add <target:user> <amount:number>", "增加玩家余额").action(wrap(async (context, target, amount) => service.adjustTargetBalance(await service.sender(context), target, amount, 1, context.session?.bot)));
104
104
  ctx.command("del <target:user> <amount:number>", "扣除玩家余额").action(wrap(async (context, target, amount) => service.adjustTargetBalance(await service.sender(context), target, amount, -1, context.session?.bot)));
@@ -267,13 +267,12 @@ class PrismApiClient {
267
267
  ...this.identityBody(identity),
268
268
  target: {
269
269
  kind: "game_machine",
270
- id: scan.deviceId,
270
+ ref: scan.deviceRef,
271
271
  },
272
272
  action: {
273
273
  type: "aime.scan",
274
274
  payload: {
275
275
  provider: scan.provider,
276
- subject: scan.subject,
277
276
  },
278
277
  },
279
278
  },
@@ -646,11 +645,28 @@ class PrismKoishiService {
646
645
  return formatHistory(result.sessions, this.config.currencyName);
647
646
  }
648
647
  async lock(sender) {
649
- await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
650
- type: "door.open",
651
- target: { kind: "facility", ref: this.config.defaultDoorDeviceId },
652
- });
653
- return "🔑 门锁指令已发送";
648
+ try {
649
+ const result = await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
650
+ type: "door.open",
651
+ target: { kind: "facility", ref: this.config.defaultDoorDeviceId },
652
+ });
653
+ const failure = this.getCommandFailureMessage(result);
654
+ if (failure) {
655
+ this.logDeviceCommandFailure(result);
656
+ return `🔑 开门失败,${deviceCommandFailureReason(result)}`;
657
+ }
658
+ if (result?.action?.executorKind && result.action.executorKind !== "ttlock") {
659
+ return "🔑 门锁指令已发送";
660
+ }
661
+ const password = cleanText(result?.action?.payload?.temporaryPassword);
662
+ if (!password)
663
+ return "🔑 临时门锁密码生成失败,请稍后再试";
664
+ return `🔑 临时门锁密码:${password}\n有效期:3 分钟`;
665
+ }
666
+ catch (error) {
667
+ this.logCommandError(error);
668
+ return `🔑 开门失败,${deviceActionFailureReason(error)}`;
669
+ }
654
670
  }
655
671
  async powerOn(sender, rawDeviceRef) {
656
672
  const deviceRef = cleanText(rawDeviceRef);
@@ -664,37 +680,53 @@ class PrismKoishiService {
664
680
  return commandUsage("prism_off");
665
681
  return this.power(sender, deviceRef, "off");
666
682
  }
667
- async coin(sender, rawDeviceId, rawCount) {
668
- const deviceId = cleanText(rawDeviceId);
669
- if (!deviceId)
683
+ async coin(sender, rawDeviceRef, rawCount) {
684
+ const deviceRef = cleanText(rawDeviceRef);
685
+ if (!deviceRef)
670
686
  return commandUsage("prism_coin");
671
687
  const { value, error } = parsePositiveInt(rawCount, "prism_coin", "数量", 1);
672
688
  if (error)
673
689
  return error;
674
- const result = await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
675
- type: "coin",
676
- target: { kind: "game_machine", id: deviceId },
677
- payload: { count: value },
678
- });
679
- const failure = this.getCommandFailureMessage(result);
680
- if (failure)
681
- return `❌ 执行失败:${failure}`;
682
- return `🪙 已为 ${deviceId} 投入 ${value} 个币`;
683
- }
684
- async scan(sender, rawDeviceId, rawSubject) {
685
- const deviceId = cleanText(rawDeviceId);
686
- const subject = cleanText(rawSubject);
687
- if (!deviceId || !subject)
690
+ try {
691
+ const result = await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
692
+ type: "coin",
693
+ target: { kind: "game_machine", ref: deviceRef },
694
+ payload: { count: value },
695
+ });
696
+ const failure = deviceCommandFailureReason(result);
697
+ if (failure) {
698
+ this.logDeviceCommandFailure(result);
699
+ return `投币失败,${failure}`;
700
+ }
701
+ const label = deviceCommandLabel(result) || deviceRef;
702
+ return `🪙 ${label} 投币成功(${value} 币)`;
703
+ }
704
+ catch (error) {
705
+ this.logCommandError(error);
706
+ return `投币失败,${deviceActionFailureReason(error)}`;
707
+ }
708
+ }
709
+ async scan(sender, rawDeviceRef) {
710
+ const deviceRef = cleanText(rawDeviceRef);
711
+ if (!deviceRef)
688
712
  return commandUsage("prism_scan");
689
- const result = await this.client.requestScanByIdentity(this.identity(sender), {
690
- deviceId,
691
- provider: this.config.defaultScanProvider || "aime",
692
- subject,
693
- });
694
- const failure = this.getCommandFailureMessage(result);
695
- if (failure)
696
- return `❌ 执行失败:${failure}`;
697
- return `💳 使用尾号为 ${subject.slice(-4)} 的卡刷卡成功`;
713
+ try {
714
+ const result = await this.client.requestScanByIdentity(this.identity(sender), {
715
+ deviceRef,
716
+ provider: this.config.defaultScanProvider || "aime",
717
+ });
718
+ const failure = deviceCommandFailureReason(result);
719
+ if (failure) {
720
+ this.logDeviceCommandFailure(result);
721
+ return `刷卡失败,${failure}`;
722
+ }
723
+ const label = deviceCommandLabel(result) || deviceRef;
724
+ return `💳 ${label} 刷卡成功`;
725
+ }
726
+ catch (error) {
727
+ this.logCommandError(error);
728
+ return `刷卡失败,${deviceActionFailureReason(error)}`;
729
+ }
698
730
  }
699
731
  async redeem(sender, rawCode) {
700
732
  const code = cleanText(rawCode);
@@ -755,7 +787,7 @@ class PrismKoishiService {
755
787
  /* ---------------------------- helpers ---------------------------------- */
756
788
  async power(sender, deviceRef, state, systemOverride = false) {
757
789
  const operation = state === "on" ? "启动" : "关闭";
758
- const staffOverride = systemOverride || this.config.powerCommandsAdminOnly === true;
790
+ const staffOverride = systemOverride || (state === "off" && this.config.powerCommandsAdminOnly === true);
759
791
  if (!systemOverride && staffOverride && !(this.config.staffUserIds ?? []).includes(sender.id)) {
760
792
  return `${operation}失败,权限不足`;
761
793
  }
@@ -766,8 +798,10 @@ class PrismKoishiService {
766
798
  payload: { state },
767
799
  }, staffOverride ? { staffOverride: true } : undefined);
768
800
  const failure = this.getCommandFailureMessage(result);
769
- if (failure)
770
- return `${operation}失败,${failure}`;
801
+ if (failure) {
802
+ this.logDeviceCommandFailure(result);
803
+ return `${operation}失败,${deviceCommandFailureReason(result)}`;
804
+ }
771
805
  const deviceLabel = result.action.payload.deviceLabel;
772
806
  return state === "on" ? `✅ ${deviceLabel} 启动成功` : `🛑 ${deviceLabel} 关闭成功`;
773
807
  }
@@ -1089,6 +1123,10 @@ class PrismKoishiService {
1089
1123
  ? (error.originalError ?? error)
1090
1124
  : error);
1091
1125
  }
1126
+ logDeviceCommandFailure(result) {
1127
+ const action = result?.action;
1128
+ this.logger?.warn("PRiSM device command failed", action?.payload?.executorFailure ?? action);
1129
+ }
1092
1130
  }
1093
1131
  function humanReadableBotError(error) {
1094
1132
  if (error.code === "DUPLICATE_SESSION_LABEL") {
@@ -1133,6 +1171,33 @@ function powerFailureReason(error) {
1133
1171
  }
1134
1172
  return "请稍后再试";
1135
1173
  }
1174
+ function deviceActionFailureReason(error) {
1175
+ if (!(error instanceof PrismBotClientError))
1176
+ return "请稍后再试";
1177
+ if (error.code === "DEVICE_COMMAND_REQUIRES_ACTIVE_SESSION")
1178
+ return "玩家未入场";
1179
+ if (error.code === "DEVICE_NOT_FOUND")
1180
+ return "设备不存在";
1181
+ if (error.code === "SCAN_IDENTITY_NOT_BOUND_TO_PLAYER")
1182
+ return "玩家未绑定 Aime 卡";
1183
+ if (error.code === "COIN_COMMAND_COOLDOWN_ACTIVE")
1184
+ return "投币冷却中,请稍后再试";
1185
+ return "请稍后再试";
1186
+ }
1187
+ function deviceCommandFailureReason(result) {
1188
+ const action = result?.action;
1189
+ if (!action || (action.status !== "expired" && action.status !== "rejected"))
1190
+ return null;
1191
+ const message = cleanText(action.payload?.executorFailure?.message);
1192
+ if (message.includes("没有在线客户端"))
1193
+ return "设备未连接";
1194
+ if (message.includes("设备配置不存在"))
1195
+ return "设备未配置";
1196
+ return "请稍后再试";
1197
+ }
1198
+ function deviceCommandLabel(result) {
1199
+ return cleanText(result?.action?.payload?.deviceLabel);
1200
+ }
1136
1201
  function genericCommandFailure() {
1137
1202
  return "操作失败,请稍后再试。";
1138
1203
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism",
3
- "version": "0.1.37",
3
+ "version": "0.1.38",
4
4
  "description": "PRiSM Next 计费与设备管理系统的 Koishi 机器人集成插件",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {