mirror of
https://github.com/danbulant/discord.js
synced 2026-06-07 16:52:16 +00:00
feat(Collection): add sweep method (#2528)
This commit is contained in:
parent
9bb8831619
commit
2b6592ed54
2 changed files with 18 additions and 6 deletions
|
|
@ -379,12 +379,9 @@ class Client extends EventEmitter {
|
||||||
if (!channel.messages) continue;
|
if (!channel.messages) continue;
|
||||||
channels++;
|
channels++;
|
||||||
|
|
||||||
for (const message of channel.messages.values()) {
|
messages += channel.messages.sweep(
|
||||||
if (now - (message.editedTimestamp || message.createdTimestamp) > lifetimeMs) {
|
message => now - (message.editedTimestamp || message.createdTimestamp) > lifetimeMs
|
||||||
channel.messages.delete(message.id);
|
);
|
||||||
messages++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('debug', `Swept ${messages} messages older than ${lifetime} seconds in ${channels} text-based channels`);
|
this.emit('debug', `Swept ${messages} messages older than ${lifetime} seconds in ${channels} text-based channels`);
|
||||||
|
|
|
||||||
|
|
@ -255,6 +255,21 @@ class Collection extends Map {
|
||||||
return Boolean(this.find(prop, value));
|
return Boolean(this.find(prop, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes entries that satisfy the provided filter function.
|
||||||
|
* @param {Function} fn Function used to test (should return a boolean)
|
||||||
|
* @param {Object} [thisArg] Value to use as `this` when executing function
|
||||||
|
* @returns {number} The number of removed entries
|
||||||
|
*/
|
||||||
|
sweep(fn, thisArg) {
|
||||||
|
if (thisArg) fn = fn.bind(thisArg);
|
||||||
|
const previousSize = this.size;
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this)) this.delete(key);
|
||||||
|
}
|
||||||
|
return previousSize - this.size;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Identical to
|
* Identical to
|
||||||
* [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter),
|
* [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue