43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
"""JARVIS warnings tasks."""
|
|
import asyncio
|
|
import logging
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from dis_snek import Snake
|
|
from jarvis_core.db import q
|
|
from jarvis_core.db.models import Warning
|
|
|
|
from jarvis_tasks.util import runat
|
|
|
|
queue = []
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def _unwarn(warn: Warning) -> None:
|
|
logger.debug(f"Deactivating warning {warn.id}")
|
|
warn.active = False
|
|
await warn.commit()
|
|
queue.remove(warn.id)
|
|
|
|
|
|
async def unwarn(bot: Snake) -> None:
|
|
"""
|
|
Deactivate warnings when they expire.
|
|
|
|
Args:
|
|
bot: Snake instance
|
|
"""
|
|
logger.debug("Starting Task-unwarn")
|
|
while True:
|
|
max_ts = datetime.now(tz=timezone.utc) + timedelta(minutes=55)
|
|
warns = Warning.find(q(active=True, expires_at__lte=max_ts, id__nin=queue))
|
|
async for warn in warns:
|
|
if warn.id in queue:
|
|
logger.warn("Warning found despite filter")
|
|
continue
|
|
coro = _unwarn(warn)
|
|
when = warn.expires_at
|
|
asyncio.create_task(runat(when, coro, logger))
|
|
queue.append(warn.id)
|
|
# Check every hour
|
|
await asyncio.sleep(3600)
|