galactic-bot/structure/client/components/settings/moderation/AutoModeration.js

152 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-07-20 00:42:21 +02:00
const { Setting } = require('../../../../interfaces/');
2020-09-01 17:46:01 +02:00
const CONSTANTS = {
INFRACTIONS: ['WARN', 'MUTE', 'KICK', 'SOFTBAN', 'BAN', 'VCMUTE', 'VCKICK', 'VCBAN'],
TIMED: ['MUTE', 'BAN', 'VCMUTE', 'VCBAN']
2020-09-01 17:46:01 +02:00
};
2020-07-20 00:42:21 +02:00
class AutoModerationSetting extends Setting {
constructor(client) {
super(client, {
name: 'autoModeration',
module: 'moderation',
2020-08-31 17:04:54 +02:00
aliases: ['automod'],
2020-07-20 00:42:21 +02:00
resolve: 'GUILD',
default: {
autoModeration: {
enabled: true,
thresholds: {
2020-09-01 17:46:01 +02:00
},
usePrevious: false // use previous threshold if no new threshold is exceeded
2020-07-20 00:42:21 +02:00
}
},
2020-09-01 17:46:01 +02:00
archivable: true
2020-07-20 00:42:21 +02:00
});
}
2020-09-01 17:46:01 +02:00
async handle(message, args) {
const setting = message.guild._settings[this.index];
const [method, ...params] = args;
let index = null,
langParams = {};
const { resolver } = this.client;
if (resolver.resolveBoolean(method)) {
setting.enabled = true;
index = 'S_AUTOMOD_TOGGLE';
langParams = { toggle: message.format('ON_OFF_TOGGLE', { toggle: true }, true) };
} else if (resolver.resolveBoolean(method) === false) {
setting.enabled = false;
index = 'S_AUTOMOD_TOGGLE';
langParams = { toggle: message.format('ON_OFF_TOGGLE', { toggle: false }, true) };
} else if (method.toLowerCase() === 'threshold') {
const points = parseInt(params.shift());
if (isNaN(points) || points < 0 || points > 100) return { error: true, msg: message.format('S_AUTOMOD_INVALID_POINTS', {}) };
const action = params.shift()?.toLowerCase();
if (!action) return { error: true, msg: message.format('S_AUTOMOD_NOARG') };
2020-09-01 17:46:01 +02:00
if (['delete', 'remove'].includes(action)) {
const old = setting.thresholds[points];
if (old) {
index = 'S_AUTOMOD_REMOVE_POINTS';
langParams = { type: old.type, points };
delete setting.thresholds[points];
} else {
return { error: true, msg: message.format('S_AUTOMOD_REMOVE_NOPOINTS', { points }) };
}
} else {
2020-09-01 17:46:01 +02:00
const infraction = resolver.resolveInfraction(action);
if (!CONSTANTS.INFRACTIONS.includes(infraction)) return { error: true, msg: message.format('S_AUTOMOD_INVALID_TYPE', { infraction }) };
const old = setting.thresholds[points];
const timed = CONSTANTS.TIMED.includes(infraction);
const _time = params.join(' ');
const time = resolver.resolveTime(_time);
2020-09-01 17:46:01 +02:00
if (old) {
index = timed && time ? 'S_AUTOMOD_OVERWRITE_POINTS_TIMED' : 'S_AUTOMOD_OVERWRITE_POINTS';
langParams = { type: infraction, points, oldType: old.type, time: resolver.timeAgo(time, true, true, true) };
2020-09-01 17:46:01 +02:00
} else {
index = timed && time ? 'S_AUTOMOD_ADD_POINTS_TIMED' : 'S_AUTOMOD_ADD_POINTS';
langParams = { type: infraction, points, time: resolver.timeAgo(time, true, true, true) };
2020-09-01 17:46:01 +02:00
}
setting.thresholds[points] = { type: infraction };
if (timed && time) setting.thresholds[points].length = time;
2020-09-01 17:46:01 +02:00
}
} else if (method.toLowerCase() === 'length') {
2020-07-20 00:42:21 +02:00
2020-09-01 17:46:01 +02:00
const points = parseInt(params.shift());
if (isNaN(points) || points < 0 || points > 100) return { error: true, msg: message.format('S_AUTOMOD_INVALID_POINTS', {}) };
const action = params.shift()?.toLowerCase();
if (!action) return { error: true, msg: message.format('S_AUTOMOD_NOARG') };
if (!setting.thresholds[points]) return { error: true, msg: message.format('S_AUTOMOD_NOACTION') };
if (['delete', 'remove'].includes(action)) {
delete setting.thresholds[points].length;
index = 'S_AUTOMOD_REMOVE_LENGTH';
langParams = { type: setting.thresholds[points].type, points };
} else {
const [, , ..._time] = args;
const time = resolver.resolveTime(_time.join(' '));
if (!time) return { error: true, msg: message.format('ERR_INVALID_TIMESTRING') };
setting.thresholds[points].length = time;
index = 'S_AUTOMOD_LENGTH_SUCCESS';
}
} else if (['useprev', 'useprevious', 'previous'].includes(method.toLowerCase())) {
const bool = resolver.resolveBoolean(params[0]);
if (bool) setting.usePrevious = bool;
else if (bool === false) setting.usePrevious = bool;
else return { error: true, msg: message.format('ERR_BOOLEAN_RESOLVE', { resolveable: params[0] }) };
index = 'S_AUTOMOD_USEPREV_TOGGLE';
langParams = { toggle: message.format('ON_OFF_TOGGLE', { toggle: bool }, true) };
2020-09-01 17:46:01 +02:00
}
await message.guild._updateSettings({ [this.index]: setting });
return { msg: message.format(index, langParams), error: false };
2020-07-20 00:42:21 +02:00
}
fields(guild) {
2020-09-01 17:46:01 +02:00
const setting = guild._settings[this.index];
const { resolver } = this.client;
return [
{
name: "》 Status",
value: guild.format('SETTING_STATUS', { bool: Boolean(setting.enabled) }, true),
inline: true
},
{
name: '》 Use previous threshold',
value: guild.format('SETTING_STATUS', { bool: Boolean(setting.usePrevious) }, true),
inline: true
},
{
name: '》 Thresholds',
value: Object.entries(setting.thresholds).reduce((acc, [points, action]) => {
acc.push(`**${points}:** \`${action.type}\`${action.length ? ', ' + resolver.timeAgo(action.length) : ''}`);
return acc;
}, []).join('\n')
}
];
2020-07-20 00:42:21 +02:00
}
}
module.exports = AutoModerationSetting;