diff options
| author | 2021-10-04 19:30:15 +0100 | |
|---|---|---|
| committer | 2021-10-04 19:31:51 +0100 | |
| commit | 17d100e2207b6b98c7b0cb6d9c378b15b1bb4c4e (patch) | |
| tree | 5506a5a4991e5dbfed902a84a39b3c7d4615ee83 | |
| parent | Merge pull request #1841 from python-discord/allow-helpers-to-edit-their-own-... (diff) | |
Monkey patch http.send_typing to catch 403s
Sometimes discord turns off typing events by throwing 403's, so we should catch those
Diffstat (limited to '')
| -rw-r--r-- | bot/__init__.py | 4 | ||||
| -rw-r--r-- | bot/typing.py | 32 | 
2 files changed, 35 insertions, 1 deletions
| diff --git a/bot/__init__.py b/bot/__init__.py index 8f880b8e6..70ff03fd4 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING  from discord.ext import commands -from bot import log +from bot import log, typing  from bot.command import Command  if TYPE_CHECKING: @@ -17,6 +17,8 @@ log.setup()  if os.name == "nt":      asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) +typing.patch_typing() +  # Monkey-patch discord.py decorators to use the Command subclass which supports root aliases.  # Must be patched before any cogs are added.  commands.command = partial(commands.command, cls=Command) diff --git a/bot/typing.py b/bot/typing.py new file mode 100644 index 000000000..4b1df3f2f --- /dev/null +++ b/bot/typing.py @@ -0,0 +1,32 @@ +import logging +from datetime import datetime, timedelta + +from discord import Forbidden, http + +log = logging.getLogger(__name__) + + +def patch_typing() -> None: +    """ +    Sometimes discord turns off typing events by throwing 403's. + +    Handle those issues by patching the trigger_typing method so it ignores 403's in general. +    """ +    log.info("Patching send_typing, which should fix things breaking when discord disables typing events. Stay safe!") + +    original = http.HTTPClient.send_typing +    last_403 = None + +    async def honeybadger_type(self, channel_id: int) -> None:  # noqa: ANN001 +        nonlocal last_403 +        if last_403 and (datetime.now() - last_403) < timedelta(minutes=5): +            log.warning("Not sending typing event, we got a 403 less than 5 minutes ago.") +            return +        try: +            await original(self, channel_id) +        except Forbidden: +            last_403 = datetime.now() +            log.warning("Got a 403 from typing event!") +            pass + +    http.HTTPClient.send_typing = honeybadger_type | 
