Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Enhance /deaths, /levels and /timeline timeout by caching call to get member #135

Merged
merged 4 commits into from
Oct 10, 2018
Merged
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
46 changes: 32 additions & 14 deletions cogs/tibia.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ async def deaths(self, ctx: NabCtx, *, name: str = None):
return

if ctx.is_private:
user_guilds = self.bot.get_user_guilds(ctx.author.id)
user_servers = self.bot.get_user_guilds(ctx.author.id)
user_worlds = self.bot.get_user_worlds(ctx.author.id)
else:
user_guilds = [ctx.guild]
user_servers = [ctx.guild]
user_worlds = [self.bot.tracked_worlds.get(ctx.guild.id)]
if user_worlds[0] is None and name is None:
await ctx.send("This server is not tracking any tibia worlds.")
Expand All @@ -132,6 +132,7 @@ async def deaths(self, ctx: NabCtx, *, name: str = None):
now = time.time()
show_links = not ctx.long
per_page = 20 if ctx.long else 5
users_cache = dict()
try:
if name is None:
title = "Latest deaths"
Expand All @@ -143,11 +144,13 @@ async def deaths(self, ctx: NabCtx, *, name: str = None):
row = c.fetchone()
if row is None:
break
user = self.bot.get_member(row["user_id"], user_guilds)
if user is None:
continue
if row["world"] not in user_worlds:
continue

user = self._get_cached_user_(self, row["user_id"], users_cache, user_servers)
if user is None:
continue

count += 1
row["time"] = get_time_diff(dt.timedelta(seconds=now - row["date"]))
row["user"] = user.display_name
Expand Down Expand Up @@ -724,10 +727,10 @@ async def levels(self, ctx: NabCtx, *, name: str=None):
return

if ctx.is_private:
user_guilds = self.bot.get_user_guilds(ctx.author.id)
user_servers = self.bot.get_user_guilds(ctx.author.id)
user_worlds = self.bot.get_user_worlds(ctx.author.id)
else:
user_guilds = [ctx.guild]
user_servers = [ctx.guild]
user_worlds = [self.bot.tracked_worlds.get(ctx.guild.id)]
if user_worlds[0] is None:
await ctx.send("This server is not tracking any tibia worlds.")
Expand All @@ -741,6 +744,7 @@ async def levels(self, ctx: NabCtx, *, name: str=None):
now = time.time()
per_page = 20 if ctx.long else 5
await ctx.channel.trigger_typing()
user_cache = dict()
try:
if name is None:
title = "Latest level ups"
Expand All @@ -752,11 +756,13 @@ async def levels(self, ctx: NabCtx, *, name: str=None):
row = c.fetchone()
if row is None:
break
user = self.bot.get_member(row["user_id"], user_guilds)
if user is None:
continue
if row["world"] not in user_worlds:
continue

user = self._get_cached_user_(self, row["user_id"], user_cache, user_servers)
if user is None:
continue

count += 1
row["time"] = get_time_diff(dt.timedelta(seconds=now - row["date"]))
row["user"] = user.display_name
Expand All @@ -771,7 +777,7 @@ async def levels(self, ctx: NabCtx, *, name: str=None):
await ctx.send("I don't have a character with that name registered.")
return
# If user doesn't share a server with the owner, don't display it
owner = self.bot.get_member(result["user_id"], user_guilds)
owner = self.bot.get_member(result["user_id"], user_servers)
if owner is None:
await ctx.send("I don't have a character with that name registered.")
return
Expand Down Expand Up @@ -1288,6 +1294,7 @@ async def timeline(self, ctx: NabCtx, *, name: str = None):
now = time.time()
per_page = 20 if ctx.long else 5
await ctx.channel.trigger_typing()
user_cache = dict()
try:
if name is None:
title = "Timeline"
Expand All @@ -1303,11 +1310,13 @@ async def timeline(self, ctx: NabCtx, *, name: str = None):
row = c.fetchone()
if row is None:
break
user = self.bot.get_member(row["user_id"], user_servers)
if user is None:
continue
if row["world"] not in user_worlds:
continue

user = self._get_cached_user_(self, row["user_id"], user_cache, user_servers)
if user is None:
continue

count += 1
row["time"] = get_time_diff(dt.timedelta(seconds=now - row["date"]))
row["user"] = user.display_name
Expand Down Expand Up @@ -2055,6 +2064,15 @@ async def scan_news(self):
except Exception:
log.exception("Task: scan_news")

@staticmethod
def _get_cached_user_(self, user_id, users_cache, user_servers):
if user_id in users_cache:
return users_cache.get(user_id)
else:
member_user = self.bot.get_member(user_id, user_servers)
users_cache[user_id] = member_user
return member_user

def __unload(self):
print("cogs.tibia: Cancelling pending tasks...")
self.news_announcements_task.cancel()
Expand Down