40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
"""JARVIS ban tasks."""
|
|
import asyncio
|
|
from datetime import datetime, timedelta
|
|
from logging import Logger
|
|
|
|
from dis_snek import Snake
|
|
from jarvis_core.db import q
|
|
from jarvis_core.db.models import Ban, Unban
|
|
|
|
|
|
async def unban(bot: Snake, logger: Logger) -> None:
|
|
"""
|
|
Unban users when ban expires.
|
|
|
|
Args:
|
|
bot: Snake instance
|
|
logger: Global logger
|
|
"""
|
|
while True:
|
|
await asyncio.sleep(600)
|
|
max_time = datetime.utcnow() + timedelta(minutes=10)
|
|
bans = Ban.find(q(type="temp", active=True))
|
|
async for ban in bans:
|
|
if ban.created_at + timedelta(hours=ban.duration) < max_time:
|
|
guild = await bot.fetch_guild(ban.guild)
|
|
user = await bot.fetch_user(ban.user)
|
|
if guild and user:
|
|
await guild.unban(user=user, reason="JARVIS tempban expired")
|
|
|
|
ban.update(q(active=False))
|
|
await ban.commit()
|
|
u = Unban(
|
|
user=user.id,
|
|
guild=guild.id,
|
|
username=user.name,
|
|
discrim=user.discriminator,
|
|
admin=bot.user.id,
|
|
reason="Ban expired",
|
|
)
|
|
await u.commit()
|