feat: Lazy member chunking.
No longer try to fetch all members on startup. Instead chunk on-demand.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
@@ -91,7 +92,11 @@ class StatsCog(LionCog):
|
||||
timestamp=utc_now(),
|
||||
)
|
||||
await ctx.interaction.response.send_message(embed=waiting_embed)
|
||||
await ctx.guild.chunk()
|
||||
try:
|
||||
await asyncio.wait_for(ctx.guild.chunk(), timeout=10)
|
||||
pass
|
||||
except asyncio.TimeoutError:
|
||||
pass
|
||||
else:
|
||||
await ctx.interaction.response.defer(thinking=True)
|
||||
ui = LeaderboardUI(self.bot, ctx.author, ctx.guild)
|
||||
|
||||
@@ -75,6 +75,8 @@ class LeaderboardUI(StatsUI):
|
||||
# (type, period) -> (pagen -> Optional[Future[Card]])
|
||||
self.cache = {}
|
||||
|
||||
self.was_chunked: bool = guild.chunked
|
||||
|
||||
async def run(self, interaction: discord.Interaction):
|
||||
self._original = interaction
|
||||
|
||||
@@ -136,6 +138,7 @@ class LeaderboardUI(StatsUI):
|
||||
|
||||
# Filter out members which are not in the server and unranked roles and bots
|
||||
# Usually hits cache
|
||||
self.was_chunked = self.guild.chunked
|
||||
unranked_setting = await self.bot.get_cog('StatsCog').settings.UnrankedRoles.get(self.guild.id)
|
||||
unranked_roleids = set(unranked_setting.data)
|
||||
true_leaderboard = []
|
||||
@@ -435,12 +438,19 @@ class LeaderboardUI(StatsUI):
|
||||
Generate UI message arguments from stored data
|
||||
"""
|
||||
t = self.bot.translator.t
|
||||
chunk_warning = t(_p(
|
||||
'ui:leaderboard|chunk_warning',
|
||||
"**Note:** Could not retrieve member list from Discord, so some members may be missing. "
|
||||
"Try again in a minute!"
|
||||
))
|
||||
if self.card is not None:
|
||||
period_start = self.period_starts[self.current_period]
|
||||
header = t(_p(
|
||||
'ui:leaderboard|since',
|
||||
"Counting statistics since {timestamp}"
|
||||
)).format(timestamp=discord.utils.format_dt(period_start))
|
||||
if not self.was_chunked:
|
||||
header = '\n'.join((header, chunk_warning))
|
||||
args = MessageArgs(
|
||||
embed=None,
|
||||
content=header,
|
||||
@@ -473,7 +483,11 @@ class LeaderboardUI(StatsUI):
|
||||
)),
|
||||
description=empty_description
|
||||
)
|
||||
args = MessageArgs(content=None, embed=embed, files=[])
|
||||
args = MessageArgs(
|
||||
content=chunk_warning if not self.was_chunked else None,
|
||||
embed=embed,
|
||||
files=[]
|
||||
)
|
||||
return args
|
||||
|
||||
async def refresh_components(self):
|
||||
|
||||
Reference in New Issue
Block a user