重构机器人
This commit is contained in:
96
bot/handlers/settings.py
Normal file
96
bot/handlers/settings.py
Normal file
@@ -0,0 +1,96 @@
|
||||
"""
|
||||
/set 命令处理器 - 用户设置
|
||||
"""
|
||||
|
||||
from telegram import Update
|
||||
from telegram.ext import ContextTypes
|
||||
|
||||
from bot.middlewares.auth import require_auth
|
||||
|
||||
|
||||
DEFAULT_WORKERS = 2
|
||||
MIN_WORKERS = 1
|
||||
MAX_WORKERS = 5
|
||||
|
||||
|
||||
def get_user_settings(context: ContextTypes.DEFAULT_TYPE, user_id: int) -> dict:
|
||||
"""获取用户设置"""
|
||||
if "user_settings" not in context.bot_data:
|
||||
context.bot_data["user_settings"] = {}
|
||||
|
||||
if user_id not in context.bot_data["user_settings"]:
|
||||
context.bot_data["user_settings"][user_id] = {
|
||||
"workers": DEFAULT_WORKERS,
|
||||
}
|
||||
|
||||
return context.bot_data["user_settings"][user_id]
|
||||
|
||||
|
||||
def set_user_setting(
|
||||
context: ContextTypes.DEFAULT_TYPE, user_id: int, key: str, value: any
|
||||
) -> None:
|
||||
"""设置用户配置"""
|
||||
settings = get_user_settings(context, user_id)
|
||||
settings[key] = value
|
||||
|
||||
|
||||
@require_auth
|
||||
async def set_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
"""
|
||||
处理 /set 命令 - 用户设置
|
||||
|
||||
用法:
|
||||
/set - 查看当前设置
|
||||
/set workers <数量> - 设置并发数 (1-5)
|
||||
"""
|
||||
user_id = update.effective_user.id
|
||||
settings = get_user_settings(context, user_id)
|
||||
|
||||
# 无参数:显示当前设置
|
||||
if not context.args:
|
||||
await update.message.reply_text(
|
||||
f"⚙️ **当前设置**\n\n"
|
||||
f"👷 并发数: {settings['workers']}\n\n"
|
||||
f"使用 `/set workers <数量>` 修改并发数 ({MIN_WORKERS}-{MAX_WORKERS})",
|
||||
parse_mode="Markdown",
|
||||
)
|
||||
return
|
||||
|
||||
# 解析设置项
|
||||
setting_name = context.args[0].lower()
|
||||
|
||||
if setting_name == "workers":
|
||||
if len(context.args) < 2:
|
||||
await update.message.reply_text(
|
||||
f"❌ 请指定并发数\n\n"
|
||||
f"用法: `/set workers <数量>`\n"
|
||||
f"范围: {MIN_WORKERS}-{MAX_WORKERS}",
|
||||
parse_mode="Markdown",
|
||||
)
|
||||
return
|
||||
|
||||
try:
|
||||
workers = int(context.args[1])
|
||||
if workers < MIN_WORKERS:
|
||||
workers = MIN_WORKERS
|
||||
elif workers > MAX_WORKERS:
|
||||
workers = MAX_WORKERS
|
||||
|
||||
set_user_setting(context, user_id, "workers", workers)
|
||||
|
||||
await update.message.reply_text(
|
||||
f"✅ 并发数已设置为 **{workers}**",
|
||||
parse_mode="Markdown",
|
||||
)
|
||||
except ValueError:
|
||||
await update.message.reply_text(
|
||||
f"❌ 无效的数值: `{context.args[1]}`",
|
||||
parse_mode="Markdown",
|
||||
)
|
||||
else:
|
||||
await update.message.reply_text(
|
||||
f"❌ 未知设置项: `{setting_name}`\n\n"
|
||||
f"可用设置:\n"
|
||||
f"• `workers` - 并发数 ({MIN_WORKERS}-{MAX_WORKERS})",
|
||||
parse_mode="Markdown",
|
||||
)
|
||||
Reference in New Issue
Block a user