273 lines
9.8 KiB
JavaScript
273 lines
9.8 KiB
JavaScript
const { stripIndents } = require('common-tags');
|
|
const { ObjectId } = require('mongodb');
|
|
|
|
const { Collection, Util } = require('../../util/');
|
|
const { Unmute, Unban } = require('./infractions/');
|
|
|
|
const Constants = {
|
|
MaxTargets: 10, //10+(10*premium-tier), theoretical max = 40
|
|
Infractions: {
|
|
UNMUTE: Unmute,
|
|
UNBAN: Unban
|
|
},
|
|
Opposites: {
|
|
MUTE: "UNMUTE",
|
|
BAN: "UNBAN"
|
|
}
|
|
};
|
|
|
|
class ModerationManager {
|
|
|
|
constructor(client) {
|
|
|
|
this.client = client;
|
|
this.callbacks = new Collection();
|
|
|
|
}
|
|
|
|
async initialize() {
|
|
|
|
this.client.transactionHandler.send({
|
|
provider: 'mongodb',
|
|
request: {
|
|
collection: 'infractions',
|
|
type: 'find',
|
|
query: {
|
|
duration: { $gt: 0 },
|
|
guild: { $in: this.client.guilds.cache.keyArray() },
|
|
callbacked: false
|
|
}
|
|
}
|
|
}).then((results) => {
|
|
console.log(results);
|
|
this.client.logger.debug(`Filtering ${results.length} infractions for callback.`);
|
|
this._handleExpirations(results);
|
|
});
|
|
|
|
}
|
|
|
|
async handleInfraction(Infraction, message, { targets, reason, duration, data }) {
|
|
|
|
const maxTargets = Constants.MaxTargets + message.guild.premium*Constants.MaxTargets;
|
|
if(targets.length > maxTargets) {
|
|
return message.respond(stripIndents`${message.format('MODERATIONMANAGER_INFRACTION_MAXTARGETS', { maxTargets, type: Infraction.targetType })}
|
|
${maxTargets < 40 ? message.format('MODERATIONMANAGER_INFRACTION_MAXTARGETSALT') : ''}`, { emoji: 'failure' });
|
|
}
|
|
|
|
const silent = Boolean(message.guild._settings.silent || message.arguments.silent);
|
|
this.client.logger.debug(`Silent infraction: ${silent}`);
|
|
|
|
const promises = [];
|
|
for(const target of targets) {
|
|
promises.push(new Infraction(this.client, {
|
|
executor: message.member,
|
|
guild: message.guild,
|
|
channel: message.channel,
|
|
arguments: message.arguments,
|
|
message,
|
|
target,
|
|
reason,
|
|
duration,
|
|
silent,
|
|
data
|
|
}).execute());
|
|
}
|
|
|
|
const responses = await Promise.all(promises);
|
|
|
|
let success = Boolean(responses.some((r) => !r.error));
|
|
const succeeded = responses.filter((r) => !r.error);
|
|
const failed = responses.filter((r) => r.error);
|
|
|
|
const succeededTargets = succeeded.map((s) => s.infraction.target);
|
|
const actions = await this._handleArguments(message, succeededTargets); //Handle prune arguments, etc. ONLY IF INFRACTION SUCCEEDS.
|
|
|
|
//NOTE: I'm not translating: infraction types (KICK, MUTE, ETC.), infraction target types (channel(s), user(s)),
|
|
/* Message Handling */
|
|
const { dictionary, targetType } = responses[0].infraction;
|
|
|
|
//Handle fatal errors, if necessary.
|
|
const fatals = failed.filter((f) => f.fatal);
|
|
if(fatals.length > 0) {
|
|
const [ error ] = fatals;
|
|
return message.respond(message.format(error.reason), { emoji: 'failure' });
|
|
}
|
|
|
|
let string = "";
|
|
if(success && !silent) {
|
|
string = message.format('MODERATIONMANAGER_INFRACTION_SUCCESS', {
|
|
infraction: dictionary.past,
|
|
targetType: `${targetType}${succeeded.length > 1 ? 's' : ''}`,
|
|
target: succeeded.map((s) => `**${Util.escapeMarkdown(s.infraction.targetName)}**`).join(', '),
|
|
action: actions.prune ? ` and pruned \`${actions.prune}\` message${actions.prune > 1 ? 's' : ''}` : ''
|
|
});
|
|
} else if(failed.length > 0) {
|
|
if(silent) success = false;
|
|
const format = failed.length === 1 ? "MODERATIONMANAGER_INFRACTION_SINGULARFAIL" : "MODERATIONMANAGER_INFRACTION_MULTIPLEFAIL";
|
|
string = message.format(format, {
|
|
infraction: dictionary.present,
|
|
targetType: `${targetType}${failed.length > 1 ? 's' : ''}`,
|
|
target: failed.length === 1 ? `**${Util.escapeMarkdown(failed[0].infraction.targetName)}**` : failed.map((f) => `**${f.infraction.targetName}**`).join(', '),
|
|
reason: message.format(failed[0].reason)
|
|
});
|
|
}
|
|
|
|
if(success && failed.length > 0
|
|
|| !success && failed.length > 1) {
|
|
for(const fail of failed) {
|
|
string += `\n${message.format('MODERATIONMANAGER_INFRACTION_FAIL', {
|
|
infraction: dictionary.present,
|
|
target: Util.escapeMarkdown(fail.infraction.targetName),
|
|
reason: message.format(fail.reason)
|
|
})}`;
|
|
}
|
|
}
|
|
|
|
if(success && silent) { //Delete message if silent.
|
|
try {
|
|
await message.delete();
|
|
} catch(e) {} //eslint-disable-line no-empty
|
|
}
|
|
|
|
if(string) message.respond(string, { emoji: success ? 'success' : 'failure' });
|
|
return succeeded;
|
|
|
|
}
|
|
|
|
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) => users.includes(m.author.id) && m.deleteable);
|
|
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)) {
|
|
if(actions[arg.name]) {
|
|
let action = actions[arg.name](message, arg, targets);
|
|
if(action instanceof Promise) action = await action;
|
|
responses[arg.name] = action;
|
|
}
|
|
}
|
|
|
|
return responses;
|
|
|
|
}
|
|
|
|
async _handleExpirations(infractions = []) {
|
|
|
|
const currentDate = Date.now();
|
|
|
|
const resolve = async (i) => {
|
|
this.client.logger.debug("Resolving infraction");
|
|
const undoClass = Constants.Infractions[Constants.Opposites[i.type]];
|
|
if(!undoClass) return false;
|
|
|
|
const guild = this.client.guilds.resolve(i.guild);
|
|
await guild.settings(); //just incase
|
|
|
|
let target = null;
|
|
if(i.targetType === 'user') {
|
|
target = await guild.members.resolve(i.target);
|
|
if(!target) {
|
|
try {
|
|
target = await guild.members.fetch(i.target);
|
|
} catch(e) {} //eslint-disable-line no-empty
|
|
if(!target && i.type === 'BAN') {
|
|
try {
|
|
target = await this.client.users.fetch(i.target);
|
|
} catch(e) {} //eslint-disable-line no-empty
|
|
}
|
|
}
|
|
} else if(i.targetType === 'channel') {
|
|
target = guild.channels.resolve(i.target);
|
|
}
|
|
|
|
if(!target) {
|
|
this.client.logger.debug(`User left the guild or channel was deleted? Unable to resolve target.\n${i}`);
|
|
return false;
|
|
}
|
|
|
|
const executor = guild.members.resolve(i.executor) || guild.me;
|
|
|
|
await new undoClass(this.client, {
|
|
reason: `AUTO-${Constants.Opposites[i.type]} from Case ${i.case}`,
|
|
channel: guild.channels.resolve(i.channel),
|
|
hyperlink: i.logMessage && i.moderationLog ? `https://discord.com/channels/${i.guild}/${i.moderationLog}/${i.logMessage}` : null,
|
|
data: i.data,
|
|
guild,
|
|
target,
|
|
executor
|
|
}).execute();
|
|
|
|
const obj = await this.client.transactionHandler.send({
|
|
provider: 'mongodb',
|
|
request: {
|
|
type: 'updateOne',
|
|
collection: 'infractions',
|
|
query: {
|
|
_id: i._id
|
|
},
|
|
upsert: false,
|
|
data: {
|
|
callbacked: true
|
|
}
|
|
}
|
|
}).catch((e) => {}); //eslint-disable-line no-empty, no-unused-vars, no-empty-function
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
for(const infraction of infractions) {
|
|
const expiration = infraction.timestamp + infraction.duration*1000;
|
|
if(expiration-currentDate < 0) {
|
|
await resolve(infraction);
|
|
continue;
|
|
}
|
|
|
|
this.client.logger.debug(`Going to resolve infraction in: ${expiration-currentDate}`);
|
|
|
|
this.callbacks.set(infraction.id, {
|
|
timeout: setTimeout(() => {
|
|
resolve(infraction);
|
|
}, expiration-currentDate),
|
|
infraction
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async _removeExpiration(expiration) {
|
|
console.log(`Removing expiration: ${expiration.infraction.id}`);
|
|
await this.client.transactionHandler.send({
|
|
provider: 'mongodb',
|
|
request: {
|
|
type: 'updateOne',
|
|
collection: 'infractions',
|
|
query: {
|
|
id: expiration.infraction.id
|
|
},
|
|
data: {
|
|
callbacked: true
|
|
}
|
|
}
|
|
});
|
|
clearInterval(expiration.timeout); //just incase node.js is doing some bullshit
|
|
this.callbacks.delete(expiration.infraction.id);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
module.exports = ModerationManager; |