Skip to content

Commit

Permalink
Merge pull request #295 from aternosorg/spam
Browse files Browse the repository at this point in the history
improve spam detection in raids
  • Loading branch information
JulianVennen authored Jul 2, 2021
2 parents bf35f3a + 277144a commit c6cdc33
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions src/RepeatedMessage.js
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -93,13 +97,7 @@ class RepeatedMessage {
* @return {Promise<void>}
*/
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');
}

/**
Expand All @@ -108,13 +106,25 @@ class RepeatedMessage {
* @return {Promise<void>}
*/
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<void>}
*/
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)));
}

/**
Expand Down Expand Up @@ -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});
}
}
}

Expand All @@ -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});
}
}
}
}
Expand Down

0 comments on commit c6cdc33

Please sign in to comment.