diff --git a/server/lib/drivers/discord/commands.ts b/server/lib/drivers/discord/commands.ts index 1b781e1..bbb879c 100644 --- a/server/lib/drivers/discord/commands.ts +++ b/server/lib/drivers/discord/commands.ts @@ -75,16 +75,44 @@ export const APP_COMMANDS = [ { name: MSG_CMD_DEL, type: COMMAND_TYPE.MESSAGE }, ]; +async function shortHash(text: string): Promise { + const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text)); + return [...new Uint8Array(digest).slice(0, 8)] + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); +} + +/** + * Registration markers are kept per application (and guild) and hold the hash + * of the command definitions last registered there: another application, or + * definitions that differ from the registered ones — newer or rolled back — + * register again instead of being skipped. + */ +function commandsHash(): Promise { + return shortHash(JSON.stringify(APP_COMMANDS)); +} + +const applicationIds = new Map(); + export async function getApplicationId(env: Env): Promise { if (env.DISCORD_APPLICATION_ID) return env.DISCORD_APPLICATION_ID; + const token = env.DISCORD_TOKEN ?? ""; + if (!token) return null; + // Cached per bot token: a token of another application never gets the + // id resolved for the previous one. Held in memory too (one KV read per + // isolate, not one per sync run). + const cacheKey = `config:discord-app-id:${await shortHash(token)}`; + const known = applicationIds.get(cacheKey); + if (known) return known; try { - const cached = await env.KV.get("config:discord-app-id"); - if (cached) return cached; + const cached = await env.KV.get(cacheKey); + if (cached) { + applicationIds.set(cacheKey, cached); + return cached; + } } catch { // fall through to the API } - const token = env.DISCORD_TOKEN ?? ""; - if (!token) return null; const res = await fetch(`${DISCORD_API}/oauth2/applications/@me`, { headers: { Authorization: `Bot ${token}` }, }); @@ -95,7 +123,8 @@ export async function getApplicationId(env: Env): Promise { const app = (await res.json()) as { id?: string }; if (app.id) { try { - await env.KV.put("config:discord-app-id", app.id); + applicationIds.set(cacheKey, app.id); + await env.KV.put(cacheKey, app.id); } catch { // cache is best-effort } @@ -107,13 +136,15 @@ export async function getApplicationId(env: Env): Promise { export async function registerGlobalCommands(env: Env): Promise { const token = env.DISCORD_TOKEN ?? ""; if (!token) return; + const appId = await getApplicationId(env); + if (!appId) return; + const marker = `cmd:registered:global:${appId}`; + const hash = await commandsHash(); try { - if (await env.KV.get("cmd:registered:global")) return; + if ((await env.KV.get(marker)) === hash) return; } catch { // fall through and register } - const appId = await getApplicationId(env); - if (!appId) return; const res = await fetch(`${DISCORD_API}/applications/${appId}/commands`, { method: "PUT", headers: { Authorization: `Bot ${token}`, "Content-Type": "application/json" }, @@ -121,7 +152,7 @@ export async function registerGlobalCommands(env: Env): Promise { }); if (res.ok) { try { - await env.KV.put("cmd:registered:global", "1", { expirationTtl: 86400 }); + await env.KV.put(marker, hash, { expirationTtl: 86400 }); } catch { // best-effort } @@ -132,38 +163,110 @@ export async function registerGlobalCommands(env: Env): Promise { } } +const GUILD_PAGE_SIZE = 200; +/** + * Discord calls one run makes for guild commands — pages listed and + * registrations together — well within a Worker invocation's subrequest + * limit (50 on the Free plan, shared with the run's other work). A run picks + * up where the previous one stopped, so a bot in any number of guilds keeps + * making progress. + */ +const GUILD_CALLS_PER_RUN = 30; + +/** Per application: which guild holds which definitions, and where the walk is. */ +interface GuildRecord { + hashes: Record; + /** The guild after which the next run continues ("" from the start). */ + after: string; +} + export async function syncGuildCommands(env: Env): Promise { const token = env.DISCORD_TOKEN ?? ""; if (!token) return; const appId = await getApplicationId(env); if (!appId) return; - const res = await fetch(`${DISCORD_API}/users/@me/guilds`, { - headers: { Authorization: `Bot ${token}` }, - }); - if (!res.ok) { - const err = await res.text(); - log.warn({ status: res.status, err }, "Failed to list guilds"); - return; - } - const guilds = (await res.json()) as Array<{ id: string }>; - for (const guild of guilds) { - try { - if (await env.KV.get(`cmd:guild:${guild.id}`)) continue; - const r = await fetch(`${DISCORD_API}/applications/${appId}/guilds/${guild.id}/commands`, { - method: "PUT", - headers: { Authorization: `Bot ${token}`, "Content-Type": "application/json" }, - body: JSON.stringify(APP_COMMANDS), - }); - if (r.ok) { - await env.KV.put(`cmd:guild:${guild.id}`, "1"); - log.info({ guildId: guild.id }, "Registered guild application commands"); - } else { - const err = await r.text(); - log.warn({ guildId: guild.id, status: r.status, err }, "Command registration failed"); - } - } catch (err) { - log.warn({ guildId: guild.id, err: String(err) }, "Command registration failed"); + const hash = await commandsHash(); + // One KV record per application, read and written once per run (not one + // read per guild). + const recordKey = `cmd:guilds:${appId}`; + const stored = await env.KV.get>(recordKey, "json").catch( + () => null, + ); + const record: GuildRecord = + stored && typeof stored === "object" && "hashes" in stored + ? { + hashes: { ...(stored as GuildRecord).hashes }, + after: (stored as GuildRecord).after ?? "", + } + : { hashes: { ...((stored as Record | null) ?? {}) }, after: "" }; + const startedAfter = record.after; + let calls = GUILD_CALLS_PER_RUN; + let changed = false; + /** Stopped at a guild still to register: where to resume must be kept. */ + let pending = false; + /** A guild needed registering this run (successful or not). */ + let attempted = false; + walk: while (calls > 0) { + calls--; + const res = await fetch( + `${DISCORD_API}/users/@me/guilds?limit=${GUILD_PAGE_SIZE}${record.after ? `&after=${record.after}` : ""}`, + { headers: { Authorization: `Bot ${token}` } }, + ); + if (!res.ok) { + const err = await res.text(); + log.warn({ status: res.status, err }, "Failed to list guilds"); + break; } + const page = (await res.json()) as Array<{ id: string }>; + for (const guild of page) { + if (record.hashes[guild.id] !== hash) { + attempted = true; + // Out of calls: the next run resumes at this guild. + if (calls <= 0) { + pending = true; + break walk; + } + calls--; + try { + const r = await fetch( + `${DISCORD_API}/applications/${appId}/guilds/${guild.id}/commands`, + { + method: "PUT", + headers: { Authorization: `Bot ${token}`, "Content-Type": "application/json" }, + body: JSON.stringify(APP_COMMANDS), + }, + ); + if (r.ok) { + record.hashes[guild.id] = hash; + changed = true; + log.info({ guildId: guild.id }, "Registered guild application commands"); + } else { + const err = await r.text(); + log.warn({ guildId: guild.id, status: r.status, err }, "Command registration failed"); + } + } catch (err) { + log.warn({ guildId: guild.id, err: String(err) }, "Command registration failed"); + } + } + record.after = guild.id; + } + if (page.length < GUILD_PAGE_SIZE) { + // The end of the list: the next run starts over (picking up guilds + // joined meanwhile, and redoing ones whose registration failed). + record.after = ""; + break; + } + } + // Out of calls right after a full page, with work found on the way: the + // walk must resume past it (or the guilds after it would never be reached). + if (calls <= 0 && record.after !== "" && attempted) pending = true; + // Written only when something changed — a registration, a walk stopped + // with work left, or the cursor reset after one: a run that only checked + // writes nothing (KV writes are scarce; this runs every few minutes). + if (changed || pending || (record.after === "" && startedAfter !== "")) { + await env.KV.put(recordKey, JSON.stringify(record)).catch((err: unknown) => + log.warn({ err: String(err) }, "Failed to record guild command registrations"), + ); } }