This commit is contained in:
Erik 2021-06-17 01:52:41 +03:00
commit a846a06eea
4 changed files with 56 additions and 38 deletions

View File

@ -2,6 +2,7 @@ const { inspect } = require('util');
const similarity = require('similarity'); const similarity = require('similarity');
const { stripIndents } = require('common-tags'); const { stripIndents } = require('common-tags');
const { GuildMember } = require('../../../extensions/');
const { Observer, BinaryTree } = require('../../../interfaces'); const { Observer, BinaryTree } = require('../../../interfaces');
const { FilterUtil, FilterPresets, Util } = require('../../../../util'); const { FilterUtil, FilterPresets, Util } = require('../../../../util');
const { Warn, Mute, Kick, Softban, Ban } = require('../../../moderation/infractions'); const { Warn, Mute, Kick, Softban, Ban } = require('../../../moderation/infractions');
@ -47,8 +48,31 @@ module.exports = class AutoModeration extends Observer {
} }
_moderate(action, guild, channel, member, moderationPoints, silent, reason, filterResult, message) { async _moderate(action, guild, channel, member, removeme2, silent, reason, filterResult, removeme) {
this.client.moderationManager.autoModerate({ guild, action, message, reason, silent, data: { filterResult } }); const InfractionClass = CONSTANTS.Infractions[action.type];
await this.client.moderationManager._handleTarget(InfractionClass, member, {
guild,
channel,
executor: guild.me,
reason,
duration: action.duration,
points: action.points,
expiration: action.expiration,
silent: true,
force: false,
data: {
automoderation: {
type: 'filler',
matched: {
type: 'wordfiltertype/explicit/regex/whatever',
word: 'whatwasmatched'
}
}
}
});
// this.client.moderationManager.autoModerate({ guild, action, message, reason, silent, data: { filterResult } });
/* this.client.moderationManager.handleInfraction( /* this.client.moderationManager.handleInfraction(
CONSTANTS.Infractions[action.type], CONSTANTS.Infractions[action.type],
@ -96,12 +120,6 @@ module.exports = class AutoModeration extends Observer {
if (!enabled || roles.some((r) => bypass.includes(r)) || ignore.includes(channel.id)) return; if (!enabled || roles.some((r) => bypass.includes(r)) || ignore.includes(channel.id)) return;
if (command?.name === 'settings') {
// NOTE: probably needs a more permanent solution
const result = await this.client.registry.components.get('inhibitor:permissions').execute(message, message.command);
if (!result.error) return;
}
// Which message obj to work with // Which message obj to work with
const msg = edited || message; const msg = edited || message;
if (!msg.content) return; if (!msg.content) return;

View File

@ -343,7 +343,7 @@ module.exports = class FilterSetting extends Setting {
action.type = response.content.toUpperCase(); action.type = response.content.toUpperCase();
if (['BAN', 'MUTE'].includes(action.type) && !action.duration) return this._editDuration(message, setting, action); if (['BAN', 'MUTE'].includes(action.type) && !action.duration) return this._editDuration(message, setting, action);
action.duration = null; action.duration = 0;
return action; return action;
} }
@ -365,7 +365,7 @@ module.exports = class FilterSetting extends Setting {
}; };
if (response.content === '0') { if (response.content === '0') {
action.duration = null; action.duration = 0;
} else { } else {
const time = resolver.resolveTime(response.content); const time = resolver.resolveTime(response.content);
if (!time) return { if (!time) return {

View File

@ -1,10 +1,9 @@
const { stripIndents } = require('common-tags'); const { stripIndents } = require('common-tags');
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const { Message } = require('discord.js'); const { Message } = require('discord.js');
const { Console } = require('winston/lib/winston/transports');
const { Collection, Util, Emojis, Constants } = require('../../util/'); const { Collection, Util, Emojis } = require('../../util/');
const { User, GuildMember } = require('../extensions/'); const { User } = require('../extensions/');
const { Warn, Unmute, Mute, Kick, Softban, Unban, Ban } = require('./infractions/'); const { Warn, Unmute, Mute, Kick, Softban, Unban, Ban } = require('./infractions/');
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const Infraction = require('./interfaces/Infraction'); const Infraction = require('./interfaces/Infraction');
@ -86,11 +85,13 @@ class ModerationManager {
guild: message.guild, guild: message.guild,
channel: message.channel, channel: message.channel,
executor: message.member, executor: message.member,
arguments: message.arguments, points: message.arguments?.points?.value,
expiration: message.arguments?.expiration?.value,
reason: info.reason, reason: info.reason,
duration: info.duration, duration: info.duration,
data: info.data, data: info.data,
force force,
silent
}); });
responses.push({ responses.push({
escalation: Infraction.type !== response.infraction.type, escalation: Infraction.type !== response.infraction.type,
@ -141,17 +142,18 @@ class ModerationManager {
const actions = await this._handleArguments(message, succeededTargets); //eslint-disable-line no-unused-vars const actions = await this._handleArguments(message, succeededTargets); //eslint-disable-line no-unused-vars
let string = ""; let string = "";
for(const [ , data ] of Object.entries(successes)) { if(!silent) {
if(silent) continue; for(const [ , data ] of Object.entries(successes)) {
const { dictionary, targetType } = data.infraction; const { dictionary, targetType } = data.infraction;
const reason = data.escalation ? message.format('INFRACTION_ESCALATIONREASON') : Util.escapeMarkdown(data.infraction.reason); const reason = data.escalation ? message.format('INFRACTION_ESCALATIONREASON') : Util.escapeMarkdown(data.infraction.reason);
const str = `${Emojis.success} ${message.format('INFRACTION_SUCCESS', { const str = `${Emojis.success} ${message.format('INFRACTION_SUCCESS', {
infraction: dictionary.past, infraction: dictionary.past,
targetType: `${targetType.toLowerCase()}${data.targets.length === 1 ? '' : 's'}`, targetType: `${targetType.toLowerCase()}${data.targets.length === 1 ? '' : 's'}`,
target: data.targets.map((t) => `**${Util.escapeMarkdown(t)}**`).join(' '), target: data.targets.map((t) => `**${Util.escapeMarkdown(t)}**`).join(' '),
text: !data.escalation ? ` ${reason.length > 120 ? `for: \`${reason.substring(0, 117)}...\`` : `for: \`${reason}\``}` : ` because \`${reason}\`` text: !data.escalation ? ` ${reason.length > 120 ? `for: \`${reason.substring(0, 117)}...\`` : `for: \`${reason}\``}` : ` because \`${reason}\``
})}`; })}`;
data.escalation ? string += str : string = `${str}\n${string}`; //eslint-disable-line data.escalation ? string += str : string = `${str}\n${string}`; //eslint-disable-line
}
} }
for(const [ , data ] of Object.entries(fails)) { for(const [ , data ] of Object.entries(fails)) {
@ -180,14 +182,14 @@ class ModerationManager {
async _handleTarget(Infraction, target, info, automod = false) { async _handleTarget(Infraction, target, info, automod = false) {
const { guild, reason, force } = info; const { guild, reason, force } = info;
const { autoModeration, moderationPoints, silent } = guild._settings; const { autoModeration, moderationPoints } = guild._settings;
const { type } = Infraction; const { type } = Infraction;
let points = 0, let points = 0,
expiration = 0; expiration = 0;
if(moderationPoints.enabled) { if(moderationPoints.enabled) {
points = info.arguments?.points?.value || moderationPoints.points[type]; points = info.points || moderationPoints.points[type];
expiration = info.arguments?.expiration?.value || moderationPoints.expirations[type]; expiration = info.expiration || moderationPoints.expirations[type];
for(const [ phrase, amount ] of Object.entries(moderationPoints.associations)) { for(const [ phrase, amount ] of Object.entries(moderationPoints.associations)) {
if(reason.toLowerCase().includes(phrase)) points = amount; if(reason.toLowerCase().includes(phrase)) points = amount;
} }
@ -205,7 +207,7 @@ class ModerationManager {
const oldPoints = await userTarget.totalPoints(guild); const oldPoints = await userTarget.totalPoints(guild);
const newPoints = oldPoints + infraction.points; const newPoints = oldPoints + infraction.points;
if(autoModeration.enabled && points > 0 && !force && !automod && !escalated) { if(autoModeration.enabled && points > 0 && !force && !escalated) {
let result = null; let result = null;
for(let [ threshold, action ] of Object.entries(autoModeration.thresholds)) { //eslint-disable-line prefer-const for(let [ threshold, action ] of Object.entries(autoModeration.thresholds)) { //eslint-disable-line prefer-const
threshold = parseInt(threshold); threshold = parseInt(threshold);
@ -250,7 +252,7 @@ class ModerationManager {
data: info.data, data: info.data,
points, points,
expiration, expiration,
silent: silent || info.arguments.silent silent: info.silent
}); });
let response = await verify(infraction); let response = await verify(infraction);
@ -270,7 +272,7 @@ class ModerationManager {
data: info.data, data: info.data,
points, points,
expiration, expiration,
silent: silent || info.arguments.silent silent: info.silent
}); });
response = await verify(escalationInfraction, true); response = await verify(escalationInfraction, true);
} }
@ -305,14 +307,12 @@ class ModerationManager {
executor: guild.me, executor: guild.me,
arguments: null, arguments: null,
reason, reason,
duration: duration, duration,
data, data,
force, force,
silent silent
}); });
console.log(response)
if (response.error) this.client.logger.debug(`Automod infraction execution failed:\n${JSON.stringify(response)}`); if (response.error) this.client.logger.debug(`Automod infraction execution failed:\n${JSON.stringify(response)}`);
// TODO: // TODO:
// Figure out what we want to do with these errors if anything apart from potentially log them // Figure out what we want to do with these errors if anything apart from potentially log them

View File

@ -33,7 +33,7 @@ class Infraction {
this.message = data.message || null; this.message = data.message || null;
this.targetId = data.target ? data.target.id : null; this.targetId = data.target ? data.target.id : null;
this.target = data.target || []; this.target = data.target || null;
this.executorId = data.executor ? data.executor.id : null; this.executorId = data.executor ? data.executor.id : null;
this.executor = data.executor || null; this.executor = data.executor || null;