Migrate tasks, closes #117

This commit is contained in:
Zeva Rose 2022-02-03 21:28:51 -07:00
parent bc94cd716b
commit e79e298c11
3 changed files with 53 additions and 27 deletions

View file

@ -1,7 +1,9 @@
"""J.A.R.V.I.S. unban background task handler."""
from asyncio import to_thread
from datetime import datetime, timedelta
from discord.ext.tasks import loop
from dis_snek.ext.tasks.task import Task
from dis_snek.ext.tasks.triggers import IntervalTrigger
import jarvis
from jarvis.config import get_config
@ -10,19 +12,18 @@ from jarvis.db.models import Ban, Unban
jarvis_id = get_config().client_id
@loop(minutes=10)
async def unban() -> None:
"""J.A.R.V.I.S. unban background task."""
async def _unban() -> None:
"""J.A.R.V.I.S. unban blocking task."""
bans = Ban.objects(type="temp", active=True)
unbans = []
for ban in bans:
if ban.created_at + timedelta(hours=ban.duration) < datetime.utcnow() + timedelta(
minutes=10
):
guild = await jarvis.jarvis.fetch_guild(ban.guild)
user = await jarvis.jarvis.fetch_user(ban.user)
guild = await jarvis.jarvis.get_guild(ban.guild)
user = await jarvis.jarvis.get_user(ban.user)
if user:
guild.unban(user)
await guild.unban(user=user, reason="Ban expired")
ban.active = False
ban.save()
unbans.append(
@ -36,4 +37,10 @@ async def unban() -> None:
)
)
if unbans:
Ban.objects().insert(unbans)
Unban.objects().insert(unbans)
@Task.create(IntervalTrigger(minutes=10))
async def unban() -> None:
"""J.A.R.V.I.S. unban background task."""
await to_thread(_unban)

View file

@ -1,25 +1,37 @@
"""J.A.R.V.I.S. unlock background task handler."""
from asyncio import to_thread
from datetime import datetime, timedelta
from discord.ext.tasks import loop
from dis_snek.ext.tasks.task import Task
from dis_snek.ext.tasks.triggers import IntervalTrigger
import jarvis
from jarvis.db.models import Lock
@loop(minutes=1)
async def unlock() -> None:
"""J.A.R.V.I.S. unlock background task."""
async def _unlock() -> None:
"""J.A.R.V.I.S. unlock blocking task."""
locks = Lock.objects(active=True)
# Block execution for now
# TODO: Reevaluate with admin/lock[down]
if False:
for lock in locks:
if lock.created_at + timedelta(minutes=lock.duration) < datetime.utcnow():
guild = await jarvis.jarvis.fetch_guild(lock.guild)
channel = await jarvis.jarvis.fetch_channel(lock.channel)
guild = await jarvis.jarvis.get_guild(lock.guild)
channel = await jarvis.jarvis.get_guild(lock.channel)
if channel:
roles = await guild.fetch_roles()
for role in roles:
overrides = channel.overwrites_for(role)
overrides.send_messages = None
await channel.set_permissions(role, overwrite=overrides, reason="Lock expired")
await channel.set_permissions(
role, overwrite=overrides, reason="Lock expired"
)
lock.active = False
lock.save()
@Task.create(IntervalTrigger(minutes=1))
async def unlock() -> None:
"""J.A.R.V.I.S. unlock background task."""
await to_thread(_unlock)

View file

@ -1,16 +1,23 @@
"""J.A.R.V.I.S. unwarn background task handler."""
from asyncio import to_thread
from datetime import datetime, timedelta
from discord.ext.tasks import loop
from dis_snek.ext.tasks.task import Task
from dis_snek.ext.tasks.triggers import IntervalTrigger
from jarvis.db.models import Warning
@loop(hours=1)
async def unwarn() -> None:
"""J.A.R.V.I.S. unwarn background task."""
async def _unwarn() -> None:
"""J.A.R.V.I.S. unwarn blocking task."""
warns = Warning.objects(active=True)
for warn in warns:
if warn.created_at + timedelta(hours=warn.duration) < datetime.utcnow():
warn.active = False
warn.save()
@Task.create(IntervalTrigger(hours=1))
async def unwarn() -> None:
"""J.A.R.V.I.S. unwarn background task."""
await to_thread(_unwarn)