aboutsummaryrefslogtreecommitdiffstats
path: root/bot/monkey_patches.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot/monkey_patches.py')
-rw-r--r--bot/monkey_patches.py30
1 files changed, 27 insertions, 3 deletions
diff --git a/bot/monkey_patches.py b/bot/monkey_patches.py
index e56a19da2..b5c0de8d9 100644
--- a/bot/monkey_patches.py
+++ b/bot/monkey_patches.py
@@ -1,9 +1,11 @@
-from datetime import datetime, timedelta
+from datetime import timedelta
+import arrow
from discord import Forbidden, http
from discord.ext import commands
from bot.log import get_logger
+from bot.utils.regex import MESSAGE_ID_RE
log = get_logger(__name__)
@@ -38,14 +40,36 @@ def patch_typing() -> 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):
+ if last_403 and (arrow.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()
+ last_403 = arrow.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)