Skip to content

Commit f94aad8

Browse files
sebkuipkhakers
authored andcommitted
Improve guild member cache fetch logic and fix escaping in message image url search regex
Merge the pydis changes to the bot (modmail-dev#3365) * Escape hyphen in regex string Unescaped this was permitting any character between $ (index 36) and _ (index 95), which aren't all valid in urls. * Get or fetch member when trying to reply This fixes an issue where the member may not be in the cache --------- Signed-off-by: Taku <45324516+Taaku18@users.noreply.github.com> Co-authored-by: Taku <45324516+Taaku18@users.noreply.github.com> Co-authored-by: Chris Lovering <chris.lovering.95@gmail.com> (cherry picked from commit 1a3bda8)
1 parent 59b9487 commit f94aad8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

bot.py

+10
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,16 @@ async def get_or_fetch_user(self, id: int) -> discord.User:
662662
"""
663663
return self.get_user(id) or await self.fetch_user(id)
664664

665+
@staticmethod
666+
async def get_or_fetch_member(guild: discord.Guild, member_id: int) -> typing.Optional[discord.Member]:
667+
"""
668+
Attempt to get a member from cache; on failure fetch from the API.
669+
670+
Returns:
671+
The :obj:`discord.Member` or :obj:`None` to indicate the member could not be found.
672+
"""
673+
return guild.get_member(member_id) or await guild.fetch_member(member_id)
674+
665675
async def retrieve_emoji(self) -> typing.Tuple[str, str]:
666676
sent_emoji = self.config["sent_emoji"]
667677
blocked_emoji = self.config["blocked_emoji"]

core/thread.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,13 @@ async def reply(
835835
"""Returns List[user_dm_msg] and thread_channel_msg"""
836836
if not message.content and not message.attachments:
837837
raise MissingRequiredArgument(DummyParam("msg"))
838-
if not any(g.get_member(self.id) for g in self.bot.guilds):
838+
for guild in self.bot.guilds:
839+
try:
840+
if await self.bot.get_or_fetch_member(guild, self.id):
841+
break
842+
except discord.NotFound:
843+
pass
844+
else:
839845
return await message.channel.send(
840846
embed=discord.Embed(
841847
color=self.bot.error_color,
@@ -1007,7 +1013,7 @@ async def send(
10071013
attachments.append(attachment)
10081014

10091015
image_urls = re.findall(
1010-
r"http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+",
1016+
r"http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$\-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+",
10111017
message.content,
10121018
)
10131019

0 commit comments

Comments
 (0)