aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar momothereal <[email protected]>2018-07-21 18:13:43 -0400
committerGravatar momothereal <[email protected]>2018-07-21 18:13:43 -0400
commited9dd532055d48ec36b4df731d073e207b866ed4 (patch)
tree0c95a93882dbc66b7fea5c548ff596911ae8e281
parentAdd expiration scheduler + startup, tempmute command (diff)
Improve dangling infraction support
-rw-r--r--bot/cogs/moderation.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/bot/cogs/moderation.py b/bot/cogs/moderation.py
index 48857faf3..c85adf33d 100644
--- a/bot/cogs/moderation.py
+++ b/bot/cogs/moderation.py
@@ -1,6 +1,7 @@
import asyncio
import datetime
import logging
+from typing import Dict
from discord import Guild, Member, User
from discord.ext.commands import Bot, Context, command
@@ -20,19 +21,20 @@ class Moderation:
def __init__(self, bot: Bot):
self.bot = bot
self.headers = {"X-API-KEY": Keys.site_api}
+ self.expiration_tasks: Dict[str, asyncio.Task] = {}
async def on_ready(self):
# Schedule expiration for previous infractions
response = await self.bot.http_session.get(
URLs.site_infractions,
- params={"active": "true"},
+ params={"dangling": "true"},
headers=self.headers
)
infraction_list = await response.json()
loop = asyncio.get_event_loop()
for infraction_object in infraction_list:
if infraction_object["expires_at"] is not None:
- loop.create_task(self._scheduled_expiration(infraction_object))
+ self.schedule_expiration(loop, infraction_object)
@with_role(Roles.admin, Roles.owner, Roles.moderator)
@command(name="moderation.warn")
@@ -171,7 +173,7 @@ class Moderation:
infraction_expiration = infraction_object["expires_at"]
loop = asyncio.get_event_loop()
- loop.create_task(self._scheduled_expiration(infraction_object))
+ self.schedule_expiration(loop, infraction_object)
if reason is None:
result_message = f":ok_hand: muted {user.mention} until {infraction_expiration}."
@@ -180,6 +182,14 @@ class Moderation:
await ctx.send(result_message)
+ def schedule_expiration(self, loop: asyncio.AbstractEventLoop, infraction_object: dict):
+ infraction_id = infraction_object["id"]
+ if infraction_id in self.expiration_tasks:
+ return
+
+ task: asyncio.Task = loop.create_task(self._scheduled_expiration(infraction_object))
+ self.expiration_tasks[infraction_id] = task
+
async def _scheduled_expiration(self, infraction_object):
guild: Guild = self.bot.get_guild(constants.Guild.id)
infraction_id = infraction_object["id"]
@@ -195,7 +205,6 @@ class Moderation:
await asyncio.sleep(delay_seconds)
log.debug(f"Marking infraction {infraction_id} as inactive (expired).")
- log.debug(infraction_object)
user_id = infraction_object["user"]["user_id"]
if infraction_type == "mute":