From 99b1c5148792caa31be004372d141deb51c9dcd3 Mon Sep 17 00:00:00 2001 From: "Mr.Xia" <1424473282@qq.com> Date: Fri, 3 Apr 2026 23:33:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9env=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E6=89=8B=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- danding_bot/plugins/danding_points/config.py | 12 ++-- .../plugins/group_horse_racing/config.py | 71 +++++++++++++------ 2 files changed, 55 insertions(+), 28 deletions(-) diff --git a/danding_bot/plugins/danding_points/config.py b/danding_bot/plugins/danding_points/config.py index ef82285..4851aad 100644 --- a/danding_bot/plugins/danding_points/config.py +++ b/danding_bot/plugins/danding_points/config.py @@ -1,21 +1,21 @@ from pydantic import field_validator from pydantic_settings import BaseSettings, SettingsConfigDict from pathlib import Path +import os class Config(BaseSettings): """Points system configuration.""" model_config = SettingsConfigDict( - env_prefix="DANDING_", - case_sensitive=True, extra="ignore", ) - POINTS_DB_FILE: str = "data/danding_points/points.db" - POINTS_MAX_BALANCE: int = 0 # 0 = unlimited - POINTS_MAX_PER_OPERATION: int = 0 # 0 = unlimited - POINTS_LOG_RETENTION_DAYS: int = 365 + # 数据库配置 + POINTS_DB_FILE: str = os.getenv("DANDING_POINTS_DB_FILE", "data/danding_points/points.db") + POINTS_MAX_BALANCE: int = int(os.getenv("DANDING_POINTS_MAX_BALANCE", "0")) + POINTS_MAX_PER_OPERATION: int = int(os.getenv("DANDING_POINTS_MAX_PER_OPERATION", "0")) + POINTS_LOG_RETENTION_DAYS: int = int(os.getenv("DANDING_POINTS_LOG_RETENTION_DAYS", "365")) @field_validator("POINTS_MAX_BALANCE", "POINTS_MAX_PER_OPERATION", "POINTS_LOG_RETENTION_DAYS") @classmethod diff --git a/danding_bot/plugins/group_horse_racing/config.py b/danding_bot/plugins/group_horse_racing/config.py index dbd97da..26f64e5 100644 --- a/danding_bot/plugins/group_horse_racing/config.py +++ b/danding_bot/plugins/group_horse_racing/config.py @@ -1,26 +1,30 @@ from pydantic import Field, field_validator from pydantic_settings import BaseSettings, SettingsConfigDict from typing import Union +import os +import json class Config(BaseSettings): model_config = SettingsConfigDict( extra="ignore", - env_prefix="GROUP_HORSE_RACING_", ) - TEST_MODE: bool = False + # 测试模式配置 + TEST_MODE: bool = os.getenv("GROUP_HORSE_RACING_TEST_MODE", "False").lower() == "true" TESTERS: set[int] = Field(default_factory=set) TEST_GROUPS: set[int] = Field(default_factory=set) ALLOWED_GROUPS: set[int] = Field(default_factory=set) - PARTICIPANT_REWARD: int = 50 - CHAMPION_REWARD: int = 200 - MIN_BET: int = 10 - MIN_ODDS: float = 1.2 - RACE_DISTANCE: int = 100 - RACE_TICK_INTERVAL: int = 5 + # 奖励配置 + PARTICIPANT_REWARD: int = int(os.getenv("GROUP_HORSE_RACING_PARTICIPANT_REWARD", "50")) + CHAMPION_REWARD: int = int(os.getenv("GROUP_HORSE_RACING_CHAMPION_REWARD", "200")) + MIN_BET: int = int(os.getenv("GROUP_HORSE_RACING_MIN_BET", "10")) + MIN_ODDS: float = float(os.getenv("GROUP_HORSE_RACING_MIN_ODDS", "1.2")) + RACE_DISTANCE: int = int(os.getenv("GROUP_HORSE_RACING_RACE_DISTANCE", "100")) + RACE_TICK_INTERVAL: int = int(os.getenv("GROUP_HORSE_RACING_RACE_TICK_INTERVAL", "5")) + # 消息撤回配置 MESSAGE_RECALL: dict[str, int] = Field( default_factory=lambda: { "race_update": 30, @@ -35,7 +39,42 @@ class Config(BaseSettings): } ) - RACE_DB_FILE: str = "data/group_horse_racing/race.db" + # 数据库配置 + RACE_DB_FILE: str = os.getenv("GROUP_HORSE_RACING_RACE_DB_FILE", "data/group_horse_racing/race.db") + + def __init__(self, **data): + super().__init__(**data) + # 从环境变量解析 TESTERS + testers_env = os.getenv("GROUP_HORSE_RACING_TESTERS", "") + if testers_env: + self.TESTERS = self._parse_id_set(testers_env) + + # 从环境变量解析 TEST_GROUPS + test_groups_env = os.getenv("GROUP_HORSE_RACING_TEST_GROUPS", "") + if test_groups_env: + self.TEST_GROUPS = self._parse_id_set(test_groups_env) + + # 从环境变量解析 ALLOWED_GROUPS + allowed_groups_env = os.getenv("GROUP_HORSE_RACING_ALLOWED_GROUPS", "") + if allowed_groups_env: + self.ALLOWED_GROUPS = self._parse_id_set(allowed_groups_env) + + @staticmethod + def _parse_id_set(v: str) -> set[int]: + """Parse ID sets from various formats.""" + # Handle JSON string format like "[1424473282]" + try: + parsed = json.loads(v) + if isinstance(parsed, list): + return set(int(x) for x in parsed) + except (json.JSONDecodeError, ValueError, TypeError): + pass + # Handle comma-separated format + try: + return set(int(x.strip()) for x in v.split(",") if x.strip()) + except ValueError: + pass + return set() @field_validator("TESTERS", "TEST_GROUPS", "ALLOWED_GROUPS", mode="before") @classmethod @@ -44,19 +83,7 @@ class Config(BaseSettings): if isinstance(v, set): return v if isinstance(v, str): - # Handle JSON string format like "[1424473282]" - import json - try: - parsed = json.loads(v) - if isinstance(parsed, list): - return set(int(x) for x in parsed) - except (json.JSONDecodeError, ValueError, TypeError): - pass - # Handle comma-separated format - try: - return set(int(x.strip()) for x in v.split(",") if x.strip()) - except ValueError: - pass + return cls._parse_id_set(v) if isinstance(v, (list, tuple)): return set(int(x) for x in v) return v if isinstance(v, set) else set()