This commit is contained in:
+36
-1
@@ -25,6 +25,7 @@ MAX_RPS_PLAYERS = 8
|
||||
BEIJING_TIMEZONE = ZoneInfo("Asia/Shanghai")
|
||||
DICE_PATTERN = re.compile(r"^(?:(\d{1,2})[dD](\d{1,3}))?$")
|
||||
RANDOM_NUMBER_PATTERN = re.compile(r"^(-?\d{1,6})\s+(-?\d{1,6})$")
|
||||
MULTI_REPLY_MARKER = "\x1eJWEBOT_MULTI_REPLY_V1\x1e"
|
||||
|
||||
|
||||
class LocalCommandError(RuntimeError):
|
||||
@@ -53,6 +54,36 @@ def deterministic_int(seed: str, lower: int, upper: int) -> int:
|
||||
return lower + value % (upper - lower + 1)
|
||||
|
||||
|
||||
def pack_reply_parts(parts: list[str] | tuple[str, ...]) -> str:
|
||||
cleaned = [str(part).strip() for part in parts if str(part).strip()]
|
||||
if not cleaned:
|
||||
raise LocalCommandError("本地命令没有可发送回复")
|
||||
if len(cleaned) == 1:
|
||||
return cleaned[0]
|
||||
return MULTI_REPLY_MARKER + json.dumps(
|
||||
cleaned,
|
||||
ensure_ascii=False,
|
||||
separators=(",", ":"),
|
||||
)
|
||||
|
||||
|
||||
def unpack_reply_parts(value: object) -> list[str] | None:
|
||||
text = str(value or "")
|
||||
if not text.startswith(MULTI_REPLY_MARKER):
|
||||
return None
|
||||
try:
|
||||
payload = json.loads(text[len(MULTI_REPLY_MARKER) :])
|
||||
except json.JSONDecodeError:
|
||||
return None
|
||||
if (
|
||||
not isinstance(payload, list)
|
||||
or not payload
|
||||
or any(not isinstance(item, str) or not item.strip() for item in payload)
|
||||
):
|
||||
return None
|
||||
return [item.strip() for item in payload]
|
||||
|
||||
|
||||
def local_command_parts(question: str) -> tuple[str, str]:
|
||||
normalized = question.lstrip()
|
||||
if not normalized.startswith("/"):
|
||||
@@ -131,7 +162,11 @@ class LocalCommandEngine:
|
||||
}:
|
||||
return self.reply_cleaner(self.players.handle(row, kind, arguments))
|
||||
if kind == "cs_autochess":
|
||||
return self.reply_cleaner(self.autochess.handle(row))
|
||||
parts = [
|
||||
self.reply_cleaner(part)
|
||||
for part in self.autochess.handle_parts(row)
|
||||
]
|
||||
return pack_reply_parts(parts)
|
||||
if kind == "dice":
|
||||
return self.reply_cleaner(self.dice(row, arguments))
|
||||
if kind == "dice_challenge":
|
||||
|
||||
Reference in New Issue
Block a user