koishi-plugin-prism 0.4.2 → 0.5.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
@@ -15,6 +15,8 @@
15
15
 
16
16
  所有技术异常在聊天中只显示通用失败提示,不会包含后端名称、URL、网络状态、原始 `fetch` 错误或 HTTP 状态。完整原始异常会写入 Koishi 的 `prism` 日志,供管理员排查。
17
17
 
18
+ 兑换码无效、尚未生效、过期、领完,以及礼物已领取或达到限领次数时,`/redeem` 返回对应的中文提示;锁争用超过后端等待预算时提示其他操作正在处理中。公共码并发等待和次数校验由平台执行,Bot 不自行重发兑换请求。命令返回文本只确认已交给 Koishi;真实群聊投递仍由聊天适配器完成。
19
+
18
20
  ## ⚙️ 配置说明
19
21
 
20
22
  Koishi 里只填怎么连到店铺:
@@ -42,8 +44,6 @@ Koishi 里只填怎么连到店铺:
42
44
  | 播报与提醒 | 只有一处设置:**播报群**(Koishi 的 `平台:频道` 格式,如群 `onebot:群号`、私聊 `onebot:private:QQ号`)和**提醒提前量**(分钟)。闲置提醒和店内动态都发到这一处,不再分成两个群。 |
43
45
  | 播报这些 | 五个开关,各自默认关闭:设备闲置(关机前提醒,提前量为 0 时不提醒)、开机/关机(含闲置自动关机)、上机(刷卡或投币后开始占用机台)、刷卡、投币。刷卡或投币同时上机时只发一条:开了「上机」按上机播报,否则按刷卡/投币播报。这个 Bot 自己执行的命令已经回复过,不再播报;别的 Bot、网页和机台上发生的照常播报。下机不播报。 |
44
46
 
45
- 播报与提醒直接通过对应平台的第一个可用机器人发送,无需 Koishi 本地数据库或频道分配,也不读取 Koishi 数据库中的 silent 标记;是否播报由 PRiSM 的上述开关决定。群聊填写 `平台:频道`,私聊填写 `平台:private:用户ID`。同一 Koishi 实例有多个同平台账号时,应确保第一个可用账号能访问配置的目标群。单个目标发送失败会记录目标与错误,并继续其他目标和后续播报;平台未连接时不会缓存补发。
46
-
47
47
  **店员**是在这家店工作、并在网页里绑定了聊天平台身份的人:Bot 按「平台 + 账号」认人,`onebot:114514` 是店员不代表 `telegram:114514` 也是。**货币名称**取店铺的余额资产名,**麻将桌**和**门锁**取店铺的资源,都不需要在 Koishi 里重复填写。店铺没有整店计费开关。Bot 属于一个资源组,`login`/`入场` 开始的是这个资源组(或 Bot 资源专属)的计费方案;平台下发的 `billingEnabled` 只表示通过这个 Bot 入场是否计费。为 `false` 时 Bot 不请求平台,直接回复「这里不计费,无需入场」;平台拒绝时(例如资源组刚改成免费)回复同样的话。钱包、账单、结账、兑换、上下桌、`list` 等命令属于玩家或各自的资源,跟 Bot 所在资源组无关,一律交给平台处理。旧平台整店未启用计费时返回的 `BILLING_DISABLED` 仍提示「本店未启用计费」。
48
48
 
49
49
  Bot 连上平台后先读取这些设置,再向平台打招呼;网页里改了设置、店员绑定了新身份、增删了麻将桌或门锁时,平台会通知 Bot 重新读取,**不需要重启 Koishi**。
@@ -143,6 +143,8 @@ npm version minor --no-git-tag-version
143
143
 
144
144
  也可以推送与 `package.json` 一致的 `v` 标签,或在 Actions 里手动运行 `Publish to npm`。
145
145
 
146
+ 回退到 0.4.0 时,手动运行上述工作流会将 npm 的 `latest` 指回已发布的 0.4.0;需要 Trusted Publisher 已允许管理 dist-tag,不会重新发布或删除版本。
147
+
146
148
  首次使用前,在 npm 包设置的 Trusted Publisher 中选择 GitHub Actions,填写仓库 `nerimoe/koishi-plugin-prism` 和工作流文件名 `publish.yml`,并允许 `npm publish`。
147
149
 
148
150
  ## 📄 开源协议
@@ -157,13 +159,9 @@ npm version minor --no-git-tag-version
157
159
 
