koishi-plugin-prism 0.5.2 → 0.5.3

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
@@ -42,7 +42,7 @@ Koishi 里只填怎么连到店铺:
42
42
  | 门锁 | `lock` 打开的门(带门锁能力的资源);店里只有一扇门时默认就是它。没有门时 `lock` 回复本店还没有设置。 |
43
43
  | 入场后自动获取门锁密码 | 默认关闭。打开后 `login`/`入场` 成功时顺带生成一个 8 位 TTLock 临时密码(有效期 3 分钟)一并回复;门锁失败只多一行提示,不会让入场失败。没有门锁时不生效。 |
44
44
  | 播报与提醒 | 只有一处设置:**播报群**(Koishi 的 `平台:频道` 格式,如群 `onebot:群号`、私聊 `onebot:private:QQ号`)和**提醒提前量**(分钟)。闲置提醒和店内动态都发到这一处,不再分成两个群。 |
45
- | 播报这些 | 五个开关,各自默认关闭:设备闲置(关机前提醒,提前量为 0 时不提醒)、开机/关机(含闲置自动关机)、上机(刷卡或投币后开始占用机台)、刷卡、投币。刷卡或投币同时上机时只发一条:开了「上机」按上机播报,否则按刷卡/投币播报。这个 Bot 自己执行的命令已经回复过,不再播报;别的 Bot、网页和机台上发生的照常播报。下机不播报。 |
45
+ | 播报这些 | 在后台按事件类型逐项勾选(`announceEvents`),平台按勾选只发相应的动态,写好要说的话并直接发到播报群(其中的玩家由插件换成群里的称呼)。分三组:**现场发生的事**(开机、关机、刷卡、上机、机台投币、开局扣币、投币计数清零,来自机台和 Home Assistant 的回报,不论在哪里操作的都只说一次)、**通过平台的操作**(通过平台开机/关机、闲置自动关机、通过平台刷卡、通过平台投币,只说明有人在平台上发起了它)和**提醒**(闲置关机提醒,提前量为 0 时不提醒)。这个 Bot 自己执行的命令已经回复过,不再播报。 |
46
46
 
47
47
  播报与闲置提醒直接通过对应平台的第一个在线机器人发送,不依赖 Koishi 的数据库、频道分配或 silent 标记,是否播报由 PRiSM 设置决定。群聊填写 `平台:频道`,私聊填写 `平台:private:用户ID`;频道 ID 内的冒号保持原样。连续播报不会修改缓存的频道列表,单个目标发送失败会记录目标及错误并继续其他目标和后续播报。相同平台有多个机器人时,第一个在线账号须能访问目标群;离线时不缓存补发。
48
48
 
@@ -50,13 +50,13 @@ HA 监听的健康心跳与 HA ping 均为每 60 秒一次,与机台 IO 对齐
50
50
 
51
51
  **店员**是在这家店工作、并在网页里绑定了聊天平台身份的人:Bot 按「平台 + 账号」认人,`onebot:114514` 是店员不代表 `telegram:114514` 也是。**货币名称**取店铺的余额资产名,**麻将桌**和**门锁**取店铺的资源,都不需要在 Koishi 里重复填写。店铺没有整店计费开关。Bot 属于一个资源组,`login`/`入场` 开始的是这个资源组(或 Bot 资源专属)的计费方案;平台下发的 `billingEnabled` 只表示通过这个 Bot 入场是否计费。为 `false` 时 Bot 不请求平台,直接回复「这里不计费,无需入场」;平台拒绝时(例如资源组刚改成免费)回复同样的话。钱包、账单、结账、兑换、上下桌、`list` 等命令属于玩家或各自的资源,跟 Bot 所在资源组无关,一律交给平台处理。旧平台整店未启用计费时返回的 `BILLING_DISABLED` 仍提示「本店未启用计费」。
52
52
 
53
- Bot 连上平台后先读取这些设置,再向平台打招呼;网页里改了设置、店员绑定了新身份、增删了麻将桌或门锁时,平台会通知 Bot 重新读取,**不需要重启 Koishi**。
53
+ 这些设置由平台自己读、自己判断,插件不读取;网页里改了设置、店员绑定了新身份、增删了麻将桌或门锁时立即生效,**不需要重启 Koishi**。
54
54
 
