Migrate tasks, closes #117
This commit is contained in:
parent
bc94cd716b
commit
e79e298c11
3 changed files with 53 additions and 27 deletions
|
@ -1,7 +1,9 @@
|
||||||
"""J.A.R.V.I.S. unban background task handler."""
|
"""J.A.R.V.I.S. unban background task handler."""
|
||||||
|
from asyncio import to_thread
|
||||||
from datetime import datetime, timedelta
|
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
|
import jarvis
|
||||||
from jarvis.config import get_config
|
from jarvis.config import get_config
|
||||||
|
@ -10,19 +12,18 @@ from jarvis.db.models import Ban, Unban
|
||||||
jarvis_id = get_config().client_id
|
jarvis_id = get_config().client_id
|
||||||
|
|
||||||
|
|
||||||
@loop(minutes=10)
|
async def _unban() -> None:
|
||||||
async def unban() -> None:
|
"""J.A.R.V.I.S. unban blocking task."""
|
||||||
"""J.A.R.V.I.S. unban background task."""
|
|
||||||
bans = Ban.objects(type="temp", active=True)
|
bans = Ban.objects(type="temp", active=True)
|
||||||
unbans = []
|
unbans = []
|
||||||
for ban in bans:
|
for ban in bans:
|
||||||
if ban.created_at + timedelta(hours=ban.duration) < datetime.utcnow() + timedelta(
|
if ban.created_at + timedelta(hours=ban.duration) < datetime.utcnow() + timedelta(
|
||||||
minutes=10
|
minutes=10
|
||||||
):
|
):
|
||||||
guild = await jarvis.jarvis.fetch_guild(ban.guild)
|
guild = await jarvis.jarvis.get_guild(ban.guild)
|
||||||
user = await jarvis.jarvis.fetch_user(ban.user)
|
user = await jarvis.jarvis.get_user(ban.user)
|
||||||
if user:
|
if user:
|
||||||
guild.unban(user)
|
await guild.unban(user=user, reason="Ban expired")
|
||||||
ban.active = False
|
ban.active = False
|
||||||
ban.save()
|
ban.save()
|
||||||
unbans.append(
|
unbans.append(
|
||||||
|
@ -36,4 +37,10 @@ async def unban() -> None:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if unbans:
|
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)
|
||||||
|
|
|
@ -1,25 +1,37 @@
|
||||||
"""J.A.R.V.I.S. unlock background task handler."""
|
"""J.A.R.V.I.S. unlock background task handler."""
|
||||||
|
from asyncio import to_thread
|
||||||
from datetime import datetime, timedelta
|
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
|
import jarvis
|
||||||
from jarvis.db.models import Lock
|
from jarvis.db.models import Lock
|
||||||
|
|
||||||
|
|
||||||
@loop(minutes=1)
|
async def _unlock() -> None:
|
||||||
async def unlock() -> None:
|
"""J.A.R.V.I.S. unlock blocking task."""
|
||||||
"""J.A.R.V.I.S. unlock background task."""
|
|
||||||
locks = Lock.objects(active=True)
|
locks = Lock.objects(active=True)
|
||||||
|
# Block execution for now
|
||||||
|
# TODO: Reevaluate with admin/lock[down]
|
||||||
|
if False:
|
||||||
for lock in locks:
|
for lock in locks:
|
||||||
if lock.created_at + timedelta(minutes=lock.duration) < datetime.utcnow():
|
if lock.created_at + timedelta(minutes=lock.duration) < datetime.utcnow():
|
||||||
guild = await jarvis.jarvis.fetch_guild(lock.guild)
|
guild = await jarvis.jarvis.get_guild(lock.guild)
|
||||||
channel = await jarvis.jarvis.fetch_channel(lock.channel)
|
channel = await jarvis.jarvis.get_guild(lock.channel)
|
||||||
if channel:
|
if channel:
|
||||||
roles = await guild.fetch_roles()
|
roles = await guild.fetch_roles()
|
||||||
for role in roles:
|
for role in roles:
|
||||||
overrides = channel.overwrites_for(role)
|
overrides = channel.overwrites_for(role)
|
||||||
overrides.send_messages = None
|
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.active = False
|
||||||
lock.save()
|
lock.save()
|
||||||
|
|
||||||
|
|
||||||
|
@Task.create(IntervalTrigger(minutes=1))
|
||||||
|
async def unlock() -> None:
|
||||||
|
"""J.A.R.V.I.S. unlock background task."""
|
||||||
|
await to_thread(_unlock)
|
||||||
|
|
|
@ -1,16 +1,23 @@
|
||||||
"""J.A.R.V.I.S. unwarn background task handler."""
|
"""J.A.R.V.I.S. unwarn background task handler."""
|
||||||
|
from asyncio import to_thread
|
||||||
from datetime import datetime, timedelta
|
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
|
from jarvis.db.models import Warning
|
||||||
|
|
||||||
|
|
||||||
@loop(hours=1)
|
async def _unwarn() -> None:
|
||||||
async def unwarn() -> None:
|
"""J.A.R.V.I.S. unwarn blocking task."""
|
||||||
"""J.A.R.V.I.S. unwarn background task."""
|
|
||||||
warns = Warning.objects(active=True)
|
warns = Warning.objects(active=True)
|
||||||
for warn in warns:
|
for warn in warns:
|
||||||
if warn.created_at + timedelta(hours=warn.duration) < datetime.utcnow():
|
if warn.created_at + timedelta(hours=warn.duration) < datetime.utcnow():
|
||||||
warn.active = False
|
warn.active = False
|
||||||
warn.save()
|
warn.save()
|
||||||
|
|
||||||
|
|
||||||
|
@Task.create(IntervalTrigger(hours=1))
|
||||||
|
async def unwarn() -> None:
|
||||||
|
"""J.A.R.V.I.S. unwarn background task."""
|
||||||
|
await to_thread(_unwarn)
|
||||||
|
|
Loading…
Add table
Reference in a new issue