Skip to content

Commit 16bfbd0

Browse files
committed
Refactor 'showcase rank' command code to display date range correctly in output in 'showcase' extension
1 parent 4022940 commit 16bfbd0

File tree

1 file changed

+61
-29
lines changed

1 file changed

+61
-29
lines changed

pcbot/exts/showcase.py

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -196,31 +196,42 @@ async def thread_triple(thread: discord.Thread):
196196
count_thread_reactions(thread, starter_message),
197197
)
198198

199-
thread_triples = sorted(
200-
[
201-
triple
202-
for thread in itertools.chain(
203-
sorted(channel.threads, key=lambda t: t.id, reverse=True),
204-
[
205-
thread
206-
async for thread in channel.archived_threads(
207-
limit=amount
208-
- len(channel.threads), # subtract active threads
209-
before=before,
210-
)
211-
],
212-
)
213-
if (
214-
before_ts is None
215-
or discord.utils.snowflake_time(thread.id) < before_ts
216-
)
217-
and (
218-
after_ts is None
219-
or discord.utils.snowflake_time(thread.id) > after_ts
220-
)
221-
and (triple := (await thread_triple(thread)))
222-
and any(tag.name.lower() in tags for tag in triple[0].applied_tags)
223-
][:amount],
199+
max_archived_threads = max(
200+
amount - len(channel.threads), 0
201+
) # subtract active threads
202+
203+
thread_triples = sorted( # sort triples by reaction count
204+
(
205+
sorted_thread_triples := [
206+
# retrieve threads as
207+
# (thread, message, reaction_count) tuples within time range
208+
# in descending order
209+
triple
210+
for thread in itertools.chain(
211+
sorted(channel.threads, key=lambda t: t.id, reverse=True),
212+
(
213+
[
214+
thread
215+
async for thread in channel.archived_threads(
216+
limit=max_archived_threads,
217+
)
218+
]
219+
if max_archived_threads
220+
else (())
221+
),
222+
)
223+
if (
224+
before_ts is None
225+
or discord.utils.snowflake_time(thread.id) < before_ts
226+
)
227+
and (
228+
after_ts is None
229+
or discord.utils.snowflake_time(thread.id) > after_ts
230+
)
231+
and (triple := (await thread_triple(thread)))
232+
and any(tag.name.lower() in tags for tag in triple[0].applied_tags)
233+
][:amount]
234+
),
224235
key=lambda tup: tup[2],
225236
reverse=True,
226237
)
@@ -233,8 +244,24 @@ async def thread_triple(thread: discord.Thread):
233244
embed_dict = {
234245
"title": f"Showcase Rankings for {channel.mention} Posts by Emoji\n"
235246
f"({len(thread_triples)} selected, from "
236-
f"<t:{int(discord.utils.snowflake_time(thread_triples[0][0].id).timestamp())}> "
237-
f"to <t:{int(discord.utils.snowflake_time(thread_triples[-1][0].id).timestamp())}>)",
247+
"<t:"
248+
+ str(
249+
int(
250+
discord.utils.snowflake_time(
251+
sorted_thread_triples[-1][0].id
252+
).timestamp()
253+
)
254+
)
255+
+ "> "
256+
f"to <t:"
257+
+ str(
258+
int(
259+
discord.utils.snowflake_time(
260+
sorted_thread_triples[0][0].id
261+
).timestamp()
262+
)
263+
)
264+
+ ">)",
238265
"color": self.theme_color.value,
239266
"fields": [],
240267
}
@@ -260,10 +287,12 @@ async def thread_triple(thread: discord.Thread):
260287
)
261288
)
262289

290+
# divide embed dict into lists of multiple embed dicts if necessary
263291
response_embed_dict_lists = [
264292
snakecore.utils.embeds.split_embed_dict(embed_dict)
265293
]
266294

295+
# group those lists based on the total character count of the embeds
267296
for i in range(len(response_embed_dict_lists)):
268297
response_embed_dicts_list = response_embed_dict_lists[i]
269298
total_char_count = 0
@@ -275,8 +304,11 @@ async def thread_triple(thread: discord.Thread):
275304
response_embed_dict
276305
)
277306
) > snakecore.utils.embeds.EMBED_TOTAL_CHAR_LIMIT:
278-
response_embed_dict_lists.insert( # slice up the response embed dict list to fit the character limit per message
279-
i + 1, response_embed_dicts_list[j : j + 1]
307+
response_embed_dict_lists.insert(
308+
# slice up the response embed dict list to fit the character
309+
# limit per message
310+
i + 1,
311+
response_embed_dicts_list[j : j + 1],
280312
)
281313
response_embed_dict_lists[i] = response_embed_dicts_list[:j]
282314
else:

0 commit comments

Comments
 (0)