From 71eac59a69b36e21efb0bcf165ad8cd41ccaa1fb Mon Sep 17 00:00:00 2001 From: sco1 Date: Mon, 7 Jan 2019 17:51:00 -0500 Subject: Add mute infraction check to role restoration --- bot/cogs/events.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/bot/cogs/events.py b/bot/cogs/events.py index edfc6e579..c604169c0 100644 --- a/bot/cogs/events.py +++ b/bot/cogs/events.py @@ -25,6 +25,7 @@ class Events: def __init__(self, bot: Bot): self.bot = bot + self.headers = {"X-API-KEY": Keys.site_api} @property def mod_log(self) -> ModLog: @@ -103,6 +104,29 @@ class Events: resp = await response.json() return resp["data"] + async def has_active_mute(self, user_id: str) -> bool: + """ + Check whether a user has any active mute infractions + """ + response = await self.bot.http_session.get( + URLs.site_infractions_user.format( + user_id=user_id + ), + params={"hidden": "True"}, + headers=self.headers + ) + infraction_list = await response.json() + + # Check for active mute infractions + if len(infraction_list) == 0: + # Short circuit + return False + + muted_check = any( + [infraction["active"] for infraction in infraction_list if infraction["type"].lower() == "mute"] + ) + return muted_check + async def on_command_error(self, ctx: Context, e: CommandError): command = ctx.command parent = None @@ -236,6 +260,10 @@ class Events: for role in RESTORE_ROLES: if role in old_roles: + # Check for mute roles that were not able to be removed and skip if present + if role == str(Roles.muted) and not await self.has_active_mute(str(member.id)): + continue + new_roles.append(Object(int(role))) for role in new_roles: -- cgit v1.2.3 From d5cb479be6925a10cf0c46e3088518f56a5a91b3 Mon Sep 17 00:00:00 2001 From: sco1 Date: Mon, 7 Jan 2019 19:16:03 -0500 Subject: Add line after docstring --- bot/cogs/events.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/cogs/events.py b/bot/cogs/events.py index c604169c0..77a15733d 100644 --- a/bot/cogs/events.py +++ b/bot/cogs/events.py @@ -108,6 +108,7 @@ class Events: """ Check whether a user has any active mute infractions """ + response = await self.bot.http_session.get( URLs.site_infractions_user.format( user_id=user_id -- cgit v1.2.3 From 0b8c5f074c725d739fffabb18076519d33a033a7 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> Date: Tue, 8 Jan 2019 16:18:43 -0500 Subject: Remove list comprehension since any() works on generators Co-Authored-By: sco1 --- bot/cogs/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/events.py b/bot/cogs/events.py index 77a15733d..b44e5871e 100644 --- a/bot/cogs/events.py +++ b/bot/cogs/events.py @@ -124,7 +124,7 @@ class Events: return False muted_check = any( - [infraction["active"] for infraction in infraction_list if infraction["type"].lower() == "mute"] + infraction["active"] for infraction in infraction_list if infraction["type"].lower() == "mute" ) return muted_check -- cgit v1.2.3 From e8a5db64dc70270488a6b9a43e21470c11936878 Mon Sep 17 00:00:00 2001 From: sco1 Date: Tue, 8 Jan 2019 16:36:54 -0500 Subject: Switch short-circuit logic, add logging --- bot/cogs/events.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/cogs/events.py b/bot/cogs/events.py index b44e5871e..78878dcb9 100644 --- a/bot/cogs/events.py +++ b/bot/cogs/events.py @@ -119,7 +119,7 @@ class Events: infraction_list = await response.json() # Check for active mute infractions - if len(infraction_list) == 0: + if not infraction_list: # Short circuit return False @@ -263,6 +263,10 @@ class Events: if role in old_roles: # Check for mute roles that were not able to be removed and skip if present if role == str(Roles.muted) and not await self.has_active_mute(str(member.id)): + log.debug( + f"User {member.id} has no active mute infraction, " + "their leftover muted role will not be persisted" + ) continue new_roles.append(Object(int(role))) -- cgit v1.2.3 From 25d9b1ea36b03ed431d7262d373124e3f788d408 Mon Sep 17 00:00:00 2001 From: sco1 Date: Tue, 8 Jan 2019 17:34:51 -0500 Subject: From review --- bot/cogs/events.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/cogs/events.py b/bot/cogs/events.py index 78878dcb9..f0baecd4b 100644 --- a/bot/cogs/events.py +++ b/bot/cogs/events.py @@ -123,10 +123,9 @@ class Events: # Short circuit return False - muted_check = any( + return any( infraction["active"] for infraction in infraction_list if infraction["type"].lower() == "mute" ) - return muted_check async def on_command_error(self, ctx: Context, e: CommandError): command = ctx.command -- cgit v1.2.3