Fix race conditions and duplicate stars in starboard

This commit is contained in:
Dragory 2020-12-23 05:28:21 +02:00
parent 1484f6b9a7
commit e1e1854041
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
5 changed files with 63 additions and 9 deletions

View file

@ -2,6 +2,10 @@ import { Client, GuildTextableChannel, Message } from "eris";
import { noop } from "../../../utils";
import { createStarboardPseudoFooterForMessage } from "./createStarboardPseudoFooterForMessage";
import { TStarboardOpts } from "../types";
import Timeout = NodeJS.Timeout;
const DEBOUNCE_DELAY = 1000;
const debouncedUpdates: Record<string, Timeout> = {};
export async function updateStarboardMessageStarCount(
starboard: TStarboardOpts,
@ -10,8 +14,16 @@ export async function updateStarboardMessageStarCount(
starEmoji: string,
starCount: number,
) {
const embed = starboardMessage.embeds[0]!;
embed.fields!.shift(); // Remove pseudo footer
embed.fields!.push(createStarboardPseudoFooterForMessage(starboard, originalMessage, starEmoji, starCount)); // Create new pseudo footer
await starboardMessage.edit({ embed });
const key = `${originalMessage.id}-${starboardMessage.id}`;
if (debouncedUpdates[key]) {
clearTimeout(debouncedUpdates[key]);
}
debouncedUpdates[key] = setTimeout(() => {
delete debouncedUpdates[key];
const embed = starboardMessage.embeds[0]!;
embed.fields!.shift(); // Remove pseudo footer
embed.fields!.push(createStarboardPseudoFooterForMessage(starboard, originalMessage, starEmoji, starCount)); // Create new pseudo footer
starboardMessage.edit({ embed });
}, DEBOUNCE_DELAY);
}