From 778574ef52de95b593e22014c9aa36f402ae0b27 Mon Sep 17 00:00:00 2001 From: Yee Jia Rong <28086837+fourjr@users.noreply.github.com> Date: Sun, 5 Sep 2021 01:21:46 +0800 Subject: [PATCH 1/4] oops --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e7438ca373..4dc7d411a5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,5 +34,5 @@ repository = 'https://github.com/kyb3r/modmail' homepage = 'https://github.com/kyb3r/modmail' keywords = ['discord', 'modmail'] -[tool.pylint.format]react_to_contact_message +[tool.pylint.format] max-line-length = "110" From c65a5b785473786e96949c5237e688ca5c46b6a9 Mon Sep 17 00:00:00 2001 From: Qwerty-133 <74311372+Qwerty-133@users.noreply.github.com> Date: Tue, 21 Sep 2021 21:26:48 +0530 Subject: [PATCH 2/4] Fetch thread closers if needed. --- bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 7d4248173c..a04661cc5f 100644 --- a/bot.py +++ b/bot.py @@ -582,7 +582,7 @@ async def on_ready(self): continue await thread.close( - closer=self.get_user(items["closer_id"]), + closer=self.get_user(items["closer_id"]) or await self.fetch_user(items["closer_id"]), after=after, silent=items["silent"], delete_channel=items["delete_channel"], From e87669c214ece58558e2cf9566e632e21f826634 Mon Sep 17 00:00:00 2001 From: Qwerty-133 <74311372+Qwerty-133@users.noreply.github.com> Date: Tue, 21 Sep 2021 21:27:08 +0530 Subject: [PATCH 3/4] Add the get_or_fetch_user method. --- bot.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bot.py b/bot.py index a04661cc5f..8cafda33b0 100644 --- a/bot.py +++ b/bot.py @@ -656,6 +656,15 @@ async def convert_emoji(self, name: str) -> str: raise return name + async def get_or_fetch_user(self, id: int) -> discord.User: + """ + Retrieve a User based on their ID. + + This tries getting the user from the cache and falls back to making + an API call if they're not found in the cache. + """ + return self.get_user(id) or await self.fetch_user(id) + async def retrieve_emoji(self) -> typing.Tuple[str, str]: sent_emoji = self.config["sent_emoji"] From 0e4c3749e4ccd4aa05837f9531b506d052fec781 Mon Sep 17 00:00:00 2001 From: Qwerty-133 <74311372+Qwerty-133@users.noreply.github.com> Date: Tue, 21 Sep 2021 21:34:41 +0530 Subject: [PATCH 4/4] Use the method where possible to tidy up code. --- bot.py | 2 +- cogs/modmail.py | 19 ++++++++----------- core/thread.py | 8 ++++---- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/bot.py b/bot.py index 8cafda33b0..f19725a954 100644 --- a/bot.py +++ b/bot.py @@ -582,7 +582,7 @@ async def on_ready(self): continue await thread.close( - closer=self.get_user(items["closer_id"]) or await self.fetch_user(items["closer_id"]), + closer=await self.get_or_fetch_user(items["closer_id"]), after=after, silent=items["silent"], delete_channel=items["delete_channel"], diff --git a/cogs/modmail.py b/cogs/modmail.py index bb3860bfde..814113f617 100644 --- a/cogs/modmail.py +++ b/cogs/modmail.py @@ -1042,7 +1042,7 @@ async def logs(self, ctx, *, user: User = None): thread = ctx.thread if not thread: raise commands.MissingRequiredArgument(SimpleNamespace(name="member")) - user = thread.recipient or await self.bot.fetch_user(thread.id) + user = thread.recipient or await self.bot.get_or_fetch_user(thread.id) default_avatar = "https://cdn.discordapp.com/embed/avatars/0.png" icon_url = getattr(user, "avatar_url", default_avatar) @@ -1492,15 +1492,12 @@ async def blocked(self, ctx): logger.debug("No longer blocked, user %s.", id_) continue - user = self.bot.get_user(int(id_)) - if user: - users.append((user.mention, reason)) + try: + user = await self.bot.get_or_fetch_user(int(id_)) + except discord.NotFound: + users.append((id_, reason)) else: - try: - user = await self.bot.fetch_user(id_) - users.append((user.mention, reason)) - except discord.NotFound: - users.append((id_, reason)) + users.append((user.mention, reason)) blocked_roles = list(self.bot.blocked_roles.items()) for id_, reason in blocked_roles: @@ -1840,7 +1837,7 @@ async def repair(self, ctx): user_id = match_user_id(message.embeds[0].footer.text) other_recipients = match_other_recipients(ctx.channel.topic) for n, uid in enumerate(other_recipients): - other_recipients[n] = self.bot.get_user(uid) or await self.bot.fetch_user(uid) + other_recipients[n] = await self.bot.get_or_fetch_user(uid) if user_id != -1: recipient = self.bot.get_user(user_id) @@ -1893,7 +1890,7 @@ async def repair(self, ctx): other_recipients = match_other_recipients(ctx.channel.topic) for n, uid in enumerate(other_recipients): - other_recipients[n] = self.bot.get_user(uid) or await self.bot.fetch_user(uid) + other_recipients[n] = await self.bot.get_or_fetch_user(uid) if recipient is None: self.bot.threads.cache[user.id] = thread = Thread( diff --git a/core/thread.py b/core/thread.py index 54509cdc36..c4fa7bbf07 100644 --- a/core/thread.py +++ b/core/thread.py @@ -126,12 +126,12 @@ async def from_channel(cls, manager: "ThreadManager", channel: discord.TextChann if recipient_id in manager.cache: thread = manager.cache[recipient_id] else: - recipient = manager.bot.get_user(recipient_id) or await manager.bot.fetch_user(recipient_id) + recipient = await manager.bot.get_or_fetch_user(recipient_id) other_recipients = [] for uid in match_other_recipients(channel.topic): try: - other_recipient = manager.bot.get_user(uid) or await manager.bot.fetch_user(uid) + other_recipient = await manager.bot.get_or_fetch_user(uid) except discord.NotFound: continue other_recipients.append(other_recipient) @@ -1243,14 +1243,14 @@ async def _find_from_channel(self, channel): return self.cache[user_id] try: - recipient = self.bot.get_user(user_id) or await self.bot.fetch_user(user_id) + recipient = await self.bot.get_or_fetch_user(user_id) except discord.NotFound: recipient = None other_recipients = [] for uid in match_other_recipients(channel.topic): try: - other_recipient = self.bot.get_user(uid) or await self.bot.fetch_user(uid) + other_recipient = await self.bot.get_or_fetch_user(uid) except discord.NotFound: continue other_recipients.append(other_recipient)