init commit
This commit is contained in:
40
app/utils/__init__.py
Normal file
40
app/utils/__init__.py
Normal file
@@ -0,0 +1,40 @@
|
||||
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
|
||||
Reference in New Issue
Block a user