55
55
  旧版的 `autoRegister`、`currencyName`、`defaultDoorDeviceId`、`defaultScanProvider`(刷卡固定使用 Aime)、`loginPricingConfigIds`、`loginSessionLabel`、`enableStaffCommands`、`staffUserIds`、`powerCommandsAdminOnly`、`playerCommands`、`readOnlyNotice`、`logoutNotifyUserIds`、`idleNotifyChannelIds`、`idleReminderMinutes`、`watchHomeAssistant`(改由平台调度)、`botName`、`mahjongTableConfigs`、`mahjongTableSize` 已全部移除;升级后请到 Bot 的资源页重新设置。玩家首次使用时是否自动建档由店铺自己的注册开关决定。入场开始这个 Bot 在 PRiSM 里的计费方案:Bot 资源的专属方案,否则它所属资源组的方案;没改过的 Bot 在默认资源组,即店铺默认计费方案。已经在计费时 Bot 回复「您已在「方案名」中」。
56
56
 
57
57
  ### 统一平台
58
58
 
59
- 配置 `baseUrl` 和本店的 Integration 令牌后(店铺编号可选),插件直接连 `/integration/ws`:凭据在握手时解析出店铺,和机台连 `/io/ws` 一样,地址里没有店铺编号。插件与 PRiSM 只保持**一条 WebSocket 连接**,所有命令都作为请求帧走这条连接,平台也通过它推送闲置关机提醒。连接断开时命令直接回复「操作失败,暂时连不上服务器。」,不排队、不自动重发;插件在后台自动重连。需要 Koishi 的 `http` 服务(`ctx.http.ws`)。不再支持不填 `shopCode` 的单店兼容部署。
59
+ 配置 `baseUrl` 和本店的 Integration 令牌后(店铺编号可选),插件直接连 `/integration/ws`:凭据在握手时解析出店铺,和机台连 `/io/ws` 一样,地址里没有店铺编号。插件与 PRiSM 只保持**一条 WebSocket 连接**,玩家的命令原样(命令名和参数)转给平台,平台执行后告诉插件说什么:第一句作为命令的回复(引用原消息),其余的(如私聊店员的账单)照平台说的地方发出;店内动态和闲置提醒也是平台写好、指定发到哪个群。插件里没有命令的业务逻辑,也不再自己判断权限和措辞。30 秒没有回音时回复「服务器没有及时回应,请稍后确认结果」。连接断开时命令直接回复「操作失败,暂时连不上服务器。」,不排队、不自动重发;插件在后台自动重连。需要 Koishi 的 `http` 服务(`ctx.http.ws`)。不再支持不填 `shopCode` 的单店兼容部署。
60
60
 
61
61
  玩家在网页取得验证码后,向任意已接入的店铺 Bot 发送 `bind 验证码`(旧命令 `prism.bind` 仍可用)。店家可以在 PRiSM「设置 → 玩家与身份 → 绑定提示」改写玩家看到的绑定提示。所有命令从消息的 `session.platform` 和 `session.userId` 读取身份,发送 `{ code, provider, subject }`,不接受手填或代填身份;`provider` 配置已移除。例如同一实例的 OneBot 和 Telegram 消息分别使用 `onebot:114514` 和 `telegram:114514`。群/私聊过滤交给 Koishi。网页账号可以跨店使用,平台验证、玩家和余额按店铺隔离;同一网页账号追加平台绑定时复用同一个玩家。
62
62
 
@@ -105,9 +105,9 @@ Bot 设置开启「账单私聊店员」时,玩家退场或店员覆盖结账
105
105
  * `麻将列表` - 查看店里麻将桌的桌名、别名,以及空闲、等位或游玩中状态。
106
106
  * `api测速 [次数]` - 走 HTTP 连续查询自己的钱包:首次(含建立连接)单独列出,之后显示最小、平均与最大延迟,并给出服务端处理时间和经过的 Cloudflare 节点(默认 5 次,最多 10 次)。
107
107
  * `ws测速 [次数]` - 走 Bot 的 WebSocket 连接:心跳往返(纯网络)和同样的钱包查询,并给出服务端处理时间(默认 5 次,最多 10 次)。两者对比可以看出延迟在网络还是在服务端,以及长连接能省下多少。
