Skip to content

Commit 5c87f89

Browse files
committed
Fix: always update the most recent help status
The message cache does not have a stable order (I suspect it is simply the order in which the messages were added to the cache). Thus, when searching for the latest status message, sometimes a message other than the latest was picked. In practice, it seems like this only ever happened when the bot was offline for a number of hours, which is why it hasn't come up in production. But in development, it could be reproduced by: - Leave the bot offline for a while (24h?) - Start the bot - Claim a channel - The channel that is moved up from being dormant has the wrong message edited, so the message at the bottom still says "dormant". I suspect this is what the `.reverse()`, which was removed in commit 09ad491, was intended to solve (but it caused bugs during normal operation). This should be a full fix. I am not sure of the performance implications here. I think it should not be too bad, because we are filtering to only messages from the bot, before sorting.
1 parent 17acc24 commit 5c87f89

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

Diff for: src/modules/helpchan.ts

+4
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,16 @@ export class HelpChanModule extends Module {
346346
this.DORMANT_EMBED.title,
347347
].includes(embed.title);
348348

349+
// The message cache does not have a stable order (at least with respect
350+
// to creation date), so sorting is needed to find the latest embed.
349351
let lastMessage = channel.messages.cache
350352
.array()
351353
.filter(m => m.author.id === this.client.user?.id)
354+
.sort((m1, m2) => m2.createdTimestamp - m1.createdTimestamp)
352355
.find(m => m.embeds.some(isStatusEmbed));
353356

354357
if (!lastMessage)
358+
// Fetch has a stable order, with recent messages first
355359
lastMessage = (await channel.messages.fetch({ limit: 5 }))
356360
.array()
357361
.filter(m => m.author.id === this.client.user?.id)

0 commit comments

Comments
 (0)