diff options
| author | 2019-09-22 15:15:27 -0400 | |
|---|---|---|
| committer | 2019-09-22 15:15:27 -0400 | |
| commit | 9e30880df12d085b545ed3d429b2dcea3ea7d544 (patch) | |
| tree | b3409f6be37e20ad8c4f67bcb0fe609647d6f93f /bot/utils/moderation.py | |
| parent | Make defcon days command turn on defcon, refactor log messaging (diff) | |
| parent | Fix date formatting bug in infraction search (diff) | |
Merge branch 'master' into defcon-fix-django
Diffstat (limited to 'bot/utils/moderation.py')
| -rw-r--r-- | bot/utils/moderation.py | 48 | 
1 files changed, 37 insertions, 11 deletions
| diff --git a/bot/utils/moderation.py b/bot/utils/moderation.py index c1eb98dd6..7860f14a1 100644 --- a/bot/utils/moderation.py +++ b/bot/utils/moderation.py @@ -1,11 +1,11 @@  import logging  from datetime import datetime -from typing import Union +from typing import Optional, Union -from aiohttp import ClientError  from discord import Member, Object, User  from discord.ext.commands import Context +from bot.api import ResponseCodeError  from bot.constants import Keys  log = logging.getLogger(__name__) @@ -21,8 +21,8 @@ async def post_infraction(      expires_at: datetime = None,      hidden: bool = False,      active: bool = True, -): - +) -> Optional[dict]: +    """Posts an infraction to the API."""      payload = {          "actor": ctx.message.author.id,          "hidden": hidden, @@ -35,12 +35,38 @@ async def post_infraction(          payload['expires_at'] = expires_at.isoformat()      try: -        response = await ctx.bot.api_client.post( -            'bot/infractions', json=payload -        ) -    except ClientError: -        log.exception("There was an error adding an infraction.") -        await ctx.send(":x: There was an error adding the infraction.") -        return +        response = await ctx.bot.api_client.post('bot/infractions', json=payload) +    except ResponseCodeError as exp: +        if exp.status == 400 and 'user' in exp.response_json: +            log.info( +                f"{ctx.author} tried to add a {type} infraction to `{user.id}`, " +                "but that user id was not found in the database." +            ) +            await ctx.send(f":x: Cannot add infraction, the specified user is not known to the database.") +            return +        else: +            log.exception("An unexpected ResponseCodeError occurred while adding an infraction:") +            await ctx.send(":x: There was an error adding the infraction.") +            return      return response + + +async def already_has_active_infraction(ctx: Context, user: Union[Member, Object, User], type: str) -> bool: +    """Checks if a user already has an active infraction of the given type.""" +    active_infractions = await ctx.bot.api_client.get( +        'bot/infractions', +        params={ +            'active': 'true', +            'type': type, +            'user__id': str(user.id) +        } +    ) +    if active_infractions: +        await ctx.send( +            f":x: According to my records, this user already has a {type} infraction. " +            f"See infraction **#{active_infractions[0]['id']}**." +        ) +        return True +    else: +        return False | 
