From e6c523da255e44c6f43cbdcd6b2445fd9c2a6c77 Mon Sep 17 00:00:00 2001 From: Tim Disney Date: Fri, 08 May 2026 18:29:29 +0000 Subject: [PATCH] actually fix scheduled messaging --- src/scheduler.ts | 20 ++++++++++++++------ src/util.ts | 9 ++++++--- src/tools/schedule.ts | 2 +- 3 file(s) changed, 21 insertion(s)(+), 10 deletion(s)(-) diff --git a/src/scheduler.ts b/src/scheduler.ts --- a/src/scheduler.ts +++ b/src/scheduler.ts @@ -131,12 +131,17 @@ const body = output.trim() || "(empty response)"; const parts = chunkForDiscord(header + body); - // Try the channel first. + // Try the channel first (force API fetch to avoid stale cache). let channel: any; try { - channel = await this.opts.client.channels.fetch(sched.destination); - } catch { - // channel fetch failed — will try DM fallback below + channel = await this.opts.client.channels.fetch(sched.destination, { + force: true, + }); + } catch (err: any) { + console.error( + `scheduler: channel fetch failed for ${sched.name} (destination ${sched.destination}):`, + err?.message ?? err + ); } if (channel && "send" in channel && typeof channel.send === "function") { @@ -146,10 +151,13 @@ return; } - // If we have a user ID (DM schedules), fall back to sending a DM. + // If we have a user ID (DM schedules, or channel fallback), send a DM. + // Use client.users.send() which handles DM channel creation internally. if (sched.user) { try { - const user = await this.opts.client.users.fetch(sched.user); + const user = await this.opts.client.users.fetch(sched.user, { + force: true, + }); for (const part of parts) { await user.send(part); } diff --git a/src/util.ts b/src/util.ts --- a/src/util.ts +++ b/src/util.ts @@ -71,13 +71,16 @@ } function formatScheduleFile(meta: ScheduleFrontmatter, prompt: string): string { - const userLine = meta.user ? `user: ${meta.user}\n` : ""; + // Quote snowflake IDs so the YAML parser keeps them as strings. + // Discord IDs are 64-bit and can exceed Number.MAX_SAFE_INTEGER. + const userLine = meta.user ? `user: "${meta.user}"\n` : ""; + const lr = meta.last_run ? `"${meta.last_run}"` : "null"; return `--- name: ${meta.name} cron: "${meta.cron}" -destination: ${meta.destination} +destination: "${meta.destination}" ${userLine}created: ${meta.created} -last_run: ${meta.last_run ?? "null"} +last_run: ${lr} --- ${prompt} `; diff --git a/src/tools/schedule.ts b/src/tools/schedule.ts --- a/src/tools/schedule.ts +++ b/src/tools/schedule.ts @@ -78,7 +78,7 @@ if (client) { let channel: any; try { - channel = await client.channels.fetch(dest); + channel = await client.channels.fetch(dest, { force: true }); } catch { if (!defaultUserId) { throw new Error( -- tangled.sh