2022-08-31 06:46:04 +00:00
|
|
|
import random
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram import Poll, Update
|
2022-08-31 06:46:04 +00:00
|
|
|
from telegram.constants import ChatAction
|
2022-12-18 14:24:27 +00:00
|
|
|
from telegram.error import BadRequest
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram.ext import filters, CallbackContext
|
2022-08-31 06:46:04 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.plugin import Plugin, handler
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.services.quiz.services import QuizService
|
|
|
|
from core.services.users.services import UserService
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2022-08-31 06:46:04 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
__all__ = ("QuizPlugin",)
|
2022-08-31 06:46:04 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
class QuizPlugin(Plugin):
|
2022-09-08 01:08:37 +00:00
|
|
|
"""派蒙的十万个为什么"""
|
2022-08-31 06:46:04 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
def __init__(self, quiz_service: QuizService = None, user_service: UserService = None):
|
|
|
|
self.user_service = user_service
|
2022-08-31 06:46:04 +00:00
|
|
|
self.quiz_service = quiz_service
|
|
|
|
self.time_out = 120
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
@handler.message(filters=filters.Regex("来一道题"))
|
|
|
|
@handler.command(command="quiz", block=False)
|
|
|
|
async def command_start(self, update: Update, _: CallbackContext) -> None:
|
2022-09-08 01:08:37 +00:00
|
|
|
message = update.effective_message
|
2023-03-14 01:27:22 +00:00
|
|
|
chat = update.effective_chat
|
2022-09-08 01:08:37 +00:00
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-08-31 06:46:04 +00:00
|
|
|
question_id_list = await self.quiz_service.get_question_id_list()
|
2022-10-11 05:46:06 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2024-03-10 12:40:26 +00:00
|
|
|
self.log_user(update, logger.info, "在群 %s[%s] 发送挑战问题命令请求", chat.title, chat.id)
|
2022-08-31 06:46:04 +00:00
|
|
|
if len(question_id_list) == 0:
|
|
|
|
return None
|
|
|
|
if len(question_id_list) == 0:
|
|
|
|
return None
|
2022-10-28 09:02:40 +00:00
|
|
|
question_id = random.choice(question_id_list) # nosec
|
|
|
|
question = await self.quiz_service.get_question(question_id)
|
2022-08-31 06:46:04 +00:00
|
|
|
_options = []
|
|
|
|
correct_option = None
|
|
|
|
for answer in question.answers:
|
|
|
|
_options.append(answer.text)
|
|
|
|
if answer.is_correct:
|
|
|
|
correct_option = answer.text
|
|
|
|
if correct_option is None:
|
|
|
|
question_id = question["question_id"]
|
2023-03-14 01:27:22 +00:00
|
|
|
logger.warning("Quiz模块 correct_option 异常 question_id[%s]", question_id)
|
2022-08-31 06:46:04 +00:00
|
|
|
return None
|
|
|
|
random.shuffle(_options)
|
|
|
|
index = _options.index(correct_option)
|
2022-12-18 14:24:27 +00:00
|
|
|
try:
|
|
|
|
poll_message = await message.reply_poll(
|
|
|
|
question.text,
|
|
|
|
_options,
|
|
|
|
correct_option_id=index,
|
|
|
|
is_anonymous=False,
|
|
|
|
open_period=self.time_out,
|
|
|
|
type=Poll.QUIZ,
|
|
|
|
)
|
|
|
|
except BadRequest as exc:
|
|
|
|
if "Not enough rights" in exc.message:
|
|
|
|
poll_message = await message.reply_text("出错了呜呜呜 ~ 权限不足,请请检查投票权限是否开启")
|
|
|
|
else:
|
|
|
|
raise exc
|
2022-10-11 05:46:06 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(message, delay=300)
|
|
|
|
self.add_delete_message_job(poll_message, delay=300)
|