fix: handle trivia answers
This commit is contained in:
+45
-1
@@ -3,7 +3,7 @@ import asyncio
|
||||
import logging
|
||||
|
||||
from telegram import Update
|
||||
from telegram.ext import ContextTypes, CommandHandler, MessageHandler, filters
|
||||
from telegram.ext import ApplicationHandlerStop, ContextTypes, CommandHandler, MessageHandler, filters
|
||||
|
||||
from db.models import Action
|
||||
from services.game_service import (
|
||||
@@ -67,6 +67,7 @@ async def cmd_trivia(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
text += f"{i}. {opt}\n"
|
||||
text += f"\n💡 请回复序号或答案文字"
|
||||
context.user_data["trivia_answer"] = q["a"]
|
||||
context.user_data["trivia_options"] = q["opts"]
|
||||
reply = await update.message.reply_text(text)
|
||||
asyncio.create_task(auto_delete(update.message))
|
||||
asyncio.create_task(auto_delete(reply))
|
||||
@@ -104,6 +105,48 @@ async def cmd_rank(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
asyncio.create_task(auto_delete(reply))
|
||||
|
||||
|
||||
async def handle_trivia_answer(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""处理 /trivia 后的用户答题。"""
|
||||
msg = update.effective_message
|
||||
user = update.effective_user
|
||||
if not msg or not msg.text or not user or user.is_bot:
|
||||
return
|
||||
|
||||
correct_answer = context.user_data.get("trivia_answer")
|
||||
options = context.user_data.get("trivia_options") or []
|
||||
if not correct_answer:
|
||||
return
|
||||
|
||||
raw_answer = msg.text.strip()
|
||||
answer = raw_answer
|
||||
if raw_answer.isdigit():
|
||||
idx = int(raw_answer) - 1
|
||||
if 0 <= idx < len(options):
|
||||
answer = options[idx]
|
||||
|
||||
result = await check_trivia_answer(user.id, user.username or user.first_name or str(user.id), answer, correct_answer)
|
||||
context.user_data.pop("trivia_answer", None)
|
||||
context.user_data.pop("trivia_options", None)
|
||||
|
||||
if result["correct"]:
|
||||
text = (
|
||||
"✅ 回答正确!\n"
|
||||
f"🎯 正确答案:{correct_answer}\n"
|
||||
f"📊 当前积分:{result['score']}\n"
|
||||
f"🔥 连击:{result['streak']}"
|
||||
)
|
||||
else:
|
||||
text = (
|
||||
"❌ 回答错误\n"
|
||||
f"🎯 正确答案:{correct_answer}\n"
|
||||
f"📊 当前积分:{result['score']}"
|
||||
)
|
||||
reply = await msg.reply_text(text)
|
||||
asyncio.create_task(auto_delete(msg))
|
||||
asyncio.create_task(auto_delete(reply))
|
||||
raise ApplicationHandlerStop
|
||||
|
||||
|
||||
async def auto_delete(message, seconds=60):
|
||||
try:
|
||||
await asyncio.sleep(seconds)
|
||||
@@ -117,4 +160,5 @@ def register_fun(app):
|
||||
app.add_handler(CommandHandler("trivia", cmd_trivia, filters.ChatType.GROUPS))
|
||||
app.add_handler(CommandHandler("score", cmd_score, filters.ChatType.GROUPS))
|
||||
app.add_handler(CommandHandler("rank", cmd_rank, filters.ChatType.GROUPS))
|
||||
app.add_handler(MessageHandler(filters.TEXT & filters.ChatType.GROUPS & ~filters.COMMAND, handle_trivia_answer), group=-1)
|
||||
app.add_handler(MessageHandler(filters.TEXT & filters.ChatType.GROUPS, handle_trigger), group=2)
|
||||
|
||||
Reference in New Issue
Block a user