diff options
| -rw-r--r-- | bot/api.py | 22 | ||||
| -rw-r--r-- | bot/cogs/token_remover.py | 2 | ||||
| -rw-r--r-- | bot/utils/moderation.py | 2 | 
3 files changed, 18 insertions, 8 deletions
diff --git a/bot/api.py b/bot/api.py index 36f9cfcd4..9a0ebaa26 100644 --- a/bot/api.py +++ b/bot/api.py @@ -1,5 +1,6 @@  import asyncio  import logging +from typing import Optional  from urllib.parse import quote as quote_url  import aiohttp @@ -10,13 +11,20 @@ log = logging.getLogger(__name__)  class ResponseCodeError(ValueError): -    def __init__(self, response: aiohttp.ClientResponse, response_data: dict): +    def __init__( +        self, +        response: aiohttp.ClientResponse, +        response_json: Optional[dict] = None, +        response_text: str = "" +    ):          self.status = response.status -        self.response_data = response_data +        self.response_json = response_json or {} +        self.response_text = response_text          self.response = response      def __str__(self): -        return f"Status: {self.status} Response: {self.response_data}" +        response = self.response_json if self.response_json else self.response_text +        return f"Status: {self.status} Response: {response}"  class APIClient: @@ -38,8 +46,12 @@ class APIClient:      async def maybe_raise_for_status(self, response: aiohttp.ClientResponse, should_raise: bool):          if should_raise and response.status >= 400: -            response_data = await response.json() -            raise ResponseCodeError(response=response, response_data=response_data) +            try: +                response_json = await response.json() +                raise ResponseCodeError(response=response, response_json=response_json) +            except aiohttp.ContentTypeError: +                response_text = await response.text() +                raise ResponseCodeError(response=response, response_text=response_text)      async def get(self, endpoint: str, *args, raise_for_status: bool = True, **kwargs):          async with self.session.get(self._url_for(endpoint), *args, **kwargs) as resp: diff --git a/bot/cogs/token_remover.py b/bot/cogs/token_remover.py index 05298a2ff..b2c4cd522 100644 --- a/bot/cogs/token_remover.py +++ b/bot/cogs/token_remover.py @@ -26,13 +26,11 @@ DELETION_MESSAGE_TEMPLATE = (  DISCORD_EPOCH_TIMESTAMP = datetime(2017, 1, 1)  TOKEN_EPOCH = 1_293_840_000  TOKEN_RE = re.compile( -    r"(?<=(\"|'))"  # Lookbehind: Only match if there's a double or single quote in front      r"[^\s\.]+"     # Matches token part 1: The user ID string, encoded as base64      r"\."           # Matches a literal dot between the token parts      r"[^\s\.]+"     # Matches token part 2: The creation timestamp, as an integer      r"\."           # Matches a literal dot between the token parts      r"[^\s\.]+"     # Matches token part 3: The HMAC, unused by us, but check that it isn't empty -    r"(?=(\"|'))"   # Lookahead: Only match if there's a double or single quote after  ) diff --git a/bot/utils/moderation.py b/bot/utils/moderation.py index b295e4649..7860f14a1 100644 --- a/bot/utils/moderation.py +++ b/bot/utils/moderation.py @@ -37,7 +37,7 @@ async def post_infraction(      try:          response = await ctx.bot.api_client.post('bot/infractions', json=payload)      except ResponseCodeError as exp: -        if exp.status == 400 and 'user' in exp.response_data: +        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."  |