Add message sweeping

This commit is contained in:
Schuyler Cebulskie
2016-09-19 16:52:46 -04:00
parent 46d7dedc85
commit afc8e5bee0
2 changed files with 26 additions and 0 deletions

View File

@@ -126,6 +126,10 @@ class Client extends EventEmitter {
this._timeouts = new Set();
this._intervals = new Set();
if (this.options.message_sweep_interval > 0) {
this.setInterval(this.sweepMessages.bind(this), this.options.message_sweep_interval * 1000);
}
}
/**
@@ -243,6 +247,22 @@ class Client extends EventEmitter {
return this.rest.methods.getInvite(code);
}
/**
* 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.
*/
sweepMessages() {
if (this.options.max_message_lifetime <= 0) return;
const now = Date.now();
const lifetime = this.options.max_message_lifetime * 1000;
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);
}
}
}
setTimeout(fn, ...params) {
const timeout = setTimeout(() => {
fn();