From 6cef9c23f0a84c09b0170020b2077921871b910c Mon Sep 17 00:00:00 2001 From: anka <1350989414@qq.com> Date: Mon, 31 Mar 2025 22:41:23 +0800 Subject: [PATCH] =?UTF-8?q?bug=20fix:=20#1074=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=9C=80=E5=A4=9A=E6=90=BA=E5=B8=A6=E5=AF=B9=E8=AF=9D=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E6=97=B6=E5=87=BA=E7=8E=B0bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/dashboard/routes/config.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/astrbot/dashboard/routes/config.py b/astrbot/dashboard/routes/config.py index d3079f0d2..629a424f1 100644 --- a/astrbot/dashboard/routes/config.py +++ b/astrbot/dashboard/routes/config.py @@ -12,8 +12,11 @@ from astrbot.core import logger def try_cast(value: str, type_: str): - if type_ == "int" and value.isdigit(): - return int(value) + if type_ == "int": + try: + return int(value) + except (ValueError, TypeError): + return None elif ( type_ == "float" and isinstance(value, str) @@ -22,6 +25,11 @@ def try_cast(value: str, type_: str): return float(value) elif type_ == "float" and isinstance(value, int): return float(value) + elif type_ == "float": + try: + return float(value) + except (ValueError, TypeError): + return None def validate_config( @@ -34,13 +42,21 @@ def validate_config( if key not in metadata: # 无 schema 的配置项,执行类型猜测 if isinstance(value, str): - if value.isdigit(): + try: data[key] = int(value) - elif value.replace(".", "", 1).isdigit(): + continue + except ValueError: + pass + + try: data[key] = float(value) - elif value == "true": + continue + except ValueError: + pass + + if value.lower() == "true": data[key] = True - elif value == "false": + elif value.lower() == "false": data[key] = False continue meta = metadata[key]