Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

monkey_patches.patch_typing()

# This patches any convertors that use PartialMessage, but not the PartialMessageConverter itself
# as library objects are made by this mapping.
# https://github.com/Rapptz/discord.py/blob/1a4e73d59932cdbe7bf2c281f25e32529fc7ae1f/discord/ext/commands/converter.py#L984-L1004
commands.converter.PartialMessageConverter = monkey_patches.FixedPartialMessageConverter

# 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=monkey_patches.Command)
Expand Down
23 changes: 23 additions & 0 deletions bot/monkey_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from discord.ext import commands

from bot.log import get_logger
from bot.utils.regex import MESSAGE_ID_RE

log = get_logger(__name__)

Expand Down Expand Up @@ -50,3 +51,25 @@ async def honeybadger_type(self, channel_id: int) -> None: # noqa: ANN001
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.
Comment thread
ChrisLovering marked this conversation as resolved.

Discord.py's Message converter is supposed to infer channelID based
Comment thread
ChrisLovering marked this conversation as resolved.
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)
Comment thread
ChrisLovering marked this conversation as resolved.
1 change: 1 addition & 0 deletions bot/utils/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
r"(?P<invite>[a-zA-Z0-9\-]+)", # the invite code itself
flags=re.IGNORECASE
)
MESSAGE_ID_RE = re.compile(r'(?P<message_id>[0-9]{15,20})$')