Files
MusicBot/musicbot/config.py
T
snowy-yang 9bcdad7aa1 新增日志支持、卡片显示专辑封面、菜单去掉音源标注
- 日志:控制台 + logs/musicbot.log(按天轮转、保留 7 天),
  LOG_LEVEL/LOG_DIR 可配;目录不可写时退化为仅控制台;
  diagnose=False 避免把凭证写进日志
- 卡片用 Markdown 图片语法显示专辑封面(![封面 #240px #240px](img)),
  封面取自 MusicServer 的 img 字段(tx/wy 有、kw/kg 无)
- 按钮 data 加第 5 段(封面 URL),解码兼容旧 4 段,已发出的按钮不失效
- 菜单去掉每首歌后的(音源)标注;卡片保留"来源:"
- 测试补:封面渲染、无封面源、旧 data 兼容、LOG_LEVEL、日志落盘
2026-09-20 22:36:40 +08:00

61 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Bot 全局配置:启动时从环境变量一次性读齐。"""
import os
from dataclasses import dataclass
from pathlib import Path
# 内嵌键盘上限 5 行 × 每行 5 个;歌曲占行 + 1 行翻页 → 每页最多 20 首
_MAX_MENU_PAGE_SIZE = 20
_LOG_LEVELS = ("TRACE", "DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL")
def _flag(name: str, default: bool) -> bool:
"""读布尔环境变量:on/true/1/yes 为真,off/false/0/no 为假。"""
raw = os.getenv(name, "").strip().lower()
if not raw:
return default
return raw in ("1", "true", "on", "yes")
def _send_mode() -> str:
"""SEND_MODE:下载发送方式。
file 先上传群文件、失败回退直链;link 直接发直链(跳过上传,可避开
"仅群主和管理员上传文件" 的 40093005)。非法值按默认 link 处理。
"""
raw = os.getenv("SEND_MODE", "").strip().lower()
return raw if raw in ("file", "link") else "link"
@dataclass(frozen=True, slots=True)
class BotConfig:
music_server_url: str
music_quality: str
music_source: str
menu_page_size: int = 10 # 选歌菜单每页候选数(1~20)
withdraw: bool = True # 自动撤回旧菜单/卡片是否开启
send_mode: str = "link" # 下载发送方式:"file" 先传文件 / "link" 直接发直链
log_level: str = "INFO" # 控制台与文件日志级别
log_dir: Path = Path("logs") # 日志目录;写不进去时自动退化为仅控制台
@classmethod
def load(cls) -> "BotConfig":
page_size = int(os.getenv("MENU_PAGE_SIZE", "10"))
return cls(
music_server_url=os.getenv("MUSIC_SERVER_URL", "http://127.0.0.1:16688"),
music_quality=os.getenv("MUSIC_QUALITY", "128k"),
music_source=os.getenv("MUSIC_SOURCE", "tx"),
menu_page_size=min(max(1, page_size), _MAX_MENU_PAGE_SIZE),
withdraw=_flag("WITHDRAW", True),
send_mode=_send_mode(),
log_level=_log_level(),
log_dir=Path(os.getenv("LOG_DIR", "logs")),
)
def _log_level() -> str:
"""LOG_LEVEL:日志级别,非法值按 INFO。"""
raw = os.getenv("LOG_LEVEL", "").strip().upper()
return raw if raw in _LOG_LEVELS else "INFO"