diff options
| author | 2022-09-23 22:58:49 +0100 | |
|---|---|---|
| committer | 2022-09-23 22:58:49 +0100 | |
| commit | f9cc77f55a7bac9cff1f5674b36b3f17560f6bfe (patch) | |
| tree | 4d9a9684d6c0d8f1f749355353fbadb7fd89960b /bot/monkey_patches.py | |
| parent | Fix issue #1050 (#1097) (diff) | |
| parent | Remove all wait_until_guil_available as this is now done in bot-core (diff) | |
Merge pull request #1092 from python-discord/bot-core-migration
Diffstat (limited to 'bot/monkey_patches.py')
| -rw-r--r-- | bot/monkey_patches.py | 91 | 
1 files changed, 0 insertions, 91 deletions
| diff --git a/bot/monkey_patches.py b/bot/monkey_patches.py deleted file mode 100644 index 925d3206..00000000 --- a/bot/monkey_patches.py +++ /dev/null @@ -1,91 +0,0 @@ -import logging -import re -from datetime import datetime, timedelta - -from discord import Forbidden, http -from discord.ext import commands - -log = logging.getLogger(__name__) -MESSAGE_ID_RE = re.compile(r"(?P<message_id>[0-9]{15,20})$") - - -class Command(commands.Command): -    """ -    A `discord.ext.commands.Command` subclass which supports root aliases. - -    A `root_aliases` keyword argument is added, which is a sequence of alias names that will act as -    top-level commands rather than being aliases of the command's group. It's stored as an attribute -    also named `root_aliases`. -    """ - -    def __init__(self, *args, **kwargs): -        super().__init__(*args, **kwargs) -        self.root_aliases = kwargs.get("root_aliases", []) - -        if not isinstance(self.root_aliases, (list, tuple)): -            raise TypeError("Root aliases of a command must be a list or a tuple of strings.") - - -class Group(commands.Group): -    """ -    A `discord.ext.commands.Group` subclass which supports root aliases. - -    A `root_aliases` keyword argument is added, which is a sequence of alias names that will act as -    top-level groups rather than being aliases of the command's group. It's stored as an attribute -    also named `root_aliases`. -    """ - -    def __init__(self, *args, **kwargs): -        super().__init__(*args, **kwargs) -        self.root_aliases = kwargs.get("root_aliases", []) - -        if not isinstance(self.root_aliases, (list, tuple)): -            raise TypeError("Root aliases of a group must be a list or a tuple of strings.") - - -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.debug("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.utcnow() - 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.utcnow() -            log.warning("Got a 403 from typing event!") -            pass - -    http.HTTPClient.send_typing = honeybadger_type - - -class FixedPartialMessageConverter(commands.PartialMessageConverter): -    """ -    Make the Message converter infer channelID from the given context if only a messageID is given. - -    Discord.py's Message converter is supposed to infer channelID based -    on ctx.channel if only a messageID is given. A refactor commit, linked below, -    a few weeks before d.py's archival broke this defined behaviour of the converter. -    Currently, if only a messageID is given to the converter, it will only find that message -    if it's in the bot's cache. - -    https://github.com/Rapptz/discord.py/commit/1a4e73d59932cdbe7bf2c281f25e32529fc7ae1f -    """ - -    @staticmethod -    def _get_id_matches(ctx: commands.Context, argument: str) -> tuple[int, int, int]: -        """Inserts ctx.channel.id before calling super method if argument is just a messageID.""" -        match = MESSAGE_ID_RE.match(argument) -        if match: -            argument = f"{ctx.channel.id}-{match.group('message_id')}" -        return commands.PartialMessageConverter._get_id_matches(ctx, argument) | 
