From 91c9ac9ece25f055c5ca9f60c2ab1426806f23a8 Mon Sep 17 00:00:00 2001 From: aiirvizionz Date: Sat, 1 Aug 2026 16:25:13 -0600 Subject: [PATCH] fix(discord): tolerate out-of-range message timestamps --- packages/bots/discord/src/index.test.ts | 18 +++++++++++++++++- packages/bots/discord/src/index.ts | 5 +++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/bots/discord/src/index.test.ts b/packages/bots/discord/src/index.test.ts index 0d605652..550ab68e 100644 --- a/packages/bots/discord/src/index.test.ts +++ b/packages/bots/discord/src/index.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; import { contractTestBot } from '@profullstack/sh1pt-core/testing'; -import bot, { loadConfig } from './index.js'; +import bot, { loadConfig, toBotEvent } from './index.js'; contractTestBot(bot, { sampleConfig: {}, sampleChannel: '1234567890' }); @@ -18,3 +18,19 @@ describe('loadConfig', () => { expect(config.maxConcurrentSessions).toBe(5); }); }); + +describe('toBotEvent', () => { + it('falls back when Discord provides an out-of-range timestamp', () => { + expect(toBotEvent({ + source: 'user-1', + sourceName: 'User', + text: 'hello', + timestamp: Number.NEGATIVE_INFINITY, + channelId: 'channel-1', + guildId: null, + isGuild: false, + attachments: [], + raw: undefined as never, + }).timestamp).toBe('1970-01-01T00:00:00.000Z'); + }); +}); diff --git a/packages/bots/discord/src/index.ts b/packages/bots/discord/src/index.ts index df014a77..a133de46 100644 --- a/packages/bots/discord/src/index.ts +++ b/packages/bots/discord/src/index.ts @@ -178,7 +178,8 @@ function isTextChannel(channel: unknown): channel is SendableChannel { return !!channel && typeof channel === "object" && "send" in channel; } -function toBotEvent(msg: IncomingMessage): BotEvent { +export function toBotEvent(msg: IncomingMessage): BotEvent { + const date = new Date(msg.timestamp); return { type: "message", channel: msg.channelId, @@ -192,7 +193,7 @@ function toBotEvent(msg: IncomingMessage): BotEvent { filename: a.filename, mimeType: a.contentType, })), - timestamp: new Date(msg.timestamp).toISOString(), + timestamp: Number.isNaN(date.getTime()) ? new Date(0).toISOString() : date.toISOString(), raw: msg.raw, }; }