quizgacha 添加命令请求限制

This commit is contained in:
洛水.山岭居室 2022-05-20 14:33:35 +08:00
parent 2ffef08e3e
commit 5bc96d7598
2 changed files with 46 additions and 6 deletions

View File

@ -1,7 +1,9 @@
import os import os
import time
from telegram import Update from telegram import Update
from telegram.constants import ChatAction from telegram.constants import ChatAction
from telegram.error import BadRequest
from telegram.ext import CallbackContext, ConversationHandler, filters from telegram.ext import CallbackContext, ConversationHandler, filters
from logger import Log from logger import Log
@ -22,6 +24,7 @@ class Gacha(BasePlugins):
for character in metadat.characters: for character in metadat.characters:
name = character["Name"] name = character["Name"]
self.character_gacha_card[name] = character["GachaCard"] self.character_gacha_card[name] = character["GachaCard"]
self.user_time = {}
CHECK_SERVER, COMMAND_RESULT = range(10600, 10602) CHECK_SERVER, COMMAND_RESULT = range(10600, 10602)
@ -29,6 +32,24 @@ class Gacha(BasePlugins):
message = update.message message = update.message
user = update.effective_user user = update.effective_user
Log.info(f"用户 {user.full_name}[{user.id}] 抽卡模拟器命令请求") 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(" ") args = message.text.split(" ")
gacha_name = "角色活动" gacha_name = "角色活动"
if len(args) > 1: if len(args) > 1:

View File

@ -7,6 +7,7 @@ from numpy.random import MT19937, Generator
from redis import DataError, ResponseError from redis import DataError, ResponseError
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, Poll, \ from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, Poll, \
ReplyKeyboardRemove, Message ReplyKeyboardRemove, Message
from telegram.error import BadRequest
from telegram.ext import CallbackContext, filters, ConversationHandler from telegram.ext import CallbackContext, filters, ConversationHandler
from telegram.helpers import escape_markdown from telegram.helpers import escape_markdown
@ -31,6 +32,7 @@ class Quiz(BasePlugins):
def __init__(self, service: BaseService): def __init__(self, service: BaseService):
super().__init__(service) super().__init__(service)
self.user_time = {}
self.send_time = time.time() self.send_time = time.time()
self.generator = Generator(MT19937(int(self.send_time))) self.generator = Generator(MT19937(int(self.send_time)))
self.service = service self.service = service
@ -69,7 +71,8 @@ class Quiz(BasePlugins):
async def command_start(self, update: Update, context: CallbackContext) -> int: async def command_start(self, update: Update, context: CallbackContext) -> int:
user = update.effective_user 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命令请求") Log.info(f"用户 {user.full_name}[{user.id}] quiz命令请求")
admin_list = await self.service.admin.get_admin_list() admin_list = await self.service.admin.get_admin_list()
if user.id in admin_list: if user.id in admin_list:
@ -77,19 +80,35 @@ class Quiz(BasePlugins):
if quiz_command_data is None: if quiz_command_data is None:
quiz_command_data = QuizCommandData() quiz_command_data = QuizCommandData()
context.chat_data["quiz_command_data"] = quiz_command_data 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 = [ reply_keyboard = [
["查看问题", "添加问题"], ["查看问题", "添加问题"],
["重载问题"], ["重载问题"],
["退出"] ["退出"]
] ]
await update.message.reply_markdown_v2(message, await message.reply_markdown_v2(text, reply_markup=ReplyKeyboardMarkup(reply_keyboard,
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
one_time_keyboard=True))
return self.CHECK_COMMAND return self.CHECK_COMMAND
else: else:
await self.send_poll(update) await self.send_poll(update)
elif filters.ChatType.GROUPS.filter(update.message): 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) poll_message = await self.send_poll(update)
if poll_message is None: if poll_message is None:
return ConversationHandler.END return ConversationHandler.END
@ -100,7 +119,7 @@ class Quiz(BasePlugins):
async def view_command(self, update: Update, context: CallbackContext) -> int: async def view_command(self, update: Update, context: CallbackContext) -> int:
keyboard = [ keyboard = [
[ [
InlineKeyboardButton(text="选择问题", switch_inline_query_current_chat="查看问题") InlineKeyboardButton(text="选择问题", switch_inline_query_current_chat="查看问题 ")
] ]
] ]
await update.message.reply_text("请回复你要查看的问题", await update.message.reply_text("请回复你要查看的问题",