"""夜间模式 handler""" import asyncio import logging from telegram import Update from telegram.ext import ContextTypes, CommandHandler, filters import config from services.message_utils import record_bot_message from services.admin_utils import require_admin from services.night_scheduler import enable_night_mode, disable_night_mode logger = logging.getLogger("spam_guard") async def cmd_night(update: Update, context: ContextTypes.DEFAULT_TYPE): """手动控制夜间模式""" if not await require_admin(update, context): return args = context.args if not args: reply = await update.message.reply_text( "🌙 夜间模式\n\n" "用法:\n" "/night on 开启\n" "/night off 关闭" ) asyncio.create_task(auto_delete(update.message, seconds=60)) await record_bot_message(reply) asyncio.create_task(auto_delete(reply, seconds=60)) return cmd = args[0].lower() if cmd == "on": await enable_night_mode(update.effective_chat) reply = await update.message.reply_text("🌙 夜间模式已开启,全员禁言") elif cmd == "off": await disable_night_mode(update.effective_chat) reply = await update.message.reply_text("☀️ 夜间模式已关闭,恢复发言") else: reply = await update.message.reply_text("❓ 用法: /night on|off") asyncio.create_task(auto_delete(update.message, seconds=60)) await record_bot_message(reply) asyncio.create_task(auto_delete(reply, seconds=60)) async def auto_delete(message, seconds=60): try: await asyncio.sleep(seconds) await message.delete() except Exception: pass def register_night_mode(app): app.add_handler(CommandHandler("night", cmd_night, filters.ChatType.GROUPS))