koishi-plugin-prism 0.3.3 → 0.4.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 +9 -3
- package/lib/index.d.ts +10 -2
- package/lib/index.js +109 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,8 +37,10 @@ Koishi 里只填怎么连到店铺:
|
|
|
37
37
|
| 开关机失败的原因 | 只说玩家该知道的:没有这个设备、设备未连接、设备已关机、设备没有响应;其余情况统一回「操作没有完成,请找店员」,内部原因只写日志。 |
|
|
38
38
|
| 关机仅限店员 | `off` 只允许店员使用(以店员身份越过入场要求);`on` 始终只允许已入场玩家使用。 |
|
|
39
39
|
| 账单私聊店员 | 玩家退场或店员覆盖结账后,把同一份账单私聊给同一聊天平台上的店员。 |
|
|
40
|
-
| 闲置提醒频道、提前分钟 | 闲置自动关机的提醒发到哪里(Koishi 的 `平台:频道` 格式,如群 `onebot:群号`、私聊 `onebot:private:QQ号`),以及提前几分钟;0 或不填频道则不提醒。 |
|
|
41
40
|
| 门锁 | `lock` 打开的门(带门锁能力的资源);店里只有一扇门时默认就是它。没有门时 `lock` 回复本店还没有设置。 |
|
|
41
|
+
| 入场后自动获取门锁密码 | 默认关闭。打开后 `login`/`入场` 成功时顺带生成一个 8 位 TTLock 临时密码(有效期 3 分钟)一并回复;门锁失败只多一行提示,不会让入场失败。没有门锁时不生效。 |
|
|
42
|
+
| 播报与提醒 | 只有一处设置:**播报群**(Koishi 的 `平台:频道` 格式,如群 `onebot:群号`、私聊 `onebot:private:QQ号`)和**提醒提前量**(分钟)。闲置提醒和店内动态都发到这一处,不再分成两个群。 |
|
|
43
|
+
| 播报这些 | 五个开关,各自默认关闭:设备闲置(关机前提醒,提前量为 0 时不提醒)、开机/关机(含闲置自动关机)、上机(刷卡或投币后开始占用机台)、刷卡、投币。刷卡或投币同时上机时只发一条:开了「上机」按上机播报,否则按刷卡/投币播报。这个 Bot 自己执行的命令已经回复过,不再播报;别的 Bot、网页和机台上发生的照常播报。下机不播报。 |
|
|
42
44
|
|
|
43
45
|
**店员**是在这家店工作、并在网页里绑定了聊天平台身份的人:Bot 按「平台 + 账号」认人,`onebot:114514` 是店员不代表 `telegram:114514` 也是。**货币名称**取店铺的余额资产名,**麻将桌**和**门锁**取店铺的资源,都不需要在 Koishi 里重复填写。店铺没有整店计费开关。Bot 属于一个资源组,`login`/`入场` 开始的是这个资源组(或 Bot 资源专属)的计费方案;平台下发的 `billingEnabled` 只表示通过这个 Bot 入场是否计费。为 `false` 时 Bot 不请求平台,直接回复「这里不计费,无需入场」;平台拒绝时(例如资源组刚改成免费)回复同样的话。钱包、账单、结账、兑换、上下桌、`list` 等命令属于玩家或各自的资源,跟 Bot 所在资源组无关,一律交给平台处理。旧平台整店未启用计费时返回的 `BILLING_DISABLED` 仍提示「本店未启用计费」。
|
|
44
46
|
|
|
@@ -69,7 +71,7 @@ Bot 一直在线,所以平台会优先让 Bot 当店铺的 Home Assistant **
|
|
|
69
71
|
|
|
70
72
|
### 玩家指令
|
|
71
73
|
* `bind <验证码>` - 把网页登录账号绑定到当前平台身份(旧命令 `prism.bind` 仍可用)。玩家第一次绑定时是否自动建档由店铺设置决定,原来的 `register` 命令已移除。
|
|
72
|
-
* `login` / `入场` - 开启当前玩家的计费场次:开始这个 Bot 在 PRiSM 里的计费方案(资源编辑页「计费」一节设置所属资源组或专属方案)。
|
|
74
|
+
* `login` / `入场` - 开启当前玩家的计费场次:开始这个 Bot 在 PRiSM 里的计费方案(资源编辑页「计费」一节设置所属资源组或专属方案)。Bot 设置开了「入场后自动获取门锁密码」时,成功入场后会再回复一条门锁临时密码。
|
|
73
75
|
* `logout` - 结算当前玩家的计费场次。
|
|
74
76
|
玩家在未产生任何费用时退场,机器人会简洁显示“本次未产生费用”和当前余额;存在收费或优惠明细时仍显示完整结算账单。负单价 session 会在区间明细中保留真实的负数计费贡献,只有后端汇总全部 session 后的最终应付金额会限制为不低于 `0`。
|
|
75
77
|
结账成功回执中的余额为后端已经完成扣款后的余额;只有 `/billing` 预览会显示当前余额与预计结账后余额。
|
|
@@ -81,7 +83,7 @@ Bot 设置开启「账单私聊店员」时,玩家退场或店员覆盖结账
|
|
|
81
83
|
* `billing` - 预览当前玩家本场计费的消费费用。
|
|
82
84
|
* `wallet` - 查看当前玩家的钱包余额。
|
|
83
85
|
* `items` - 查看当前玩家持有的道具或资产。
|
|
84
|
-
* `list [资源或方案]` - 查看在店的人:正在玩(占用某个资源)或有任意进行中计费的人都算;刷了本店认不出主人的卡不算占用,`[总计 N 人]`
|
|
86
|
+
* `list [资源或方案]` - 查看在店的人:正在玩(占用某个资源)或有任意进行中计费的人都算;刷了本店认不出主人的卡不算占用,`[总计 N 人]` 就是这个人数。每人只列一处:正在占用的资源下(显示当前人数和容量),没有占用资源的人统一列在「其他」下,不再按计费方案分组。带名称时只列这一项:资源名或别名列出上面的人,方案名列出所有在计费这个方案的人,例如 `list maimai`、`list 普通游玩`。
|
|
85
87
|
* `show [deviceId]` - 查看设备电源与连接状态。
|
|
86
88
|
设备状态按后端的普通字符串契约显示;插件仍兼容历史数据库中的 JSON 字符串状态,避免旧记录显示为 `unknown`。
|
|
87
89
|
* `history` - 查看自己的历史游玩记录。
|
|
@@ -153,6 +155,10 @@ npm version minor --no-git-tag-version
|
|
|
153
155
|
|
|
154
156
|
绑定支持 Koishi 提供的各适配器标识,身份值保持字符串,不限定数字格式。店员和账单私聊按「平台标识 + 身份值」认人,相同号码的其他平台不会继承权限或通知。管理员代操作使用目标平台标识,禁止通过当前适配器误操作另一个平台。`/list` 只用当前平台的身份查询昵称,缺失时使用后台名称。源码和 npm 发行包需要分别核对版本。
|
|
155
157
|
|
|
158
|
+
### 0.4.0
|
|
159
|
+
|
|
160
|
+
闲置提醒与店内动态合并成一件事:Bot 只有一处「播报群」(`announceChannelIds`),以及五种各自独立的播报开关——设备闲置、开机/关机(含闲置自动关机)、上机、刷卡、投币;频道不再是区分两类消息的依据。投币从「上机」里分出来单独播报,投币同时上机时仍只发一条,按「上机」的措辞。新增 Bot 设置「入场后自动获取门锁密码」(`doorPasswordOnEntry`,默认关闭):打开后 `login`/`入场` 成功后顺带回复门锁临时密码,门锁失败不影响入场。需要配套 PRiSM(平台侧迁移 `0046_bot_announcement_channels.sql`),旧版平台下发的 `idleChannelIds`/`eventChannelIds` 不再被读取,升级平台前不要升级到此版本。
|
|
161
|
+
|
|
156
162
|
### 0.3.0
|
|
157
163
|
|
|
158
164
|
Koishi 配置只剩 `baseUrl` 和 `integrationToken`(`shopCode` 可选、已不需要)。Bot 在 PRiSM 里是一个资源,其余设置(只读模式、店员命令、关机权限、账单私聊、闲置提醒、门锁、名字、启用)都在商户工作台的「Bot 设置」里改,连接后读取、修改后由平台通知重新读取。店员改为网页里绑定了平台身份的店员,货币名称、门锁和麻将桌取自店铺。麻将改由平台完成(不必先入场,满桌开局),`下桌` 可指明桌号;`list` 按平台的在店名单输出(每人一处,资源带人数/容量),`list <资源或方案>` 只列这一项;下桌后回复还在计费的方案;入场开始 Bot 所在资源组的方案(或 Bot 的专属方案),重复入场回复「已在「方案名」中」,入场结束了互斥方案时列出被结束的方案。移除插件自己的麻将桌状态与所有旧配置项。需要配套支持 `integration/bot`、`integration/mahjong-tables`、`integration/roster` 和 `config.changed` 的 PRiSM(1.1.0 起);店铺所在的 PRiSM 升级之前不要升级到此版本。
|
package/lib/index.d.ts
CHANGED
|
@@ -29,10 +29,18 @@ export type BotConfig = {
|
|
|
29
29
|
powerOffStaffOnly: boolean;
|
|
30
30
|
/** Staff on the same platform get checkout receipts privately. */
|
|
31
31
|
receiptsToStaff: boolean;
|
|
32
|
-
/**
|
|
33
|
-
|
|
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[];
|
|
34
36
|
/** How many minutes before an idle power-off to tell them; 0 for never. */
|
|
35
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;
|
|
36
44
|
};
|
|
37
45
|
/** Which shop this credential belongs to; the shop code is no longer configured by hand. */
|
|
38
46
|
shop?: {
|
package/lib/index.js
CHANGED
|
@@ -57,6 +57,8 @@ const USAGE = {
|
|
|
57
57
|
list: "/list [资源或方案]",
|
|
58
58
|
show: "/show [设备ID]",
|
|
59
59
|
};
|
|
60
|
+
/** Where `list` puts players who occupy no resource. */
|
|
61
|
+
const OTHER_PLAYERS_LABEL = "其他";
|
|
60
62
|
function applyPrismKoishiPlugin(ctx, config) {
|
|
61
63
|
const service = new PrismKoishiService(ctx, config);
|
|
62
64
|
const wrap = (handler, gate = {}) => async (context, ...args) => {
|
|
@@ -591,6 +593,8 @@ class PrismKoishiService {
|
|
|
591
593
|
watcher = null;
|
|
592
594
|
/** The bot's settings as last read; null until read, and again once the shop disables the bot. */
|
|
593
595
|
bot = null;
|
|
596
|
+
/** Announcements still being sent; each waits for the one before. */
|
|
597
|
+
announcing = Promise.resolve();
|
|
594
598
|
/** The read in flight, shared by commands arriving together. */
|
|
595
599
|
reading = null;
|
|
596
600
|
reads = 0;
|
|
@@ -621,6 +625,11 @@ class PrismKoishiService {
|
|
|
621
625
|
this.socket = new PrismSocket(open, platformSocketUrl(options.baseUrl), options.integrationToken, () => this.hello(), (event) => {
|
|
622
626
|
if (event.event === "idle.reminder")
|
|
623
627
|
void this.remindIdle(event.data);
|
|
628
|
+
// One at a time, so the channels read them in the order they happened.
|
|
629
|
+
else if (event.event === "shop.event")
|
|
630
|
+
this.announcing = this.announcing
|
|
631
|
+
.then(() => this.announce(event.data))
|
|
632
|
+
.catch((error) => this.logger?.warn("PRiSM event announcement failed", error));
|
|
624
633
|
}, this.logger, this.watcher
|
|
625
634
|
? {
|
|
626
635
|
// Only a bot whose settings allow it watches, whatever the platform asks.
|
|
@@ -700,8 +709,11 @@ class PrismKoishiService {
|
|
|
700
709
|
name: bot.bot.name,
|
|
701
710
|
// Saying so is what makes the platform consider this bot for the watch.
|
|
702
711
|
...(this.watching ? { ha: true } : {}),
|
|
703
|
-
// Without anywhere to send it,
|
|
704
|
-
|
|
712
|
+
// Without anywhere to send it, or with idle news switched off, the
|
|
713
|
+
// platform need not plan a reminder.
|
|
714
|
+
...(settings.announceIdle && settings.announceChannelIds.length && settings.idleReminderMinutes > 0
|
|
715
|
+
? { reminderLeadSeconds: settings.idleReminderMinutes * 60 }
|
|
716
|
+
: {}),
|
|
705
717
|
};
|
|
706
718
|
}
|
|
707
719
|
/** Whether the sender is one of the shop's staff on this platform. */
|
|
@@ -719,10 +731,70 @@ class PrismKoishiService {
|
|
|
719
731
|
throw error;
|
|
720
732
|
}
|
|
721
733
|
}
|
|
722
|
-
/**
|
|
734
|
+
/**
|
|
735
|
+
* Tell the configured channels what happened, if this bot announces that kind
|
|
736
|
+
* of thing. A card that put its owner on the cabinet is one message: a start
|
|
737
|
+
* when starts are announced, otherwise a card; a coin that did is the same.
|
|
738
|
+
* What this bot's own command did is left out: the bot has answered the
|
|
739
|
+
* command already.
|
|
740
|
+
*/
|
|
741
|
+
async announce(event) {
|
|
742
|
+
const bot = this.bot;
|
|
743
|
+
const channels = bot?.settings.announceChannelIds ?? [];
|
|
744
|
+
if (!bot || !channels.length || typeof this.ctx.broadcast !== "function")
|
|
745
|
+
return;
|
|
746
|
+
if (event.botId && event.botId === bot.bot.id)
|
|
747
|
+
return;
|
|
748
|
+
const message = await this.announcement(event, bot.settings);
|
|
749
|
+
if (!message)
|
|
750
|
+
return;
|
|
751
|
+
try {
|
|
752
|
+
await this.ctx.broadcast(channels, message);
|
|
753
|
+
}
|
|
754
|
+
catch (error) {
|
|
755
|
+
this.logger?.warn("PRiSM event announcement failed", error);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
async announcement(event, settings) {
|
|
759
|
+
const place = event.resourceName || event.resourceId;
|
|
760
|
+
const who = async () => {
|
|
761
|
+
if (!event.player)
|
|
762
|
+
return "未绑定的卡";
|
|
763
|
+
// Named as the platform the channels are on would name them, when it can.
|
|
764
|
+
const provider = event.player.identities[0]?.provider ?? "";
|
|
765
|
+
return this.displayNameForPlayer({
|
|
766
|
+
playerId: event.player.playerId,
|
|
767
|
+
displayName: event.player.displayName ?? undefined,
|
|
768
|
+
sessions: [{ playerDisplayName: event.player.displayName ?? undefined, identities: event.player.identities }],
|
|
769
|
+
}, provider);
|
|
770
|
+
};
|
|
771
|
+
if (event.kind === "power") {
|
|
772
|
+
if (!settings.announcePower)
|
|
773
|
+
return null;
|
|
774
|
+
if (event.by === "system")
|
|
775
|
+
return event.state === "off" ? `💤 ${place} 闲置,已自动关机` : `✅ ${place} 已开机`;
|
|
776
|
+
const by = event.player ? `(${await who()})` : "";
|
|
777
|
+
return event.state === "on" ? `✅ ${place} 已开机${by}` : `🛑 ${place} 已关机${by}`;
|
|
778
|
+
}
|
|
779
|
+
if (event.kind === "card") {
|
|
780
|
+
if (event.started && settings.announceStarts)
|
|
781
|
+
return `🎮 ${await who()} 在 ${place} 刷卡上机`;
|
|
782
|
+
return settings.announceCards ? `💳 ${await who()} 在 ${place} 刷卡` : null;
|
|
783
|
+
}
|
|
784
|
+
if (event.kind === "coin") {
|
|
785
|
+
if (event.started && settings.announceStarts)
|
|
786
|
+
return `🎮 ${await who()} 在 ${place} 投币上机`;
|
|
787
|
+
return settings.announceCoins ? `🪙 ${await who()} 在 ${place} 投币` : null;
|
|
788
|
+
}
|
|
789
|
+
if (event.kind === "occupancy")
|
|
790
|
+
return settings.announceStarts ? `🎮 ${await who()} 在 ${place} 上机` : null;
|
|
791
|
+
return null;
|
|
792
|
+
}
|
|
793
|
+
/** Tell the configured channels a power-off is coming, when the shop asks for it. */
|
|
723
794
|
async remindIdle(reminder) {
|
|
724
|
-
const
|
|
725
|
-
|
|
795
|
+
const settings = this.bot?.settings;
|
|
796
|
+
const channels = settings?.announceChannelIds ?? [];
|
|
797
|
+
if (!settings?.announceIdle || !channels.length || typeof this.ctx.broadcast !== "function")
|
|
726
798
|
return;
|
|
727
799
|
const now = (this.options.now?.() ?? new Date()).getTime();
|
|
728
800
|
const minutes = Math.max(1, Math.round((reminder.at - now) / 60_000));
|
|
@@ -765,7 +837,13 @@ class PrismKoishiService {
|
|
|
765
837
|
// Starting the plan may end one it cannot bill together with.
|
|
766
838
|
const stopped = Array.isArray(result?.billing?.stopped) ? result.billing.stopped.filter((name) => typeof name === "string" && name) : [];
|
|
767
839
|
const ended = stopped.length ? `\n结束方案:${stopped.join("、")}` : "";
|
|
768
|
-
|
|
840
|
+
const message = (isTargeted ? `✅ 已为用户 ${formatPlayerReference(sender)} 入场成功` : "✅ 入场成功") + ended;
|
|
841
|
+
// Entering and getting the door code as one command, when the shop asks
|
|
842
|
+
// for it. A door that fails must not undo an entry that succeeded.
|
|
843
|
+
const settings = (await this.config());
|
|
844
|
+
return settings.settings.doorPasswordOnEntry && settings.door
|
|
845
|
+
? `${message}\n${await this.doorCode(sender, settings.door, true)}`
|
|
846
|
+
: message;
|
|
769
847
|
}, bot);
|
|
770
848
|
}
|
|
771
849
|
async withTarget(actor, targetSubject, action, bot) {
|
|
@@ -1031,6 +1109,14 @@ class PrismKoishiService {
|
|
|
1031
1109
|
const door = (await this.config()).door;
|
|
1032
1110
|
if (!door)
|
|
1033
1111
|
return "🔑 本店还没有设置 Bot 开的门";
|
|
1112
|
+
return this.doorCode(sender, door, false);
|
|
1113
|
+
}
|
|
1114
|
+
/**
|
|
1115
|
+
* The door code, or why there is none. `onEntry` marks the copy that rides
|
|
1116
|
+
* along with a successful entry: there the failure is said in one line and
|
|
1117
|
+
* never takes the entry down with it.
|
|
1118
|
+
*/
|
|
1119
|
+
async doorCode(sender, door, onEntry) {
|
|
1034
1120
|
try {
|
|
1035
1121
|
const result = await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
|
|
1036
1122
|
type: "door.open",
|
|
@@ -1039,19 +1125,19 @@ class PrismKoishiService {
|
|
|
1039
1125
|
const failure = this.getCommandFailureMessage(result);
|
|
1040
1126
|
if (failure) {
|
|
1041
1127
|
this.logDeviceCommandFailure(result);
|
|
1042
|
-
return `🔑 开门失败,${deviceCommandFailureReason(result)}`;
|
|
1128
|
+
return onEntry ? "🔑 门锁临时密码没有生成,可以在入场后用 lock 再取一次" : `🔑 开门失败,${deviceCommandFailureReason(result)}`;
|
|
1043
1129
|
}
|
|
1044
1130
|
if (result?.action?.executorKind && result.action.executorKind !== "ttlock") {
|
|
1045
1131
|
return "🔑 门锁指令已发送";
|
|
1046
1132
|
}
|
|
1047
1133
|
const password = cleanText(result?.action?.payload?.temporaryPassword);
|
|
1048
1134
|
if (!password)
|
|
1049
|
-
return "🔑 临时门锁密码生成失败,请稍后再试";
|
|
1135
|
+
return onEntry ? "🔑 门锁临时密码没有生成,可以在入场后用 lock 再取一次" : "🔑 临时门锁密码生成失败,请稍后再试";
|
|
1050
1136
|
return `🔑 临时门锁密码:${password}\n有效期:3 分钟`;
|
|
1051
1137
|
}
|
|
1052
1138
|
catch (error) {
|
|
1053
1139
|
this.logCommandError(error);
|
|
1054
|
-
return `🔑 开门失败,${deviceActionFailureReason(error)}`;
|
|
1140
|
+
return onEntry ? "🔑 门锁临时密码没有生成,可以在入场后用 lock 再取一次" : `🔑 开门失败,${deviceActionFailureReason(error)}`;
|
|
1055
1141
|
}
|
|
1056
1142
|
}
|
|
1057
1143
|
async powerOn(sender, rawDeviceRef) {
|
|
@@ -1158,7 +1244,13 @@ class PrismKoishiService {
|
|
|
1158
1244
|
if (wanted && only && !only.players.length) {
|
|
1159
1245
|
return only.capacity !== undefined ? `${only.label} ( 0/${only.capacity} )` : `${only.label} ( 0人 )`;
|
|
1160
1246
|
}
|
|
1161
|
-
|
|
1247
|
+
if (wanted)
|
|
1248
|
+
return formatPlayerGroups(groups);
|
|
1249
|
+
// The whole shop: players on a resource are listed there, everyone else together,
|
|
1250
|
+
// whatever plan they bill. Asking for a plan by name still lists that plan.
|
|
1251
|
+
const onResources = groups.filter((group) => group.capacity !== undefined);
|
|
1252
|
+
const others = groups.filter((group) => group.capacity === undefined).flatMap((group) => group.players);
|
|
1253
|
+
return formatPlayerGroups([...onResources, ...(others.length ? [{ label: OTHER_PLAYERS_LABEL, players: others }] : [])]);
|
|
1162
1254
|
}
|
|
1163
1255
|
async listDeviceStates(rawAlias) {
|
|
1164
1256
|
const alias = cleanText(rawAlias);
|
|
@@ -1569,8 +1661,14 @@ function parseBotConfig(value) {
|
|
|
1569
1661
|
staffCommands: settings.staffCommands !== false,
|
|
1570
1662
|
powerOffStaffOnly: settings.powerOffStaffOnly === true,
|
|
1571
1663
|
receiptsToStaff: settings.receiptsToStaff === true,
|
|
1572
|
-
|
|
1664
|
+
doorPasswordOnEntry: settings.doorPasswordOnEntry === true,
|
|
1665
|
+
announceChannelIds: strings(settings.announceChannelIds),
|
|
1573
1666
|
idleReminderMinutes: Math.max(0, Number(settings.idleReminderMinutes ?? 5) || 0),
|
|
1667
|
+
announceIdle: settings.announceIdle === true,
|
|
1668
|
+
announcePower: settings.announcePower === true,
|
|
1669
|
+
announceStarts: settings.announceStarts === true,
|
|
1670
|
+
announceCards: settings.announceCards === true,
|
|
1671
|
+
announceCoins: settings.announceCoins === true,
|
|
1574
1672
|
},
|
|
1575
1673
|
billingEnabled: record.billingEnabled !== false,
|
|
1576
1674
|
currencyName: cleanText(record.currencyName) || "余额",
|