galactic-bot/structure/client/components/settings/moderation/ModerationPoints.js
2020-09-01 18:45:43 +03:00

193 lines
7.4 KiB
JavaScript

const { Setting } = require('../../../../interfaces/');
const CONSTANTS = {
INFRACTIONS: ['WARN', 'MUTE', 'KICK', 'SOFTBAN', 'BAN', 'VCMUTE', 'VCKICK', 'VCBAN']
};
class ModerationPointsSetting extends Setting {
constructor(client) {
super(client, {
name: 'moderationPoints',
module: 'moderation',
aliases: [
'moderationPoint',
'modPoints',
'modPoint'
],
usage: '<action|method> [value]',
resolve: 'GUILD',
examples: [
'modpoints kick points 5',
'modpoints warn expire 2 weeks',
'modpoints on',
'modpoints multiplier on',
'modpoints associate nsfw 5'
],
default: {
moderationPoints: {
enabled: false,
points: {
WARN: 0,
MUTE: 0,
KICK: 0,
SOFTBAN: 0,
BAN: 0,
VCMUTE: 0,
VCKICK: 0,
VCBAN: 0
},
expirations: {
WARN: 0,
KICK: 0,
MUTE: 0,
SOFTBAN: 0,
BAN: 0,
VCMUTE: 0,
VCKICK: 0,
VCBAN: 0
},
associations: {},
multiplier: false //expiration = expiration*points
}
},
archivable: false
});
}
async handle(message, args) {
const setting = message.guild._settings[this.index];
// eslint-disable-next-line prefer-const
let [method, ...params] = args,
index = null,
langParams = {};
method = method.toLowerCase();
const { resolver } = this.client;
if (resolver.resolveBoolean(method)) {
setting.enabled = true;
index = 'S_MODERATIONPOINTS_TOGGLE';
langParams = { toggle: message.format('ON_OFF_TOGGLE', { toggle: true }, true) };
} else if (resolver.resolveBoolean(method) === false) {
setting.enabled = false;
index = 'S_MODERATIONPOINTS_TOGGLE';
langParams = { toggle: message.format('ON_OFF_TOGGLE', { toggle: false }, true) };
} else if (method === 'multiplier') {
const [bool] = params;
if (resolver.resolveBoolean(bool)) setting.multiplier = true;
else if (resolver.resolveBooeal(bool) === false) setting.multiplier = false;
else return {
error: true,
msg: message.format('ERR_INVALID_SUBMETHOD', { submethod: bool })
};
} else if (method === 'associate') {
const { method: submethod, changed } = await resolver.resolveMethod(params, null, Object.keys(setting.associations));
if (submethod === 'reset') {
setting.associations = {};
index = 'S_MODERATIONPOINTS_ASSOCIATE_RESET';
} else if (submethod === 'remove') {
for (const elem of changed) delete setting.associations[elem];
index = 'S_MODERATIONPOINTS_REMOVE';
langParams = { words: changed.join(', ') };
} else {
// eslint-disable-next-line prefer-const
let [word, arg] = params;
word = word.toLowerCase();
const points = parseInt(arg);
if (isNaN(points)) return { error: true, msg: message.format('S_MODERATIONPOINTS_NAN', { arg }) };
if (points < 0) return { error: true, msg: message.format('S_MODERATIONPOINTS_TOOSMALL', { arg: points }) };
if (points > 100) return { error: true, msg: message.format('S_MODERATIONPOINTS_TOOBIG', { arg: points }) };
setting.associations[word] = points;
index = 'S_MODERATIONPOINTS_ASSOCIATE';
langParams = { word, points };
}
} else if (resolver.resolveInfraction(method)) { //CONSTANTS.INFRACTIONS.includes(method.toLowerCase())
// eslint-disable-next-line prefer-const
let [submethod, value] = params;
submethod = submethod.toLowerCase();
const infraction = resolver.resolveInfraction(method);
if (!CONSTANTS.INFRACTIONS.includes(infraction)) return { error: true, msg: message.format('S_MODERATIONPOINTS_INVALID_TYPE', { infraction }) };
if (['points', 'point', 'p'].includes(submethod) || !isNaN(parseInt(submethod))) {
if (!value) value = submethod;
const points = parseInt(value);
if (isNaN(points)) return { error: true, msg: message.format('S_MODERATIONPOINTS_NAN', { arg: value }) };
if (points < 0) return { error: true, msg: message.format('S_MODERATIONPOINTS_TOOSMALL', { arg: points }) };
if (points > 100) return { error: true, msg: message.format('S_MODERATIONPOINTS_TOOBIG', { arg: points }) };
setting.points[infraction] = points;
index = 'S_MODERATIONPOINTS_ASSOCIATE';
langParams = { word: infraction, points };
} else if (['expire', 'expires'].includes(submethod)) {
// eslint-disable-next-line require-unicode-regexp
const timereg = /([0-9]{1,3}\s?[a-z]{1,7})|(0)/i;
const { content } = message;
if (!timereg.test(content)) return { error: true, msg: message.format('S_MODERATIONPOINTS_INVALID_TIMESTRING') };
const match = content.match(timereg);
const time = resolver.resolveTime(match[1]);
if (time === null) return { error: true, msg: message.format('S_MODERATIONPOINTS_INVALID_TIMESTRING') };
setting.expirations[infraction] = time;
index = 'S_MODERATIONPOINTS_EXPIRE';
langParams = { type: infraction, time: this.client.resolver.timeAgo(time) };
} else return {
error: true,
msg: message.format('ERR_INVALID_SUBMETHOD', { submethod })
};
} else return {
error: true,
msg: message.format('ERR_INVALID_METHOD', { method })
};
await message.guild._updateSettings({ [this.index]: setting });
return { msg: message.format(index, langParams), error: false };
}
async fields(guild) {
const setting = guild._settings[this.index];
return [
{
name: '》 Status',
value: guild.format('SETTING_STATUS', { bool: Boolean(setting?.enabled) }, true),
inline: true
},
{
name: '》 Default Points',
value: Object.entries(setting.points).map(([key, value]) => `**${key}:** \`${value}\``).join('\n'),
inline: true
},
{
name: '》 Default Expirations',
value: Object.entries(setting.expirations).map(([key, value]) => `**${key}:** \`${value ? this.client.resolver.timeAgo(value): 'N/A'}\``).join('\n'),
inline: true
}
];
}
}
module.exports = ModerationPointsSetting;