From 49aacec4a7593e160c444e817339f95a98b8ad59 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Thu, 5 Mar 2020 21:18:10 -0400 Subject: [PATCH] fix: correctly splice and merge fetched messages This was eliminating messages in the middle of the array instead of eliminating older messages to reach the specified limit. close #97 --- src/structures/Chat.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 8303765..f6ba2ab 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -131,14 +131,14 @@ class Chat extends Base { while(msgs.length < limit) { const loadedMessages = await chat.loadEarlierMsgs(); if(!loadedMessages) break; - msgs = [...msgs, ...loadedMessages.filter(msgFilter)]; + msgs = [...loadedMessages.filter(msgFilter), ...msgs]; } - return msgs.splice(0, limit).map(m => m.serialize()); + msgs.sort((a, b) => (a.t > b.t) ? 1 : -1); + return msgs.splice(msgs.length - limit).map(m => m.serialize()); }, this.id._serialized, searchOptions.limit); - messages.sort((a, b) => (a.t > b.t) ? 1 : -1); return messages.map(m => new Message(this.client, m)); }