fix antiflood megaozbeksystem

This commit is contained in:
huker667 committed 2026-07-05 15:43:52 +03:00
1 parent c033859505
commit 4c8e21fd42
3 files changed
+46 -10

No files matched your search

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