Add swept messages counter and debug message

This commit is contained in:
Schuyler Cebulskie
2016-09-20 01:14:15 -04:00
parent 5a9753e461
commit 8f1bc5edab
2 changed files with 11 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@@ -250,17 +250,25 @@ class Client extends EventEmitter {
/**
* Sweeps all channels' messages and removes the ones older than the max message lifetime.
* If the message has been edited, the time of the edit is used rather than the time of the original message.
* @returns {number} Amount of messages that were removed from the caches,
* or -1 if the message cache lifetime is unlimited
*/
sweepMessages() {
if (this.options.message_cache_lifetime <= 0) return;
if (this.options.message_cache_lifetime <= 0) return -1;
const now = Date.now();
const lifetime = this.options.message_cache_lifetime * 1000;
let swept = 0;
for (const channel of this.channels.values()) {
if (!channel.messages) continue;
for (const message of channel.messages.values()) {
if (now - (message._editedTimestamp || message._timestamp) > lifetime) channel.messages.delete(message.id);
if (now - (message._editedTimestamp || message._timestamp) > lifetime) {
channel.messages.delete(message.id);
swept++;
}
}
}
this.emit('debug', `Swept ${swept} messages`);
return swept;
}
setTimeout(fn, ...params) {