mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Add message sweeping
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user