diff --git a/index.ts b/index.ts index 6379fcc..9ccfd2b 100644 --- a/index.ts +++ b/index.ts @@ -214,7 +214,7 @@ function validateMessage(message: Discord.Message): string | null { async function fetchMessages(message: Discord.Message): Promise { let historyCache: MessageRecord[] = []; let keepGoing = true; - let oldestMessageID; + let oldestMessageID: string | undefined; while (keepGoing) { const messages: Discord.Collection< @@ -238,9 +238,10 @@ async function fetchMessages(message: Discord.Message): Promise { return dbObj; }); historyCache = historyCache.concat(nonBotMessageFormatted); - oldestMessageID = messages.last().id; - if (messages.size < PAGE_SIZE) { + if (!messages.last() || messages.size < PAGE_SIZE) { keepGoing = false; + } else { + oldestMessageID = messages.last().id; } } console.log(`Trained from ${historyCache.length} past human authored messages.`);