From a77a752074f8f32f7f615c8151795717ef0f2d1e Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 23 Jun 2021 22:20:58 +0200 Subject: [PATCH 1/3] fix styling --- src/RepeatedMessage.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/RepeatedMessage.js b/src/RepeatedMessage.js index 63bd5d715..ad1c743a6 100644 --- a/src/RepeatedMessage.js +++ b/src/RepeatedMessage.js @@ -1,5 +1,5 @@ const Discord = require('discord.js'); -const stringSimilarity = require("string-similarity"); +const stringSimilarity = require('string-similarity'); const Log = require('./Log'); const util = require('./util'); @@ -93,11 +93,11 @@ class RepeatedMessage { * @return {Promise} */ async deleteAll() { - const reason = `Fast message spam`; + const reason = 'Fast message spam'; for (const message of this.#messages) { if (message.deletable) { await util.delete(message, {reason}); - await Log.logMessageDeletion(message, reason) + await Log.logMessageDeletion(message, reason); } } } @@ -108,11 +108,11 @@ class RepeatedMessage { * @return {Promise} */ async deleteSimilar(message) { - const reason = `Repeated messages`; + const reason = 'Repeated messages'; for (const cacheMessage of this.getSimilarMessages(message)) { if (cacheMessage.deletable) { await util.delete(cacheMessage, {reason}); - await Log.logMessageDeletion(cacheMessage, reason) + await Log.logMessageDeletion(cacheMessage, reason); } } } From de124d1e348fff3864efb7828ca5f9a6054fcec4 Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 23 Jun 2021 22:45:21 +0200 Subject: [PATCH 2/3] only warn users once --- src/RepeatedMessage.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/RepeatedMessage.js b/src/RepeatedMessage.js index ad1c743a6..5c9aad4c6 100644 --- a/src/RepeatedMessage.js +++ b/src/RepeatedMessage.js @@ -25,12 +25,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); } /** @@ -160,9 +165,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 +184,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}); + } } } } From 277144ae60b3bc957b5fc65fe899f395ec123725 Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 23 Jun 2021 23:04:44 +0200 Subject: [PATCH 3/3] use bulk delete, async logging --- src/RepeatedMessage.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/RepeatedMessage.js b/src/RepeatedMessage.js index 5c9aad4c6..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 Log = require('./Log'); -const util = require('./util'); class RepeatedMessage { @@ -98,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'); } /** @@ -113,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))); } /**