koishi-plugin-prism 0.1.35 → 0.1.37
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 +6 -2
- package/lib/index.d.ts +6 -1
- package/lib/index.js +89 -32
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
* 🎟️ **礼物兑换码**:使用 `/redeem <code>` 兑换系统发放的福利礼包。
|
|
14
14
|
* 🛠️ **管理员快捷指令**:允许管理员为指定平台用户增加或扣除余额,并覆盖结账金额后立即结账。
|
|
15
15
|
|
|
16
|
+
所有技术异常在聊天中只显示通用失败提示,不会包含后端名称、URL、网络状态、原始 `fetch` 错误或 HTTP 状态。完整原始异常会写入 Koishi 的 `prism` 日志,供管理员排查。
|
|
17
|
+
|
|
16
18
|
## ⚙️ 配置说明
|
|
17
19
|
|
|
18
20
|
在 Koishi WebUI 的插件配置页面中,填入以下选项:
|
|
@@ -31,6 +33,7 @@
|
|
|
31
33
|
| `resolveDisplayName` | `function` | - | 可选。自定义用于获取群内昵称作为玩家注册名的异步逻辑。 |
|
|
32
34
|
| `enableStaffCommands` | `boolean` | `false` | 是否开启管理员快捷指令。 |
|
|
33
35
|
| `staffUserIds` | `string[]` | `[]` | 允许执行管理员快捷指令的平台用户 ID(如 QQ 号)白名单。空列表不授予目标用户操作权限。 |
|
|
36
|
+
| `powerCommandsAdminOnly` | `boolean` | `false` | 开关机是否仅允许 `staffUserIds` 中的管理员使用。关闭时,`/on` 和 `/off` 仍只允许已入场玩家使用。 |
|
|
34
37
|
| `logoutNotifyUserIds` | `string[]` | `[]` | 结账成功后额外私聊完整账单的平台用户 ID。通知收件人为该列表与 `staffUserIds` 的去重并集。 |
|
|
35
38
|
| `mahjongTableConfigs` | `object[]` | `[]` | 推荐的结构化麻将桌列表。每项填写显示名称、命令别名列表与计费方案 ID 列表。显示名称同时作为内部桌位锚点与 session 标签。 |
|
|
36
39
|
|
|
@@ -66,10 +69,11 @@ pricingConfigIds: [pricing-mahjong-a]
|
|
|
66
69
|
* `items` - 查看当前玩家持有的道具或资产。
|
|
67
70
|
* `list` - 查看当前在线/在店游玩玩家的列表,按 session 标签分组并对同一玩家去重;存在非音乐游戏 session 时取最新的非音乐标签,麻将桌显示当前人数和容量。已开局桌位由后端 session 恢复;未满桌候座由机器人进程暂存,机器人重启后不会保留。
|
|
68
71
|
* `show [deviceId]` - 查看设备电源与连接状态。
|
|
72
|
+
设备状态按后端的普通字符串契约显示;插件仍兼容历史数据库中的 JSON 字符串状态,避免旧记录显示为 `unknown`。
|
|
69
73
|
* `history` - 查看自己的历史游玩记录。
|
|
70
74
|
* `lock` - 发送开门指令。
|
|
71
|
-
* `on <deviceRef>` - 使用后台设备名称、别名或 `all`
|
|
72
|
-
* `off <deviceRef>` - 使用后台设备名称、别名或 `all`
|
|
75
|
+
* `on <deviceRef>` - 使用后台设备名称、别名或 `all` 请求启动电源;仅已通过 `login`/`入场` 开启活跃计费 session 的玩家可用。不接受 Home Assistant entity ID,成功回复使用后端返回的设备名称。
|
|
76
|
+
* `off <deviceRef>` - 使用后台设备名称、别名或 `all` 请求关闭电源;与 `/on` 一样仅允许已入场玩家使用,不接受 Home Assistant entity ID,`all` 显示为“所有设备”。
|
|
73
77
|
* `coin <deviceId> [count]` - 请求向指定设备投币指定枚数。
|
|
74
78
|
* `scan <deviceId> <subject>` - 请求向设备发送模拟刷卡。
|
|
75
79
|
* `redeem <code>` - 兑换礼物码。
|
package/lib/index.d.ts
CHANGED
|
@@ -7,7 +7,8 @@ export declare class PrismBotClientError extends Error {
|
|
|
7
7
|
readonly code: string;
|
|
8
8
|
readonly status: number;
|
|
9
9
|
readonly body: unknown;
|
|
10
|
-
|
|
10
|
+
readonly originalError?: unknown | undefined;
|
|
11
|
+
constructor(message: string, code: string, status: number, body: unknown, originalError?: unknown | undefined);
|
|
11
12
|
}
|
|
12
13
|
export type MahjongTableConfig = {
|
|
13
14
|
tableId: string;
|
|
@@ -26,6 +27,7 @@ export type PrismKoishiPluginConfig = {
|
|
|
26
27
|
currencyName: string;
|
|
27
28
|
enableStaffCommands?: boolean;
|
|
28
29
|
staffUserIds?: string[];
|
|
30
|
+
powerCommandsAdminOnly?: boolean;
|
|
29
31
|
logoutNotifyUserIds?: string[];
|
|
30
32
|
mahjongTableConfigs?: MahjongTableConfigInput[];
|
|
31
33
|
mahjongTableSize?: number;
|
|
@@ -53,6 +55,9 @@ export type KoishiCommandBuilder = {
|
|
|
53
55
|
export type KoishiLikeContext = {
|
|
54
56
|
command(name: string, description: string): KoishiCommandBuilder;
|
|
55
57
|
setInterval?(handler: () => Promise<void> | void, intervalMs: number): void;
|
|
58
|
+
logger?(name: string): {
|
|
59
|
+
warn(...args: unknown[]): void;
|
|
60
|
+
};
|
|
56
61
|
};
|
|
57
62
|
export type KoishiActionContext = {
|
|
58
63
|
session: {
|
package/lib/index.js
CHANGED
|
@@ -24,6 +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
28
|
logoutNotifyUserIds: koishi_1.Schema.array(koishi_1.Schema.string()).default([]).description("结账账单私聊通知的平台用户ID列表"),
|
|
28
29
|
powerOffInterval: koishi_1.Schema.number().default(0).description("无人自动关机等待秒数 (0为禁用)"),
|
|
29
30
|
mahjongTableConfigs: koishi_1.Schema.array(koishi_1.Schema.object({
|
|
@@ -40,11 +41,13 @@ class PrismBotClientError extends Error {
|
|
|
40
41
|
code;
|
|
41
42
|
status;
|
|
42
43
|
body;
|
|
43
|
-
|
|
44
|
+
originalError;
|
|
45
|
+
constructor(message, code, status, body, originalError) {
|
|
44
46
|
super(message);
|
|
45
47
|
this.code = code;
|
|
46
48
|
this.status = status;
|
|
47
49
|
this.body = body;
|
|
50
|
+
this.originalError = originalError;
|
|
48
51
|
this.name = "PrismBotClientError";
|
|
49
52
|
}
|
|
50
53
|
}
|
|
@@ -154,12 +157,15 @@ class PrismApiClient {
|
|
|
154
157
|
return response;
|
|
155
158
|
}
|
|
156
159
|
catch (error) {
|
|
157
|
-
if (error
|
|
158
|
-
const body = error.response.data;
|
|
159
|
-
const err = body.error
|
|
160
|
-
|
|
160
|
+
if (error?.response) {
|
|
161
|
+
const body = error.response.data ?? {};
|
|
162
|
+
const err = typeof body === "object" && body !== null && typeof body.error === "object"
|
|
163
|
+
? body.error
|
|
164
|
+
: {};
|
|
165
|
+
const status = Number(error.response.status) || 500;
|
|
166
|
+
throw new PrismBotClientError(typeof err.message === "string" ? err.message : `Backend request failed with HTTP ${status}.`, typeof err.code === "string" ? err.code : `HTTP_${status}`, status, body, error);
|
|
161
167
|
}
|
|
162
|
-
throw new PrismBotClientError(
|
|
168
|
+
throw new PrismBotClientError("Unable to reach PRiSM backend.", "API_UNREACHABLE", 0, {}, error);
|
|
163
169
|
}
|
|
164
170
|
}
|
|
165
171
|
identityBody(identity) {
|
|
@@ -240,11 +246,12 @@ class PrismApiClient {
|
|
|
240
246
|
},
|
|
241
247
|
});
|
|
242
248
|
}
|
|
243
|
-
async requestDeviceCommandByIdentity(identity, command) {
|
|
249
|
+
async requestDeviceCommandByIdentity(identity, command, options) {
|
|
244
250
|
return this.request("POST", "/rpc/integration/players/by-identity/device-actions", {
|
|
245
251
|
token: this.config.integrationToken,
|
|
246
252
|
body: {
|
|
247
253
|
...this.identityBody(identity),
|
|
254
|
+
...(options?.staffOverride ? { staffOverride: true } : {}),
|
|
248
255
|
target: command.target,
|
|
249
256
|
action: {
|
|
250
257
|
type: command.type,
|
|
@@ -307,8 +314,10 @@ class PrismKoishiService {
|
|
|
307
314
|
mahjongTables = new Map();
|
|
308
315
|
logoutInFlight = new Map();
|
|
309
316
|
client;
|
|
317
|
+
logger;
|
|
310
318
|
constructor(ctx, config) {
|
|
311
319
|
this.config = config;
|
|
320
|
+
this.logger = ctx.logger?.("prism");
|
|
312
321
|
if (config.client) {
|
|
313
322
|
this.client = config.client;
|
|
314
323
|
}
|
|
@@ -720,11 +729,11 @@ class PrismKoishiService {
|
|
|
720
729
|
const matched = states.find((d) => d.deviceId === alias || d.label === alias);
|
|
721
730
|
if (!matched)
|
|
722
731
|
return `找不到设备: ${alias}`;
|
|
723
|
-
const stateVal = matched.state
|
|
732
|
+
const stateVal = normalizeDeviceState(matched.state);
|
|
724
733
|
return `${matched.label || matched.deviceId}: ${stateVal}`;
|
|
725
734
|
}
|
|
726
735
|
return states
|
|
727
|
-
.map((d) => `${d.label || d.deviceId}: ${d.state
|
|
736
|
+
.map((d) => `${d.label || d.deviceId}: ${normalizeDeviceState(d.state)}`)
|
|
728
737
|
.join("\n");
|
|
729
738
|
}
|
|
730
739
|
async autoPowerOffLoop() {
|
|
@@ -737,24 +746,35 @@ class PrismKoishiService {
|
|
|
737
746
|
return;
|
|
738
747
|
const statesResult = (await this.client.listDeviceStates());
|
|
739
748
|
const states = (statesResult?.deviceStates ?? []);
|
|
740
|
-
const anyOn = states.some((d) => d.state
|
|
749
|
+
const anyOn = states.some((d) => normalizeDeviceState(d.state) === "on");
|
|
741
750
|
if (!anyOn)
|
|
742
751
|
return;
|
|
743
752
|
const dummySender = { id: "system", name: "system" };
|
|
744
|
-
await this.
|
|
753
|
+
await this.power(dummySender, "all", "off", true);
|
|
745
754
|
}
|
|
746
755
|
/* ---------------------------- helpers ---------------------------------- */
|
|
747
|
-
async power(sender, deviceRef, state) {
|
|
748
|
-
const
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
756
|
+
async power(sender, deviceRef, state, systemOverride = false) {
|
|
757
|
+
const operation = state === "on" ? "启动" : "关闭";
|
|
758
|
+
const staffOverride = systemOverride || this.config.powerCommandsAdminOnly === true;
|
|
759
|
+
if (!systemOverride && staffOverride && !(this.config.staffUserIds ?? []).includes(sender.id)) {
|
|
760
|
+
return `${operation}失败,权限不足`;
|
|
761
|
+
}
|
|
762
|
+
try {
|
|
763
|
+
const result = await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
|
|
764
|
+
type: state === "on" ? "power.on" : "power.off",
|
|
765
|
+
target: { kind: "facility", ref: deviceRef },
|
|
766
|
+
payload: { state },
|
|
767
|
+
}, staffOverride ? { staffOverride: true } : undefined);
|
|
768
|
+
const failure = this.getCommandFailureMessage(result);
|
|
769
|
+
if (failure)
|
|
770
|
+
return `${operation}失败,${failure}`;
|
|
771
|
+
const deviceLabel = result.action.payload.deviceLabel;
|
|
772
|
+
return state === "on" ? `✅ ${deviceLabel} 启动成功` : `🛑 ${deviceLabel} 关闭成功`;
|
|
773
|
+
}
|
|
774
|
+
catch (error) {
|
|
775
|
+
this.logCommandError(error);
|
|
776
|
+
return `${operation}失败,${powerFailureReason(error)}`;
|
|
777
|
+
}
|
|
758
778
|
}
|
|
759
779
|
getCommandFailureMessage(result) {
|
|
760
780
|
const action = result?.action;
|
|
@@ -1058,13 +1078,16 @@ class PrismKoishiService {
|
|
|
1058
1078
|
return lines.join("\n");
|
|
1059
1079
|
}
|
|
1060
1080
|
handleCommandError(error) {
|
|
1081
|
+
this.logCommandError(error);
|
|
1061
1082
|
if (error instanceof PrismBotClientError) {
|
|
1062
1083
|
return humanReadableBotError(error);
|
|
1063
1084
|
}
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1085
|
+
return genericCommandFailure();
|
|
1086
|
+
}
|
|
1087
|
+
logCommandError(error) {
|
|
1088
|
+
this.logger?.warn("PRiSM Bot command failed", error instanceof PrismBotClientError
|
|
1089
|
+
? (error.originalError ?? error)
|
|
1090
|
+
: error);
|
|
1068
1091
|
}
|
|
1069
1092
|
}
|
|
1070
1093
|
function humanReadableBotError(error) {
|
|
@@ -1077,25 +1100,59 @@ function humanReadableBotError(error) {
|
|
|
1077
1100
|
if (error.code === "ACTIVE_SESSION_NOT_FOUND") {
|
|
1078
1101
|
return "您当前没有进行中的计费场次。";
|
|
1079
1102
|
}
|
|
1103
|
+
if (error.code === "DEVICE_COMMAND_REQUIRES_ACTIVE_SESSION") {
|
|
1104
|
+
return "操作失败,玩家未入场";
|
|
1105
|
+
}
|
|
1080
1106
|
if (error.code === "PLAYER_IDENTITY_NOT_FOUND") {
|
|
1081
1107
|
return "未找到您的玩家身份,请先注册或绑定账号。";
|
|
1082
1108
|
}
|
|
1083
1109
|
if (error.code === "INSUFFICIENT_BALANCE") {
|
|
1084
1110
|
return "余额不足,暂时不能结账。请先充值,或由店员在后台改价后再结账。";
|
|
1085
1111
|
}
|
|
1086
|
-
if (error.code === "API_UNREACHABLE" || error.code === "HTTP_0") {
|
|
1087
|
-
return
|
|
1112
|
+
if (error.code === "API_UNREACHABLE" || error.code === "NETWORK_ERROR" || error.code === "HTTP_0") {
|
|
1113
|
+
return genericCommandFailure();
|
|
1088
1114
|
}
|
|
1089
1115
|
if (error.code === "API_TIMEOUT") {
|
|
1090
|
-
return
|
|
1116
|
+
return genericCommandFailure();
|
|
1091
1117
|
}
|
|
1092
1118
|
if (error.code === "INVALID_JSON_RESPONSE") {
|
|
1093
|
-
return
|
|
1119
|
+
return genericCommandFailure();
|
|
1094
1120
|
}
|
|
1095
1121
|
if (error.code === "STAFF_TOKEN_REQUIRED") {
|
|
1096
|
-
return
|
|
1122
|
+
return genericCommandFailure();
|
|
1123
|
+
}
|
|
1124
|
+
return genericCommandFailure();
|
|
1125
|
+
}
|
|
1126
|
+
function powerFailureReason(error) {
|
|
1127
|
+
if (error instanceof PrismBotClientError) {
|
|
1128
|
+
if (error.code === "DEVICE_COMMAND_REQUIRES_ACTIVE_SESSION")
|
|
1129
|
+
return "玩家未入场";
|
|
1130
|
+
if (error.code === "PLAYER_IDENTITY_NOT_FOUND")
|
|
1131
|
+
return "玩家未注册";
|
|
1132
|
+
return "请稍后再试";
|
|
1133
|
+
}
|
|
1134
|
+
return "请稍后再试";
|
|
1135
|
+
}
|
|
1136
|
+
function genericCommandFailure() {
|
|
1137
|
+
return "操作失败,请稍后再试。";
|
|
1138
|
+
}
|
|
1139
|
+
function normalizeDeviceState(value) {
|
|
1140
|
+
if (typeof value === "object" && value !== null) {
|
|
1141
|
+
return cleanText(value.state) || "unknown";
|
|
1142
|
+
}
|
|
1143
|
+
const raw = cleanText(value);
|
|
1144
|
+
if (!raw)
|
|
1145
|
+
return "unknown";
|
|
1146
|
+
if (raw.startsWith("{")) {
|
|
1147
|
+
try {
|
|
1148
|
+
const decoded = JSON.parse(raw);
|
|
1149
|
+
return cleanText(decoded?.state) || "unknown";
|
|
1150
|
+
}
|
|
1151
|
+
catch {
|
|
1152
|
+
return raw;
|
|
1153
|
+
}
|
|
1097
1154
|
}
|
|
1098
|
-
return
|
|
1155
|
+
return raw;
|
|
1099
1156
|
}
|
|
1100
1157
|
function resolveMahjongTableConfigs(structured) {
|
|
1101
1158
|
const tables = new Map();
|