From eaedaad5ff1cc039942ce6433fb43423a33e3b92 Mon Sep 17 00:00:00 2001 From: Charlie Laabs Date: Sat, 10 Oct 2020 12:21:14 -0500 Subject: [PATCH] Fix error when final message page is exactly the page length. --- index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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.`);