From 5bc96d7598a6b28629d8d38903961978464389fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=2E=E5=B1=B1=E5=B2=AD=E5=B1=85=E5=AE=A4?= Date: Fri, 20 May 2022 14:33:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99=20`quiz`=20=E5=92=8C=20`gacha`=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=91=BD=E4=BB=A4=E8=AF=B7=E6=B1=82=E9=99=90?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/gacha.py | 21 +++++++++++++++++++++ plugins/quiz.py | 31 +++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/plugins/gacha.py b/plugins/gacha.py index b0ca626e..25dce0ef 100644 --- a/plugins/gacha.py +++ b/plugins/gacha.py @@ -1,7 +1,9 @@ import os +import time from telegram import Update from telegram.constants import ChatAction +from telegram.error import BadRequest from telegram.ext import CallbackContext, ConversationHandler, filters from logger import Log @@ -22,6 +24,7 @@ class Gacha(BasePlugins): for character in metadat.characters: name = character["Name"] self.character_gacha_card[name] = character["GachaCard"] + self.user_time = {} CHECK_SERVER, COMMAND_RESULT = range(10600, 10602) @@ -29,6 +32,24 @@ class Gacha(BasePlugins): message = update.message user = update.effective_user Log.info(f"用户 {user.full_name}[{user.id}] 抽卡模拟器命令请求") + if filters.ChatType.GROUPS.filter(message): + try: + command_time = self.user_time.get(f"{user.id}") + if command_time is None: + self.user_time[f"{user.id}"] = time.time() + else: + if time.time() - command_time <= 10: + try: + await message.delete() + except BadRequest as error: + Log.warning("删除消息失败", error) + pass + return + else: + self.user_time[f"{user.id}"] = time.time() + except (ValueError, KeyError) as error: + Log.error("quiz模块 user_time 操作失败", error) + pass args = message.text.split(" ") gacha_name = "角色活动" if len(args) > 1: diff --git a/plugins/quiz.py b/plugins/quiz.py index 2fcbd79d..495485ba 100644 --- a/plugins/quiz.py +++ b/plugins/quiz.py @@ -7,6 +7,7 @@ from numpy.random import MT19937, Generator from redis import DataError, ResponseError from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, Poll, \ ReplyKeyboardRemove, Message +from telegram.error import BadRequest from telegram.ext import CallbackContext, filters, ConversationHandler from telegram.helpers import escape_markdown @@ -31,6 +32,7 @@ class Quiz(BasePlugins): def __init__(self, service: BaseService): super().__init__(service) + self.user_time = {} self.send_time = time.time() self.generator = Generator(MT19937(int(self.send_time))) self.service = service @@ -69,7 +71,8 @@ class Quiz(BasePlugins): async def command_start(self, update: Update, context: CallbackContext) -> int: user = update.effective_user - if filters.ChatType.PRIVATE.filter(update.message): + message = update.message + if filters.ChatType.PRIVATE.filter(message): Log.info(f"用户 {user.full_name}[{user.id}] quiz命令请求") admin_list = await self.service.admin.get_admin_list() if user.id in admin_list: @@ -77,19 +80,35 @@ class Quiz(BasePlugins): if quiz_command_data is None: quiz_command_data = QuizCommandData() context.chat_data["quiz_command_data"] = quiz_command_data - message = f'你好 {user.mention_markdown_v2()} {escape_markdown("!请选择你的操作!")}' + text = f'你好 {user.mention_markdown_v2()} {escape_markdown("!请选择你的操作!")}' reply_keyboard = [ ["查看问题", "添加问题"], ["重载问题"], ["退出"] ] - await update.message.reply_markdown_v2(message, - reply_markup=ReplyKeyboardMarkup(reply_keyboard, - one_time_keyboard=True)) + await message.reply_markdown_v2(text, reply_markup=ReplyKeyboardMarkup(reply_keyboard, + one_time_keyboard=True)) return self.CHECK_COMMAND else: await self.send_poll(update) elif filters.ChatType.GROUPS.filter(update.message): + try: + command_time = self.user_time.get(f"{user.id}") + if command_time is None: + self.user_time[f"{user.id}"] = time.time() + else: + if time.time() - command_time <= 10: + try: + await message.delete() + except BadRequest as error: + Log.warning("删除消息失败", error) + pass + return ConversationHandler.END + else: + self.user_time[f"{user.id}"] = time.time() + except (ValueError, KeyError) as error: + Log.error("quiz模块 user_time 操作失败", error) + pass poll_message = await self.send_poll(update) if poll_message is None: return ConversationHandler.END @@ -100,7 +119,7 @@ class Quiz(BasePlugins): async def view_command(self, update: Update, context: CallbackContext) -> int: keyboard = [ [ - InlineKeyboardButton(text="选择问题", switch_inline_query_current_chat="查看问题") + InlineKeyboardButton(text="选择问题", switch_inline_query_current_chat="查看问题 ") ] ] await update.message.reply_text("请回复你要查看的问题",