108
- * `versions` - 显示当前 Bot npm 包版本和后端发布版本;后端不可达时仍会显示 Bot 版本。
108
+ * `versions` - 显示当前 Bot npm 包版本,以及后端是哪次部署(Worker 名称和提交号的前 12 位,如 `prism-link-beta (df66f121da54)`);后端不可达时仍会显示 Bot 版本。
109
109
 
110
- Bot 版本直接读取本 npm 包的 `package.json`,后端版本读取无需认证的 `GET /version`。插件发布遵循 SemVer;修复使用 patch、兼容新增功能使用 minor、不兼容改动使用 major,发布前通过 `npm version <patch|minor|major>` 自动更新包版本。
110
+ Bot 版本直接读取本 npm 包的 `package.json`,后端的部署读取无需认证的 `GET /version`(`{ worker, revision }`;后端没有发布版本号)。插件发布遵循 SemVer;修复使用 patch、兼容新增功能使用 minor、不兼容改动使用 major,发布前通过 `npm version <patch|minor|major>` 自动更新包版本。
111
111
 
112
112
  ### 店员快捷指令
113
113
 
@@ -166,6 +166,12 @@ npm version minor --no-git-tag-version
166
166
 
167
167
  绑定支持 Koishi 提供的各适配器标识,身份值保持字符串,不限定数字格式。店员和账单私聊按「平台标识 + 身份值」认人,相同号码的其他平台不会继承权限或通知。管理员代操作使用目标平台标识,禁止通过当前适配器误操作另一个平台。`/list` 只用当前平台的身份查询昵称,缺失时使用后台名称。源码和 npm 发行包需要分别核对版本。
168
168
 
169
+ ### 0.5.3
170
+
171
+ 插件改为只转发命令:`login`、`logout`、`wallet` 等命令连同参数原样发给平台(`command` 帧),平台执行、判断权限(只读模式、店员、计费)、写好回复,插件照发(`say` 帧);账单私聊店员、入场附带的门锁密码也由平台决定发到哪。店内动态和闲置提醒同样由平台写好、按 Bot 的监听规则发到规则指定的群或个人,插件不再读取 Bot 设置,也不再收 `shop.event`。插件自己只保留 `versions`、`api测速`、`ws测速` 和 Home Assistant 监听。播报新增机台投币、开局扣币、投币计数清零(需要机台 IO 开启 `cabinetMonitor`),以及来自 Home Assistant 的开机、关机;刷卡改为在游戏取到卡时播报,网页刷卡和实体卡一样只说一次;`下桌` 不必再指定桌号。需要支持命令协议的 PRiSM,店铺所在的 PRiSM 升级之前不要升级到此版本。
172
+
173
+ `versions` 按平台现在的 `GET /version` 显示后端:Worker 名称和提交号,不再有发布版本号。
174
+
169
175
  ### 0.5.2
170
176
 
171
177
  完整恢复 0.4.1/0.4.2 的修复:HA 心跳与 ping 每 60 秒一次;店内动态和闲置预告直接通过适配器发送,不依赖 Koishi 数据库,不修改频道列表,支持群聊/私聊及逐目标失败隔离。电源播报继续由平台根据 HA 事件分发。同时支持店员 `/上桌 a @玩家`、`/下桌 a @玩家`,保留 0.5.1 的兑换提示修复。
