diff options
| author | 2019-01-11 14:49:40 +0100 | |
|---|---|---|
| committer | 2019-01-11 14:49:40 +0100 | |
| commit | 49c2a6df5eec940b08833f951fed9e25ea74c7f1 (patch) | |
| tree | f3cc92e5a7b5e53ddbd1cc95c9445726c7337685 | |
| parent | Merge pull request #267 from python-discord/token-detection-warning-strong-la... (diff) | |
| parent | Merge branch 'master' into accidental-permamute (diff) | |
Merge pull request #260 from python-discord/accidental-permamute
Add mute infraction check to role restoration so users won't be accidentally permamuted when they come back to server after leaving while were tempmuted.
| -rw-r--r-- | bot/cogs/events.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/bot/cogs/events.py b/bot/cogs/events.py index edfc6e579..f0baecd4b 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 not infraction_list: + # Short circuit + return False + + return any( + infraction["active"] for infraction in infraction_list if infraction["type"].lower() == "mute" + ) + async def on_command_error(self, ctx: Context, e: CommandError): command = ctx.command parent = None @@ -236,6 +260,14 @@ 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)): + 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))) for role in new_roles: |