diff options
author | 2022-02-14 23:41:14 +0000 | |
---|---|---|
committer | 2022-02-14 23:41:14 +0000 | |
commit | 657aaf5f613b0bd219d67c71f7dc37ede0bb6aab (patch) | |
tree | 548e48ae21a98d5e6e16e8619d1af4ee75fe8b7a /bot/monkey_patches.py | |
parent | fix: Add newlines in codeblock formatting (diff) | |
parent | Merge pull request #1026 from Shom770/fix-trivia-night (diff) |
Merge branch 'main' into merge-github-issues
Diffstat (limited to 'bot/monkey_patches.py')
-rw-r--r-- | bot/monkey_patches.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/bot/monkey_patches.py b/bot/monkey_patches.py index fa6627d1..925d3206 100644 --- a/bot/monkey_patches.py +++ b/bot/monkey_patches.py @@ -1,10 +1,12 @@ 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): @@ -65,3 +67,25 @@ def patch_typing() -> None: 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) |