diff --git a/src/RepeatedMessage.js b/src/RepeatedMessage.js index 63bd5d715..22481e97a 100644 --- a/src/RepeatedMessage.js +++ b/src/RepeatedMessage.js @@ -1,7 +1,6 @@ const Discord = require('discord.js'); -const stringSimilarity = require("string-similarity"); +const stringSimilarity = require('string-similarity'); const Log = require('./Log'); -const util = require('./util'); class RepeatedMessage { @@ -25,12 +24,17 @@ class RepeatedMessage { */ #messages = []; + /** + * has this user been warned not to spam before + */ + warned = false; + /** * @param {module:"discord.js".Message} message */ constructor(message) { this.#key = this.constructor.getKey(message); - this.#messages.push(message); + this.add(message); } /** @@ -93,13 +97,7 @@ class RepeatedMessage { * @return {Promise} */ async deleteAll() { - const reason = `Fast message spam`; - for (const message of this.#messages) { - if (message.deletable) { - await util.delete(message, {reason}); - await Log.logMessageDeletion(message, reason) - } - } + return this.delete(this.#messages, 'Fast message spam'); } /** @@ -108,13 +106,25 @@ class RepeatedMessage { * @return {Promise} */ async deleteSimilar(message) { - const reason = `Repeated messages`; - for (const cacheMessage of this.getSimilarMessages(message)) { - if (cacheMessage.deletable) { - await util.delete(cacheMessage, {reason}); - await Log.logMessageDeletion(cacheMessage, reason) - } - } + return this.delete(this.getSimilarMessages(message), 'Repeated messages'); + } + + /** + * delete an array of messages if possible + * @param {module:"discord.js".Message[]} messages + * @param {String} reason + * @returns {Promise} + */ + async delete(messages, reason) { + messages = messages.filter(m => m.deletable); + + if (messages.length === 0) return; + + /** @type {module:"discord.js".TextBasedChannelFields} */ + const channel = messages[0].channel; + await channel.bulkDelete(messages); + + await Promise.all(messages.map(m => Log.logMessageDeletion(m , reason))); } /** @@ -160,9 +170,12 @@ class RepeatedMessage { if (cache.getMessageCount() > count) { await cache.deleteAll(); - /** @type {module:"discord.js".Message} */ - const reply = await message.channel.send(`<@!${message.author.id}> Stop sending messages this fast!`); - await reply.delete({timeout: 3000}); + if (!cache.warned) { + cache.warned = true; + /** @type {module:"discord.js".Message} */ + const reply = await message.channel.send(`<@!${message.author.id}> Stop sending messages this fast!`); + await reply.delete({timeout: 3000}); + } } } @@ -176,9 +189,12 @@ class RepeatedMessage { const similar = cache.getSimilarMessageCount(message); if (similar > count) { await cache.deleteSimilar(message); - /** @type {module:"discord.js".Message} */ - const reply = await message.channel.send(`<@!${message.author.id}> Stop repeating your messages!`); - await reply.delete({timeout: 3000}); + if (!cache.warned) { + cache.warned = true; + /** @type {module:"discord.js".Message} */ + const reply = await message.channel.send(`<@!${message.author.id}> Stop repeating your messages!`); + await reply.delete({timeout: 3000}); + } } } }