158
160
  绑定支持 Koishi 提供的各适配器标识,身份值保持字符串,不限定数字格式。店员和账单私聊按「平台标识 + 身份值」认人,相同号码的其他平台不会继承权限或通知。管理员代操作使用目标平台标识,禁止通过当前适配器误操作另一个平台。`/list` 只用当前平台的身份查询昵称,缺失时使用后台名称。源码和 npm 发行包需要分别核对版本。
159
161
 
160
- ### 0.4.2
161
-
162
- 修复店内动态与闲置提醒使用 `ctx.broadcast` 导致的数据库依赖警告及漏发:改用适配器消息接口,连续播报不会修改缓存的频道配置,目标群不必预先存在于 Koishi 的频道数据库。支持群聊和私聊,单个目标失败不影响其他目标。
163
-
164
- ### 0.4.1
162
+ ### 0.5.1
165
163
 
166
- Bot 作为 Home Assistant 监听者时的心跳从 30 秒改成 60 秒,与机台 IO 一致,平台因此用同一个租约(150 秒)衡量两者。同一个间隔也是向 Home Assistant 发的 `ping`,所以 Home Assistant 停止应答现在最多 60 秒后才发现(原来 30 秒)。
164
+ 在 0.4.0 的基础上补齐兑换失败提示:区分无效、未生效、过期、领完、已领取、达到限领次数与操作争用,不再统一回复稍后重试。公共码并发保护由平台负责(PRiSM `b4871d8` 起);本次不恢复旧 0.5.0 的 HA 本地播报实现,电源播报仍由平台确认 HA 事件后分发给 Bot。npm 上已发布的 0.5.0 不可覆盖,因此此修复使用 0.5.1。
167
165
 
168
166
  ### 0.4.0
169
167
 
package/lib/ha-watch.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * The platform hands a bot the Home Assistant addresses to try, a read-only token and the entities it
5
5
  * cares about (`ha.watch`). The bot subscribes with `subscribe_entities` and reports `ha.status` (also
6
- * every 60 s, as a heartbeat) and `ha.states` back over its platform connection, every report carrying
6
+ * every 30 s, as a heartbeat) and `ha.states` back over its platform connection, every report carrying
7
7
  * the term of the assignment it answers; that is how the platform tells a current watcher from a
8
8
  * replaced one. This is the same job a HINATA IO does, in the same shape, so the platform treats both
9
9
  * alike.
package/lib/ha-watch.js CHANGED
@@ -10,9 +10,7 @@ exports.parseWatch = parseWatch;
10
10
  exports.HA_TIMING = {
11
11
  connectMs: 10_000,
12
12
  authMs: 10_000,
13
- // The same beat as a machine's IO: the platform gives a watcher two beats of
14
- // lease plus slack, and does not care whether the watcher is a bot or a box.
15
- heartbeatMs: 60_000,
13
+ heartbeatMs: 30_000,
16
14
  firstRetryMs: 2_000,
17
15
  maxRetryMs: 60_000,
18
16
  };
package/lib/index.d.ts CHANGED
@@ -120,12 +120,8 @@ export type KoishiLikeContext = {
120
120
  logger?(name: string): {
121
121
  warn(...args: unknown[]): void;
122
122
  };
123
- bots?: Array<{
124
- platform: string;
125
- isActive: boolean;
126
- sendMessage(channelId: string, content: string): Promise<unknown>;
127
- sendPrivateMessage(userId: string, content: string): Promise<unknown>;
128
- }>;
123
+ /** Send to channels as `platform:channelId`. */
124
+ broadcast?(channels: string[], content: string): Promise<unknown>;
129
125
  on?(event: "dispose", listener: () => void): void;
130
126
  };
