Add ban task
This commit is contained in:
parent
a5edb0b59a
commit
759fe011d5
1 changed files with 41 additions and 0 deletions
41
jarvis_tasks/tasks/ban.py
Normal file
41
jarvis_tasks/tasks/ban.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
"""JARVIS ban tasks."""
|
||||
import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from jarvis_core.db import q
|
||||
from jarvis_core.db.models import Ban, Unban
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from dis_snek import Snake
|
||||
|
||||
|
||||
async def unban(bot: "Snake") -> None:
|
||||
"""
|
||||
Unban users when ban expires.
|
||||
|
||||
Args:
|
||||
bot: Snake instance
|
||||
"""
|
||||
while True:
|
||||
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()
|
Loading…
Add table
Reference in a new issue