fix : no startup sync

This commit is contained in:
2026-09-10 19:24:42 +07:00 Unverified
parent 89be58c517
commit cc0757d0a0
3 changed files with 18 additions and 23 deletions
+5 -5
View File
@@ -121,12 +121,12 @@ refetch authoritative state; SSE never contains page content and is not the
source of truth. source of truth.
The Discord catalog worker watches the private channel configured by The Discord catalog worker watches the private channel configured by
`DISCORD_CHANNEL_ID`. It parses Lunaris `Version Change` announcements and `DISCORD_CHANNEL_ID`. It parses new Lunaris `Version Change` announcements and
then fetches and synchronizes the latest version reported by Lunaris. On startup then fetches and synchronizes the latest version reported by Lunaris. Startup
it checks recent channel history, so it does not need to poll Lunaris while idle. only connects the listener; it does not replay channel history or start a sync.
Worker and sync results are sent to `DISCORD_LOG_CHANNEL_ID`. Enable the Discord Worker and sync results are sent to `DISCORD_LOG_CHANNEL_ID`. Enable the Discord
Message Content intent and grant the bot View Channel, Read Message History, and Message Content intent and grant the bot View Channel and Send Messages in the
Send Messages in the configured channels. configured channels.
Public snapshots use a shared Redis-backed Next.js cache. Admin routes remain Public snapshots use a shared Redis-backed Next.js cache. Admin routes remain
dynamic. SSE streams send a 90-second heartbeat and close after 30 minutes so dynamic. SSE streams send a 90-second heartbeat and close after 30 minutes so
+5 -18
View File
@@ -1,5 +1,4 @@
import { import {
ChannelType,
Client, Client,
Events, Events,
GatewayIntentBits, GatewayIntentBits,
@@ -144,10 +143,10 @@ async function drain(): Promise<void> {
} }
} }
function enqueue(version: string, source: "history" | "message"): void { function enqueue(version: string): void {
if (!pendingVersion || compareLunarisVersions(version, pendingVersion) > 0) { if (!pendingVersion || compareLunarisVersions(version, pendingVersion) > 0) {
pendingVersion = version; pendingVersion = version;
log("version_queued", { version, source }); log("version_queued", { version, source: "message" });
} }
if (!runner) { if (!runner) {
runner = drain().finally(() => { runner = drain().finally(() => {
@@ -156,13 +155,13 @@ function enqueue(version: string, source: "history" | "message"): void {
} }
} }
function handleMessage(message: Message, source: "history" | "message"): void { function handleMessage(message: Message): void {
if (message.channelId !== channelId || message.author.id === client.user?.id) return; if (message.channelId !== channelId || message.author.id === client.user?.id) return;
const change = parseLunarisVersionChange(messageText(message)); const change = parseLunarisVersionChange(messageText(message));
if (change) enqueue(change.targetVersion, source); if (change) enqueue(change.targetVersion);
} }
client.on(Events.MessageCreate, (message) => handleMessage(message, "message")); client.on(Events.MessageCreate, handleMessage);
client.on(Events.Error, (cause) => log("discord_error", { error: cause.message })); client.on(Events.Error, (cause) => log("discord_error", { error: cause.message }));
client.on(Events.Warn, (message) => log("discord_warning", { message })); client.on(Events.Warn, (message) => log("discord_warning", { message }));
@@ -186,18 +185,6 @@ try {
if (!shutdown.signal.aborted) { if (!shutdown.signal.aborted) {
log("discord_ready", { userId: client.user?.id, channelId }); log("discord_ready", { userId: client.user?.id, channelId });
await notify("worker ready", { userId: client.user?.id, channelId }); await notify("worker ready", { userId: client.user?.id, channelId });
const channel = await client.channels.fetch(channelId);
if (
!channel ||
(channel.type !== ChannelType.GuildText &&
channel.type !== ChannelType.GuildAnnouncement)
) {
throw new Error("DISCORD_CHANNEL_ID must identify a guild text channel.");
}
const messages = await channel.messages.fetch({ limit: 25 });
for (const message of messages.values()) handleMessage(message, "history");
await stopped; await stopped;
} }
} finally { } finally {
+8
View File
@@ -180,4 +180,12 @@ describe("environment template contract", () => {
expect(deployment).toContain('drop: ["ALL"]'); expect(deployment).toContain('drop: ["ALL"]');
expect(rbac).toContain("buzz-sheet-discord-worker"); expect(rbac).toContain("buzz-sheet-discord-worker");
}); });
it("syncs only for new Discord version announcements", async () => {
const worker = await repositoryFile("backend/discord.ts");
expect(worker).toContain("client.on(Events.MessageCreate, handleMessage)");
expect(worker).not.toContain("channel.messages.fetch");
expect(worker).not.toContain('source: "history"');
});
}); });