131
127
  export type KoishiActionContext = {
package/lib/index.js CHANGED
@@ -741,36 +741,18 @@ class PrismKoishiService {
741
741
  async announce(event) {
742
742
  const bot = this.bot;
743
743
  const channels = bot?.settings.announceChannelIds ?? [];
744
- if (!bot || !channels.length)
744
+ if (!bot || !channels.length || typeof this.ctx.broadcast !== "function")
745
745
  return;
746
746
  if (event.botId && event.botId === bot.bot.id)
747
747
  return;
748
748
  const message = await this.announcement(event, bot.settings);
749
749
  if (!message)
750
750
  return;
751
- await this.broadcast(channels, message);
752
- }
753
- /** Explicit targets need neither Koishi's channel database nor its mutable broadcast list. */
754
- async broadcast(channels, message) {
755
- for (const target of channels) {
756
- try {
757
- const separator = target.indexOf(":");
758
- if (separator < 1 || separator === target.length - 1)
759
- throw new Error(`无效的播报频道:${target}`);
760
- const platform = target.slice(0, separator), channel = target.slice(separator + 1);
761
- if (channel === "private:")
762
- throw new Error(`无效的私聊目标:${target}`);
763
- const bot = this.ctx.bots?.find((bot) => bot.platform === platform && bot.isActive);
764
- if (!bot)
765
- throw new Error(`没有可用的 ${platform} 机器人`);
766
- if (channel.startsWith("private:"))
767
- await bot.sendPrivateMessage(channel.slice(8), message);
768
- else
769
- await bot.sendMessage(channel, message);
770
- }
771
- catch (error) {
772
- this.logger?.warn(`PRiSM announcement to ${target} failed`, error);
773
- }
751
+ try {
752
+ await this.ctx.broadcast(channels, message);
753
+ }
754
+ catch (error) {
755
+ this.logger?.warn("PRiSM event announcement failed", error);
774
756
  }
775
757
  }
776
758
  async announcement(event, settings) {
@@ -812,7 +794,7 @@ class PrismKoishiService {
812
794
  async remindIdle(reminder) {
813
795
  const settings = this.bot?.settings;
814
796
  const channels = settings?.announceChannelIds ?? [];
815
- if (!settings?.announceIdle || !channels.length)
797
+ if (!settings?.announceIdle || !channels.length || typeof this.ctx.broadcast !== "function")
816
798
  return;
817
799
  const now = (this.options.now?.() ?? new Date()).getTime();
818
800
  const minutes = Math.max(1, Math.round((reminder.at - now) / 60_000));
@@ -820,7 +802,12 @@ class PrismKoishiService {
820
802
  const message = reminder.reason === "shop_empty"
821
803
  ? `店里没人,${minutes} 分钟后全部关机`
822
804
  : `${names} 闲置中,${minutes} 分钟后关机。已入场的玩家发送 keep 设备名 可以延迟`;
823
- await this.broadcast(channels, message);
805
+ try {
806
+ await this.ctx.broadcast(channels, message);
807
+ }
808
+ catch (error) {
809
+ this.logger?.warn("PRiSM idle reminder failed", error);
810
+ }
824
811
  }
825
812
  async sender(context) {
826
813
  const id = cleanText(context.session?.userId);
@@ -1539,6 +1526,22 @@ class PrismKoishiService {
1539
1526
  function humanReadableBotError(error) {
1540
1527
  if (error.code === "INVALID_PLATFORM_IDENTITY")
1541
1528
  return error.message;
1529
+ if (error.code === "OPERATION_IN_PROGRESS")
1530
+ return "其他操作正在处理中,请稍后再试。";
1531
+ if (error.code === "REDEEM_CODE_NOT_FOUND")
1532
+ return "兑换码无效,请检查后再试。";
1533
+ if (error.code === "REDEEM_CODE_NOT_ACTIVE")
1534
+ return "兑换码尚未生效。";
1535
+ if (error.code === "REDEEM_CODE_EXPIRED" || error.code === "PRESENT_EXPIRED")
1536
+ return "兑换码已过期。";
1537
+ if (error.code === "PRESENT_NOT_ACTIVE" || error.code === "PRESENT_ARCHIVED")
1538
+ return "这份礼物当前无法兑换。";
1539
+ if (error.code === "REDEEM_CODE_MAX_USE_REACHED")
1540
+ return "兑换码已被领完。";
1541
+ if (error.code === "PRESENT_ONCE_PER_PLAYER_REDEEMED")
1542
+ return "您已经领取过这份礼物,无需重复兑换。";
1543
+ if (error.code === "PRESENT_GRANT_LIMIT_REACHED")
1544
+ return "您已达到这份礼物的领取上限。";
1542
1545
  if (error.code === "BOT_DISABLED")
1543
1546
  return "Bot 已被店铺停用";
1544
1547
  if (error.code === "BILLING_DISABLED")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism",
3
- "version": "0.4.2",
3
+ "version": "0.5.1",
4
4
  "description": "PRiSM Next 计费与设备管理系统的 Koishi 机器人集成插件",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {