Files
TG_autoposter/app/utils/__init__.py
2025-12-18 05:55:32 +09:00

41 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import datetime, timedelta
from app.models import Group
async def can_send_message(group: Group) -> tuple[bool, int]:
"""
Проверить, можно ли отправить сообщение в группу с учетом slow mode
Возвращает (можно_ли_отправить, сколько_секунд_ждать)
"""
if group.slow_mode_delay == 0:
# Нет ограничений
return True, 0
if group.last_message_time is None:
# Первое сообщение
return True, 0
time_since_last_message = datetime.utcnow() - group.last_message_time
seconds_to_wait = group.slow_mode_delay - time_since_last_message.total_seconds()
if seconds_to_wait <= 0:
return True, 0
else:
return False, int(seconds_to_wait) + 1
async def wait_for_slow_mode(group: Group) -> int:
"""
Ждать, пока пройдет slow mode
Возвращает реальное время ожидания в секундах
"""
can_send, wait_time = await can_send_message(group)
if can_send:
return 0
await asyncio.sleep(wait_time)
return wait_time
import asyncio