fix: auto delete idle panel after timeout
This commit is contained in:
+40
-9
@@ -13,8 +13,38 @@ from services.game_service import (
|
||||
)
|
||||
|
||||
logger = logging.getLogger("spam_guard")
|
||||
PANEL_IDLE_SECONDS = 60
|
||||
|
||||
# 关键词触发
|
||||
def panel_keyboard():
|
||||
return InlineKeyboardMarkup([
|
||||
[InlineKeyboardButton("👤 我的信息", callback_data="panel:me"), InlineKeyboardButton("✅ 签到", callback_data="panel:checkin")],
|
||||
[InlineKeyboardButton("🎁 抽奖", callback_data="panel:lottery"), InlineKeyboardButton("🛒 商城", callback_data="panel:shop")],
|
||||
[InlineKeyboardButton("📖 帮助", callback_data="panel:help")],
|
||||
])
|
||||
|
||||
|
||||
def schedule_panel_idle_delete(context: ContextTypes.DEFAULT_TYPE, message, seconds: int = PANEL_IDLE_SECONDS):
|
||||
tasks = context.bot_data.setdefault("panel_idle_tasks", {})
|
||||
key = (message.chat_id, message.message_id)
|
||||
old_task = tasks.pop(key, None)
|
||||
if old_task and not old_task.done():
|
||||
old_task.cancel()
|
||||
|
||||
async def _delete_later():
|
||||
try:
|
||||
await asyncio.sleep(seconds)
|
||||
await message.delete()
|
||||
except asyncio.CancelledError:
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
tasks.pop(key, None)
|
||||
|
||||
tasks[key] = asyncio.create_task(_delete_later())
|
||||
|
||||
|
||||
TRIGGER_REACTIONS = {
|
||||
"谢谢": "👍❤️",
|
||||
"感谢": "👍❤️🎉",
|
||||
@@ -154,15 +184,12 @@ async def cmd_buy(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
|
||||
|
||||
async def cmd_panel(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
keyboard = InlineKeyboardMarkup([
|
||||
[InlineKeyboardButton("👤 我的信息", callback_data="panel:me"), InlineKeyboardButton("✅ 签到", callback_data="panel:checkin")],
|
||||
[InlineKeyboardButton("🎁 抽奖", callback_data="panel:lottery"), InlineKeyboardButton("🛒 商城", callback_data="panel:shop")],
|
||||
[InlineKeyboardButton("📖 帮助", callback_data="panel:help")],
|
||||
])
|
||||
reply = await update.message.reply_text("🎛️ 操作面板\n请选择要执行的操作:", reply_markup=keyboard)
|
||||
reply = await update.message.reply_text(
|
||||
"🎛️ 操作面板\n请选择要执行的操作:\n\n⏱️ 60 秒无操作后自动消失。",
|
||||
reply_markup=panel_keyboard(),
|
||||
)
|
||||
asyncio.create_task(auto_delete(update.message, seconds=60))
|
||||
asyncio.create_task(auto_delete(reply, seconds=180))
|
||||
|
||||
schedule_panel_idle_delete(context, reply)
|
||||
|
||||
async def handle_panel_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
query = update.callback_query
|
||||
@@ -183,7 +210,11 @@ async def handle_panel_callback(update: Update, context: ContextTypes.DEFAULT_TY
|
||||
text = f"👤 我的积分\n总积分:{summary['total']} 分\n" + "\n".join([f"{s.game_type}: {s.score} 分" for s in summary['scores']])
|
||||
else:
|
||||
text = "📖 常用:/me /checkin /trivia /lottery /shop /panel"
|
||||
await query.edit_message_text(text)
|
||||
await query.edit_message_text(
|
||||
text + "\n\n⏱️ 60 秒无操作后自动消失。",
|
||||
reply_markup=panel_keyboard(),
|
||||
)
|
||||
schedule_panel_idle_delete(context, query.message)
|
||||
|
||||
|
||||
async def handle_trivia_answer(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
|
||||
Reference in New Issue
Block a user