@@ -198,10 +204,4 @@ Koishi 配置只剩 `baseUrl` 和 `integrationToken`(`shopCode` 可选、已
198
204
 
199
205
  ## 时间展示
200
206
 
201
- 账单、历史与资产到期使用 API 时间字符串所带的店铺偏移,例如 `2026-10-03T10:08:00+08:00`,不依赖 Bot 服务器时区,也不手工加八小时。账单注明 UTC 偏移;时间排序及游玩时长按绝对时间计算,兼容旧 `Z` 时间戳。网页/Swift 的个人时间可按玩家设备时区展示,因此跨地区使用时显示时钟不同,但时长和金额一致。
202
-
203
- 当前上下桌功能使用平台 `integration/players/by-identity/mahjong`;平台不再兼容旧 Bot 通过指定方案的 `session/start` 模拟麻将计费。
204
-
205
- ### 0.4.1 / 0.4.2 修复恢复
206
-
207
- 回退 0.4.0 后发布的 0.5.1 漏带了两组修复:0.4.1 的 60 秒 HA 心跳和 ping,以及 0.4.2 的无数据库播报发送、群聊/私聊、逐目标失败隔离和连续播报回归测试。现已完整恢复,保留后续兑换与管理员上下桌改动。Bot 本地判断 HA 电源变化的已撤回实现不属于这些修复。
207
+ 账单、历史与资产到期的时间由平台按店铺时区写好,例如「⏰ 游玩时间:18:00–19:00(UTC+08:00)」,不依赖 Bot 服务器时区。
package/lib/index.d.ts CHANGED
@@ -10,77 +10,18 @@ export declare class PrismBotClientError extends Error {
10
10
  readonly code: string;
11
11
  readonly status: number;
12
12
  readonly body: unknown;
13
- readonly originalError?: unknown | undefined;
14
- constructor(message: string, code: string, status: number, body: unknown, originalError?: unknown | undefined);
13
+ constructor(message: string, code: string, status: number, body: unknown);
15
14
  }
16
- /** What the bot is told about itself and the shop by `GET integration/bot`. */
17
- export type BotConfig = {
18
- bot: {
19
- id: string;
20
- name: string;
21
- };
22
- settings: {
23
- /** "readonly" leaves players binding, lookups and redeeming; staff keep every command. */
24
- playerCommands: "all" | "readonly";
25
- readOnlyNotice: string;
26
- /** Staff may act for other players. */
27
- staffCommands: boolean;
28
- /** Only staff may switch machines off. */
29
- powerOffStaffOnly: boolean;
30
- /** Staff on the same platform get checkout receipts privately. */
31
- receiptsToStaff: boolean;
32
- /** `login`/`入场` also hands out the door code. */
33
- doorPasswordOnEntry: boolean;
34
- /** Channels (`platform:channelId`) the shop says everything in: idle warnings and what happens. */
35
- announceChannelIds: string[];
36
- /** How many minutes before an idle power-off to tell them; 0 for never. */
37
- idleReminderMinutes: number;
38
- /** Which news is told, each on its own. */
39
- announceIdle: boolean;
40
- announcePower: boolean;
41
- announceStarts: boolean;
42
- announceCards: boolean;
43
- announceCoins: boolean;
44
- };
45
- /** Which shop this credential belongs to; the shop code is no longer configured by hand. */
46
- shop?: {
47
- code: string;
48
- name: string;
49
- };
50
- /** Whether entering through this bot bills: its own plan, otherwise its resource group's. */
51
- billingEnabled: boolean;
52
- currencyName: string;
53
- /** Staff are these platform identities. */
54
- staff: Array<{
55
- provider: string;
56
- subject: string;
57
- }>;
58
- /** The door `lock`/开门 opens. */
59
- door: {
60
- id: string;
61
- name: string;
62
- } | null;
63
- mahjongTables: Array<{
64
- id: string;
65
- name: string;
66
- aliases: string[];
67
- capacity: number;
68
- }>;
69
- };
70
15
  export type PrismKoishiPluginConfig = {
71
16
  /**
72
- * Used by /list to resolve player display names from the chat platform.
73
- * For Koishi, this maps an adapter subject and provider to a nickname (e.g. the
74
- * member list of the current group). If not provided, the plugin will fall
75
- * back to `playerDisplayName` from the backend.
17
+ * How the chat names a player: an adapter subject and provider to a nickname
18
+ * (e.g. from the current group's member list). Without it, the platform's
19
+ * display name is used.
76
20
  */
77
21
  resolveDisplayName?: (subject: string, provider: string) => Promise<string | null | undefined> | string | null | undefined;
78
- /** Get a stable current ISO timestamp; mainly for tests. */
79
- now?: () => Date;
80
22
  shopCode?: string;
81
23
  baseUrl?: string;
82
24
  integrationToken?: string;
83
- client?: any;
84
25
  /** Opens the platform connection; defaults to Koishi's `ctx.http.ws`. Mainly for tests. */
85
26
  socket?: SocketFactory;
86
27
  /** Plain HTTP POST, used only by `api测速`; defaults to Koishi's `ctx.http`. Mainly for tests. */
@@ -108,12 +49,8 @@ export type SocketLike = {
108
49
  addEventListener(type: "open" | "message" | "close" | "error", listener: (event: any) => void): void;
109
50
  };
110
51
  export type SocketFactory = (url: string, headers: Record<string, string>) => SocketLike;
111
- export type KoishiCommandRegistration = {
112
- description: string;
113
- action: (context: KoishiActionContext, ...args: string[]) => Promise<string> | string;
114
- };
115
52
  export type KoishiCommandBuilder = {
116
- action(handler: KoishiCommandRegistration["action"]): KoishiCommandBuilder;
53
+ action(handler: (context: KoishiActionContext, ...args: any[]) => Promise<string> | string): KoishiCommandBuilder;
117
54
  };
118
55
  export type KoishiLikeContext = {
119
56
  command(name: string, description: string): KoishiCommandBuilder;
@@ -133,8 +70,8 @@ export type KoishiActionContext = {
133
70
  platform?: string;
134
71
  isDirect?: boolean;
135
72
  userId: string;
73
+ channelId?: string;
136
74
  messageId?: string;
137
- senderId?: string;
138
75
  senderName?: string;
139
76
  username?: string;
140
77
  bot?: {
@@ -142,33 +79,70 @@ export type KoishiActionContext = {
142
79
  getUser?(id: string): Promise<{
143
80
  name?: string;
144
81
  }>;
145
- broadcast?(userIds: string[], content: string): Promise<void>;
82
+ /** Send to this bot's channels by id; `private:<user>` for a private chat. */
83
+ broadcast?(channels: string[], content: string): Promise<unknown>;
146
84
  };
147
85
  };
148
86
  };
149
87
  export declare function applyPrismKoishiPlugin(ctx: KoishiLikeContext, config: PrismKoishiPluginConfig): void;
150
- type PlatformEvent = {
151
- event: string;
152
- data: unknown;
88
+ /** A player as the platform names one in what it says. */
89
+ type MessagePlayer = {
90
+ playerId: string;
91
+ displayName: string | null;
92
+ identities: Array<{
93
+ provider: string;
94
+ subject: string;
95
+ }>;
153
96
  };
154
97
  /**
155
- * The one connection to PRiSM. Requests go out as frames and are matched to
156
- * their replies by id; the platform also pushes events down it. While it is
157
- * down a request fails at once: queueing a checkout or a top-up to run later
158
- * would make players repeat it, and a resend could run it twice.
98
+ * What the platform writes for a bot to say (PRiSM's core `bot-message.ts`):
99
+ * text, and players the bot names as its chat knows them. A segment of a kind
100
+ * this version does not know is said by its `text`, if it has one.
101
+ */
102
+ type BotMessage = Array<{
103
+ text?: string;
104
+ player?: MessagePlayer;
105
+ }>;
106
+ /** `say`: something to say, the answer to a command (`replyTo`) or the shop's news. */
107
+ export type SayFrame = {
108
+ type: "say";
109
+ message: BotMessage;
110
+ replyTo?: string;
111
+ to?: string;
112
+ done?: boolean;
113
+ };
114
+ type ChatIdentity = {
115
+ provider: string;
116
+ subject: string;
117
+ name?: string;
118
+ };
119
+ /** A player's command as the platform runs it. */
120
+ type CommandFrame = {
121
+ name: string;
122
+ args: string[];
123
+ from: ChatIdentity;
124
+ target?: ChatIdentity;
125
+ channel: string;
126
+ };
127
+ type SocketReply = {
128
+ status: number;
129
+ body: any;
130
+ serverMs?: number | null;
131
+ };
132
+ /**
133
+ * The one connection to PRiSM. Commands go out as `command` frames and the
134
+ * platform answers with `say` frames naming them; the shop's news comes as
135
+ * `say` frames naming none. While it is down a command fails at once:
136
+ * queueing a checkout or a top-up to run later would make players repeat it.
159
137
  */
160
138
  export declare class PrismSocket {
161
139
  private readonly open;
162
- /** Resolved at each connection: the shop is learned from the credential first. */
163
140
  private readonly url;
164
141
  private readonly token;
165
- /**
166
- * What to say in hello, read afresh each time: the bot's settings live on
167
- * the platform, so they are fetched over this connection before it is
168
- * sent. Null when they could not be read.
169
- */
142
+ /** What to say in hello besides that this bot sends commands. */
170
143
  private readonly hello;
171
- private readonly onEvent;
144
+ /** The shop's news: something to say that answers no command. */
145
+ private readonly onNews;
172
146
  private readonly logger?;
173
147
  /**
174
148
  * `onWatch`: the platform asked this bot to watch Home Assistant, or with null to stop.
@@ -177,24 +151,20 @@ export declare class PrismSocket {
177
151
  private readonly hooks?;
178
152
  private socket;
179
153
  private readonly pending;
154
+ /** Commands still being answered, by id. */
155
+ private readonly listening;
180
156
  private retryMs;
181
157
  private retryTimer;
182
158
  private pingTimer;
183
159
  private lastHeard;
184
160
  private stopped;
185
161
  private sequence;
186
- /** Bumped by every hello being prepared, so only the newest one is sent. */
187
- private greeting;
188
162
  private readonly pongWaiters;
189
- constructor(open: SocketFactory,
190
- /** Resolved at each connection: the shop is learned from the credential first. */
191
- url: string | (() => string), token: string,
192
- /**
193
- * What to say in hello, read afresh each time: the bot's settings live on
194
- * the platform, so they are fetched over this connection before it is
195
- * sent. Null when they could not be read.
196
- */
197
- hello: () => Promise<Record<string, unknown> | null>, onEvent: (event: PlatformEvent) => void, logger?: {
163
+ constructor(open: SocketFactory, url: string, token: string,
164
+ /** What to say in hello besides that this bot sends commands. */
165
+ hello: () => Record<string, unknown>,
166
+ /** The shop's news: something to say that answers no command. */
167
+ onNews: (say: SayFrame) => void, logger?: {
198
168
  warn(...args: unknown[]): void;
199
169
  } | undefined,
200
170
  /**
@@ -208,45 +178,33 @@ export declare class PrismSocket {
208
178
  /** Send a frame if the connection is up; a report made while it is down is dropped, as the platform has forgotten the watch by then. */
209
179
  send(frame: Record<string, unknown>): void;
210
180
  start(): void;
211
- /** Connect again with what the platform has just told us, such as the shop. */
212
- reconnect(): void;
213
181
  stop(): void;
214
182
  private connect;
215
- /**
216
- * Read the bot's settings and say hello with them: on connecting, and again
217
- * when the platform says they changed. A connection that cannot read them
218
- * at all is closed and tried again later, without resetting the backoff, so
219
- * a platform that keeps failing is not asked every second.
220
- */
183
+ /** Hello: on connecting, and again when the platform says the bot's settings changed, so it rereads them. */
221
184
  private greet;
222
185
  private reconnectLater;
223
186
  private failPending;
224
187
  private receive;
188
+ private nextId;
189
+ /**
190
+ * Send a command; `onSay` hears each thing the platform says for it, or
191
+ * null once the connection is gone. False while the connection is down.
192
+ */
193
+ command(frame: CommandFrame, onSay: (say: SayFrame | null) => void): boolean;
225
194
  /** One heartbeat round trip in milliseconds: the network to the platform and nothing else. */
226
195
  ping(): Promise<number>;
196
+ /** A platform route over the connection: the version and the speed tests, which measure exactly that. */
227
197
  request(frame: {
228
- method: "GET" | "POST" | "PUT";
198
+ method: "GET" | "POST";
229
199
  path: string;
230
- query?: Record<string, unknown>;
231
200
  body?: unknown;
232
201
  }): Promise<SocketReply>;
233
202
  }
234
- type SocketReply = {
235
- status: number;
236
- body: any;
237
- serverMs?: number | null;
238
- };
239
203
  /**
240
204
  * The platform connection URL. The credential names the shop, as a device's
241
205
  * token names its machine at `/io/ws`, so nothing identifying is in the path.
242
206
  */
243
207
  export declare function platformSocketUrl(baseUrl: string): string;
244
- export type Sender = {
245
- id: string;
246
- name: string;
247
- provider: string;
248
- };
249
- export declare function humanReadableBotError(error: PrismBotClientError): string;
250
208
  declare const _default: {
251
209
  name: string;
252
210
  Config: Schema<PrismKoishiPluginConfig>;