Files
MusicBot/musicbot/config.py
T
snowy-yang 7a8adb4571 QQ 群点歌 bot:选歌菜单 + 歌曲卡片 + 回调按钮
基于 qqbotsdk 的群点歌机器人:

- 点歌 <歌名>:Markdown 选歌菜单,每行 5 个编号回调按钮 + 上下页
  (每页 MENU_PAGE_SIZE 首,默认 10,受内嵌键盘 5 行限制上限 20)
- 点编号弹歌曲卡片:[▶播放] 发语音 / [⬇下载] 发文件或直链(SEND_MODE)
- 支持不 @ 机器人也能触发(GROUP_MESSAGE_CREATE 全量模式),
  按 msg_id 排重并拦截机器人自发消息回推(self-echo)
- 直链候选源回退:tx 等源受 CDN 风控时按 kw/wy/mg 逐个换源
- 自动撤回(WITHDRAW)翻页撤旧菜单、选歌撤菜单、点击后撤卡片
2026-09-20 22:27:09 +08:00

48 lines
1.7 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
# 内嵌键盘上限 5 行 × 每行 5 个;歌曲占行 + 1 行翻页 → 每页最多 20 首
_MAX_MENU_PAGE_SIZE = 20
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" 直接发直链
@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(),
)