From cc5ed60b111cb38526ce7632d6627b383157553a Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sun, 27 Mar 2022 16:25:04 +0300 Subject: [PATCH] automod stuff --- src/structure/client/ModerationManager.js | 49 ++++++++++++------- .../components/observers/Automoderation.js | 12 +++-- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/structure/client/ModerationManager.js b/src/structure/client/ModerationManager.js index 82c5bc3..4901df2 100644 --- a/src/structure/client/ModerationManager.js +++ b/src/structure/client/ModerationManager.js @@ -40,6 +40,22 @@ class ModerationManager { } + actions = { + prune: async (message, argument, targets) => { + const users = targets.map((t) => t.id); + let messages = await message.channel.messages.fetch({ + limit: argument.value + }); + messages = messages.filter((m) => { + return users.includes(m.author.id) && m.deletable; + }); + try { + await message.channel.bulkDelete(messages, true); + } catch (err) { } //eslint-disable-line no-empty + return messages.size; + } + }; + async initialize() { //TODO: Load infractions for non-cached guilds... @@ -189,7 +205,18 @@ class ModerationManager { } - async handleAutomod(Infraction, info) { + async handleAutomod(Infraction, target, info) { + + const response = await this._handleTarget(Infraction, target, info); + const success = !response.error; + + if (!success) { + this.client.emit('automodError', response); + return response; + } + + if (info.prune) await this.actions.prune({ channel: info.channel }, { value: 100 }, [target]); + return response; } @@ -310,27 +337,11 @@ class ModerationManager { async _handleArguments(message, targets) { - const actions = { - prune: async (message, argument, targets) => { - const users = targets.map((t) => t.id); - let messages = await message.channel.messages.fetch({ - limit: argument.value - }); - messages = messages.filter((m) => { - return users.includes(m.author.id) && m.deletable; - }); - try { - await message.channel.bulkDelete(messages, true); - } catch (err) { } //eslint-disable-line no-empty - return messages.size; - } - }; - const responses = {}; for (const arg of Object.values(message.arguments)) { // console.log(arg, targets); - if (actions[arg.name]) { - const action = await actions[arg.name](message, arg, targets); + if (this.actions[arg.name]) { + const action = await this.actions[arg.name](message, arg, targets); responses[arg.name] = action; } } diff --git a/src/structure/components/observers/Automoderation.js b/src/structure/components/observers/Automoderation.js index 1103a89..93d4f88 100644 --- a/src/structure/components/observers/Automoderation.js +++ b/src/structure/components/observers/Automoderation.js @@ -6,7 +6,6 @@ const { Observer } = require('../../interfaces'); const { BinaryTree, Util, FilterUtil } = require('../../../utilities'); const { FilterPresets } = require('../../../constants'); const { Warn, Mute, Kick, Softban, Ban } = require('../infractions'); -const { GuildWrapper } = require('../../client/wrappers'); const CONSTANTS = { Infractions: { @@ -58,7 +57,7 @@ module.exports = class AutoModeration extends Observer { this.executing[filterResult.filter].push(member.id); const InfractionClass = CONSTANTS.Infractions[action.type]; - const result = await this.client.moderationManager._handleTarget(InfractionClass, member, { + const result = await this.client.moderationManager.handleAutomod(InfractionClass, member, { wrapper, channel, executor: wrapper.guild.me, @@ -66,15 +65,18 @@ module.exports = class AutoModeration extends Observer { duration: action.duration, points: action.points, expiration: action.expiration, - silent: true, //Won't DM Users. + silent: false, //Won't DM Users. force: false, + prune: action.prune, data: { automoderation: filterResult } }).catch(this.logger.error.bind(this.logger)); + filterResult.sanctioned = !result.error; await Util.wait(5000); this.executing[filterResult.filter].splice(this.executing[filterResult.filter].indexOf(member.id), 1); + return !result.error; } @@ -261,12 +263,11 @@ module.exports = class AutoModeration extends Observer { return this.client.rateLimiter.queueDelete(msg.channel, msg).catch(catcher(275)); } - msg.filtered.sanctioned = true; this.client.rateLimiter.queueDelete(msg.channel, msg).catch(catcher(279)); this.logger.debug(log + '\nSanctioned'); filterResult.filter = 'word'; - this._moderate(action, wrapper, channel, member, wrapper.format('W_FILTER_ACTION'), filterResult, message); + await this._moderate(action, wrapper, channel, member, wrapper.format('W_FILTER_ACTION'), filterResult, message); } else { this.client.rateLimiter.queueDelete(msg.channel, msg).catch(catcher(269)); @@ -331,6 +332,7 @@ module.exports = class AutoModeration extends Observer { }, []) }; + // TODO: Add action buttons const sent = await logChannel.send({ embeds: [embed] }).catch((err) => { this.logger.error('Error in message flag:\n' + err.stack); });