koishi-plugin-prism 0.1.31 → 0.1.33
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/lib/index.js +47 -8
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -641,11 +641,14 @@ class PrismKoishiService {
|
|
|
641
641
|
const { value, error } = parsePositiveInt(rawCount, "prism_coin", "数量", 1);
|
|
642
642
|
if (error)
|
|
643
643
|
return error;
|
|
644
|
-
await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
|
|
644
|
+
const result = await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
|
|
645
645
|
type: "coin",
|
|
646
646
|
target: { kind: "game_machine", id: deviceId },
|
|
647
647
|
payload: { count: value },
|
|
648
648
|
});
|
|
649
|
+
const failure = this.getCommandFailureMessage(result);
|
|
650
|
+
if (failure)
|
|
651
|
+
return `❌ 执行失败:${failure}`;
|
|
649
652
|
return `🪙 已为 ${deviceId} 投入 ${value} 个币`;
|
|
650
653
|
}
|
|
651
654
|
async scan(sender, rawDeviceId, rawSubject) {
|
|
@@ -653,11 +656,14 @@ class PrismKoishiService {
|
|
|
653
656
|
const subject = cleanText(rawSubject);
|
|
654
657
|
if (!deviceId || !subject)
|
|
655
658
|
return commandUsage("prism_scan");
|
|
656
|
-
await this.client.requestScanByIdentity(this.identity(sender), {
|
|
659
|
+
const result = await this.client.requestScanByIdentity(this.identity(sender), {
|
|
657
660
|
deviceId,
|
|
658
661
|
provider: this.config.defaultScanProvider || "aime",
|
|
659
662
|
subject,
|
|
660
663
|
});
|
|
664
|
+
const failure = this.getCommandFailureMessage(result);
|
|
665
|
+
if (failure)
|
|
666
|
+
return `❌ 执行失败:${failure}`;
|
|
661
667
|
return `💳 使用尾号为 ${subject.slice(-4)} 的卡刷卡成功`;
|
|
662
668
|
}
|
|
663
669
|
async redeem(sender, rawCode) {
|
|
@@ -683,6 +689,28 @@ class PrismKoishiService {
|
|
|
683
689
|
this.mergeWaitingSeats(groups);
|
|
684
690
|
return formatPlayerGroups(groups, this.config.mahjongTableSize ?? 4, this.config.mahjongLabelPrefix ?? "麻将桌");
|
|
685
691
|
}
|
|
692
|
+
parseDeviceStateValue(stateVal) {
|
|
693
|
+
if (!stateVal)
|
|
694
|
+
return "unknown";
|
|
695
|
+
if (typeof stateVal === "string") {
|
|
696
|
+
try {
|
|
697
|
+
const decoded = JSON.parse(stateVal);
|
|
698
|
+
if (typeof decoded === "string") {
|
|
699
|
+
return decoded;
|
|
700
|
+
}
|
|
701
|
+
if (decoded && typeof decoded.state === "string") {
|
|
702
|
+
return decoded.state;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
catch {
|
|
706
|
+
return stateVal;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
if (typeof stateVal === "object" && stateVal !== null) {
|
|
710
|
+
return stateVal.state ?? "unknown";
|
|
711
|
+
}
|
|
712
|
+
return "unknown";
|
|
713
|
+
}
|
|
686
714
|
async listDeviceStates(rawAlias) {
|
|
687
715
|
const alias = cleanText(rawAlias);
|
|
688
716
|
const result = (await this.client.listDeviceStates());
|
|
@@ -693,11 +721,11 @@ class PrismKoishiService {
|
|
|
693
721
|
const matched = states.find((d) => d.deviceId === alias || d.label === alias);
|
|
694
722
|
if (!matched)
|
|
695
723
|
return `找不到设备: ${alias}`;
|
|
696
|
-
const stateVal = matched.state
|
|
724
|
+
const stateVal = this.parseDeviceStateValue(matched.state);
|
|
697
725
|
return `${matched.label || matched.deviceId}: ${stateVal}`;
|
|
698
726
|
}
|
|
699
727
|
return states
|
|
700
|
-
.map((d) => `${d.label || d.deviceId}: ${d.state
|
|
728
|
+
.map((d) => `${d.label || d.deviceId}: ${this.parseDeviceStateValue(d.state)}`)
|
|
701
729
|
.join("\n");
|
|
702
730
|
}
|
|
703
731
|
async autoPowerOffLoop() {
|
|
@@ -710,7 +738,7 @@ class PrismKoishiService {
|
|
|
710
738
|
return;
|
|
711
739
|
const statesResult = (await this.client.listDeviceStates());
|
|
712
740
|
const states = (statesResult?.deviceStates ?? []);
|
|
713
|
-
const anyOn = states.some((d) => d.state
|
|
741
|
+
const anyOn = states.some((d) => this.parseDeviceStateValue(d.state) !== "off");
|
|
714
742
|
if (!anyOn)
|
|
715
743
|
return;
|
|
716
744
|
const dummySender = { id: "system", name: "system" };
|
|
@@ -718,16 +746,27 @@ class PrismKoishiService {
|
|
|
718
746
|
}
|
|
719
747
|
/* ---------------------------- helpers ---------------------------------- */
|
|
720
748
|
async power(sender, deviceId, state) {
|
|
721
|
-
await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
|
|
749
|
+
const result = await this.client.requestDeviceCommandByIdentity(this.identity(sender), {
|
|
722
750
|
type: state === "on" ? "power.on" : "power.off",
|
|
723
751
|
target: { kind: "facility", id: deviceId },
|
|
724
752
|
payload: { state },
|
|
725
753
|
});
|
|
754
|
+
const failure = this.getCommandFailureMessage(result);
|
|
755
|
+
if (failure)
|
|
756
|
+
return `❌ 执行失败:${failure}`;
|
|
757
|
+
const displayName = result?.action?.payload?.deviceLabel || deviceId;
|
|
726
758
|
if (state === "on")
|
|
727
|
-
return `✅ ${
|
|
759
|
+
return `✅ ${displayName} 启动成功`;
|
|
728
760
|
if (deviceId === "all")
|
|
729
761
|
return `🛑 全部机器关闭成功`;
|
|
730
|
-
return `🛑 ${
|
|
762
|
+
return `🛑 ${displayName} 关闭成功`;
|
|
763
|
+
}
|
|
764
|
+
getCommandFailureMessage(result) {
|
|
765
|
+
const action = result?.action;
|
|
766
|
+
if (action?.status === "expired" || action?.status === "rejected") {
|
|
767
|
+
return action.payload?.executorFailure?.message || "命令执行被拒绝或超时";
|
|
768
|
+
}
|
|
769
|
+
return null;
|
|
731
770
|
}
|
|
732
771
|
async resolvePlatformName(subject) {
|
|
733
772
|
if (!this.config.resolveDisplayName)
|