Skip to content

Commit 3a774d8

Browse files
committed
Merge branch 'fetch-users' of git://github.com/Qwerty-133/modmail into Qwerty-133-fetch-users
2 parents af13ec4 + 0e4c374 commit 3a774d8

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

bot.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ async def on_ready(self):
582582
continue
583583

584584
await thread.close(
585-
closer=self.get_user(items["closer_id"]),
585+
closer=await self.get_or_fetch_user(items["closer_id"]),
586586
after=after,
587587
silent=items["silent"],
588588
delete_channel=items["delete_channel"],
@@ -656,6 +656,15 @@ async def convert_emoji(self, name: str) -> str:
656656
raise
657657
return name
658658

659+
async def get_or_fetch_user(self, id: int) -> discord.User:
660+
"""
661+
Retrieve a User based on their ID.
662+
663+
This tries getting the user from the cache and falls back to making
664+
an API call if they're not found in the cache.
665+
"""
666+
return self.get_user(id) or await self.fetch_user(id)
667+
659668
async def retrieve_emoji(self) -> typing.Tuple[str, str]:
660669

661670
sent_emoji = self.config["sent_emoji"]

cogs/modmail.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ async def logs(self, ctx, *, user: User = None):
10531053
thread = ctx.thread
10541054
if not thread:
10551055
raise commands.MissingRequiredArgument(SimpleNamespace(name="member"))
1056-
user = thread.recipient or await self.bot.fetch_user(thread.id)
1056+
user = thread.recipient or await self.bot.get_or_fetch_user(thread.id)
10571057

10581058
default_avatar = "https://cdn.discordapp.com/embed/avatars/0.png"
10591059
icon_url = getattr(user, "avatar_url", default_avatar)
@@ -1510,15 +1510,12 @@ async def blocked(self, ctx):
15101510
logger.debug("No longer blocked, user %s.", id_)
15111511
continue
15121512

1513-
user = self.bot.get_user(int(id_))
1514-
if user:
1515-
users.append((user.mention, reason))
1513+
try:
1514+
user = await self.bot.get_or_fetch_user(int(id_))
1515+
except discord.NotFound:
1516+
users.append((id_, reason))
15161517
else:
1517-
try:
1518-
user = await self.bot.fetch_user(id_)
1519-
users.append((user.mention, reason))
1520-
except discord.NotFound:
1521-
users.append((id_, reason))
1518+
users.append((user.mention, reason))
15221519

15231520
blocked_roles = list(self.bot.blocked_roles.items())
15241521
for id_, reason in blocked_roles:
@@ -1858,7 +1855,7 @@ async def repair(self, ctx):
18581855
user_id = match_user_id(message.embeds[0].footer.text, any_string=True)
18591856
other_recipients = match_other_recipients(ctx.channel.topic)
18601857
for n, uid in enumerate(other_recipients):
1861-
other_recipients[n] = self.bot.get_user(uid) or await self.bot.fetch_user(uid)
1858+
other_recipients[n] = await self.bot.get_or_fetch_user(uid)
18621859

18631860
if user_id != -1:
18641861
recipient = self.bot.get_user(user_id)
@@ -1911,7 +1908,7 @@ async def repair(self, ctx):
19111908

19121909
other_recipients = match_other_recipients(ctx.channel.topic)
19131910
for n, uid in enumerate(other_recipients):
1914-
other_recipients[n] = self.bot.get_user(uid) or await self.bot.fetch_user(uid)
1911+
other_recipients[n] = await self.bot.get_or_fetch_user(uid)
19151912

19161913
if recipient is None:
19171914
self.bot.threads.cache[user.id] = thread = Thread(

core/thread.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ async def from_channel(cls, manager: "ThreadManager", channel: discord.TextChann
125125
if recipient_id in manager.cache:
126126
thread = manager.cache[recipient_id]
127127
else:
128-
recipient = manager.bot.get_user(recipient_id) or await manager.bot.fetch_user(recipient_id)
128+
recipient = await manager.bot.get_or_fetch_user(recipient_id)
129129

130130
other_recipients = []
131131
for uid in other_ids:
132132
try:
133-
other_recipient = manager.bot.get_user(uid) or await manager.bot.fetch_user(uid)
133+
other_recipient = await manager.bot.get_or_fetch_user(uid)
134134
except discord.NotFound:
135135
continue
136136
other_recipients.append(other_recipient)
@@ -1304,14 +1304,14 @@ async def _find_from_channel(self, channel):
13041304
return self.cache[user_id]
13051305

13061306
try:
1307-
recipient = self.bot.get_user(user_id) or await self.bot.fetch_user(user_id)
1307+
recipient = await self.bot.get_or_fetch_user(user_id)
13081308
except discord.NotFound:
13091309
recipient = None
13101310

13111311
other_recipients = []
13121312
for uid in other_ids:
13131313
try:
1314-
other_recipient = self.bot.get_user(uid) or await self.bot.fetch_user(uid)
1314+
other_recipient = await self.bot.get_or_fetch_user(uid)
13151315
except discord.NotFound:
13161316
continue
13171317
other_recipients.append(other_recipient)

0 commit comments

Comments
 (0)