Fix error when final message page is exactly the page length.

This commit is contained in:
Charlie Laabs
2020-10-10 12:21:14 -05:00
parent 3e562f5ffc
commit eaedaad5ff

View File

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