Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a utility to get/fetch a user and use it when closing pending threads. #3105

Merged
merged 4 commits into from
Nov 19, 2021
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
11 changes: 10 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ async def on_ready(self):
continue

await thread.close(
closer=self.get_user(items["closer_id"]),
closer=await self.get_or_fetch_user(items["closer_id"]),
after=after,
silent=items["silent"],
delete_channel=items["delete_channel"],
Expand Down Expand Up @@ -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"]
Expand Down
19 changes: 8 additions & 11 deletions cogs/modmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"