fix antiflood megaozbeksystem
This commit is contained in:
1 parent
c033859505
commit
4c8e21fd42
3 files changed
+46
-10
No files matched your search
+13
@@ -11,6 +11,12 @@ def is_blocked(user_id):
|
||||
current_time = time.time()
|
||||
if user_id and user_id in last_command_time:
|
||||
time_diff = current_time - last_command_time[user_id][0]
|
||||
if user_id in last_error_time:
|
||||
time_error_diff = current_time - last_error_time[user_id]
|
||||
else:
|
||||
last_error_time[user_id] = current_time
|
||||
time_error_diff = 1984
|
||||
|
||||
if time_diff < 3:
|
||||
alo = "🚫 *файл не вошёл.* хватит так быстро слать свои сообщения.\n" \
|
||||
f"подожди {round(3 - time_diff, 3)} секунд, брат😡😡"
|
||||
@@ -18,8 +24,15 @@ def is_blocked(user_id):
|
||||
alo = "🚫 *файл не вошёл.* я ещё не сгенерировал прошлое сообщение.\n" \
|
||||
f"подожди {round(10 - time_diff, 3)} секунд, чтобы я закрыл глаза " \
|
||||
"на это da."
|
||||
elif len(last_command_time[user_id][2]) > 1:
|
||||
alo = "🚫 *файл не вошёл.* хватит так быстро слать свои сообщения."
|
||||
else:
|
||||
return "", False
|
||||
|
||||
if time_error_diff < 2:
|
||||
return "", True
|
||||
else:
|
||||
last_error_time[user_id] = current_time
|
||||
return alo, True
|
||||
return "", False
|
||||
|
||||
|
||||
@@ -94,14 +94,12 @@ async def guest_middleware(handler, event, data):
|
||||
if not is_replied_bot(message, is_channel, user_text, config) and event.message:
|
||||
return
|
||||
|
||||
# снизу очень важный кусок кода!! у нас же async бот да ну вот
|
||||
# если переслать несколько сообщений то удасться обойти ограничение
|
||||
# по задержке между генерациями и поэтому тут такое решение
|
||||
# скоро это будет нормально реализовано. ждите.
|
||||
if user_id:
|
||||
await asyncio.sleep(uniform(0,1))
|
||||
await asyncio.sleep(uniform(0,0.3))
|
||||
|
||||
text, blocked = is_blocked(user_id)
|
||||
if not text and blocked:
|
||||
return
|
||||
if blocked:
|
||||
if event.guest_message:
|
||||
await g_answer(data, message, text, title="ошибка врат")
|
||||
@@ -115,16 +113,25 @@ async def guest_middleware(handler, event, data):
|
||||
|
||||
return
|
||||
|
||||
ensure_user(user_id)
|
||||
|
||||
if not user_id in last_command_time:
|
||||
last_command_time[user_id] = [current_time, True, [message.message_id]]
|
||||
elif len(last_command_time[user_id][2]) == 0:
|
||||
last_command_time[user_id] = [current_time, True, [message.message_id]]
|
||||
else:
|
||||
last_command_time[user_id][0] = current_time
|
||||
last_command_time[user_id][1] = True
|
||||
last_command_time[user_id][2] += [message.message_id]
|
||||
|
||||
await asyncio.sleep(uniform(0,0.5))
|
||||
|
||||
if event.message:
|
||||
try:
|
||||
await bot.send_chat_action(chat_id=message.chat.id, action="typing")
|
||||
except TelegramForbiddenError:
|
||||
return
|
||||
|
||||
ensure_user(user_id)
|
||||
|
||||
last_command_time[user_id] = [current_time, True]
|
||||
|
||||
msg = None
|
||||
replied = message.reply_to_message or None
|
||||
if replied:
|
||||
@@ -279,6 +286,7 @@ async def guest_middleware(handler, event, data):
|
||||
else:
|
||||
message_id = None
|
||||
|
||||
if len(last_command_time[user_id][2]) == 1:
|
||||
if is_channel and event.message:
|
||||
result = await generate(
|
||||
bot=bot,
|
||||
@@ -308,10 +316,14 @@ async def guest_middleware(handler, event, data):
|
||||
|
||||
if event.guest_message:
|
||||
add_one_to("stats/guest")
|
||||
else:
|
||||
print(f"{YELLOW} -- err in blocking 2 {RESET}")
|
||||
return
|
||||
else:
|
||||
return
|
||||
|
||||
last_command_time[user_id][1] = False
|
||||
last_command_time[user_id][2] = []
|
||||
|
||||
try:
|
||||
if event.guest_message:
|
||||
@@ -391,6 +403,8 @@ async def chosen_inline_result_handler(chosen_result: ChosenInlineResult):
|
||||
else:
|
||||
current_time = time.time()
|
||||
alo, blocked = is_blocked(user_id)
|
||||
if not alo:
|
||||
return
|
||||
if alo and blocked:
|
||||
await bot.edit_message_text(
|
||||
text=alo,
|
||||
@@ -398,7 +412,14 @@ async def chosen_inline_result_handler(chosen_result: ChosenInlineResult):
|
||||
)
|
||||
return
|
||||
|
||||
last_command_time[user_id] = [current_time, True]
|
||||
if not user_id in last_command_time:
|
||||
last_command_time[user_id] = [current_time, True, [inline_message_id]]
|
||||
elif len(last_command_time[user_id][2]) == 0:
|
||||
last_command_time[user_id] = [current_time, True, [inline_message_id]]
|
||||
else:
|
||||
last_command_time[user_id][0] = current_time
|
||||
last_command_time[user_id][1] = True
|
||||
last_command_time[user_id][2] += [inline_message_id]
|
||||
|
||||
if inline_message_id:
|
||||
try:
|
||||
@@ -407,6 +428,7 @@ async def chosen_inline_result_handler(chosen_result: ChosenInlineResult):
|
||||
user_id=chosen_result.from_user.id,
|
||||
)
|
||||
last_command_time[user_id][1] = False
|
||||
last_command_time[user_id][2] = []
|
||||
except Exception as e:
|
||||
error = parse_response(str(e))
|
||||
await bot.edit_message_text(
|
||||
|
||||
@@ -11,6 +11,7 @@ from colors import *
|
||||
from config import *
|
||||
|
||||
last_command_time = {}
|
||||
last_error_time = {}
|
||||
last_block_time = {}
|
||||
user_contexts = {}
|
||||
|
||||
|
||||
Reference in new issue
Block a user