invite filter setting

This commit is contained in:
Erik 2020-11-12 00:58:05 +02:00
parent 2675cafef4
commit 9013dbe5ee

View File

@ -0,0 +1,201 @@
const { FilterSetting } = require('../../../../interfaces/');
module.exports = class InviteFilter extends FilterSetting {
constructor(client) {
super(client, {
name: 'inviteFilter',
module: 'moderation',
aliases: [],
resolve: 'GUILD',
usage: '<option> <method> <value..>',
examples: [
],
default: {
inviteFilter: {
enabled: false,
silent: false,
bypass: [],
ignore: [],
actions: []
}
}
});
}
async handle(message, params) {
const [_method, ...args] = params;
const method = _method.toLowerCase();
const { resolver } = this.client;
const { guild } = message;
const setting = guild._settings[this.index];
let index = null,
langParams = {};
const bool = resolver.resolveBoolean(method);
if (bool === null && !args.length) return {
error: true,
msg: message.format('MISSING_ARGS')
};
if (bool || bool === false) {
index = 'S_INVITEFILTER_TOGGLE';
langParams.toggle = message.format('ON_OFF_TOGGLE', { toggle: bool }, true);
setting.enabled = bool;
} else if (['ignore', 'channelignore', 'ignorechannel'].includes(method)) {
const resolved = await resolver.resolveMethod(args, {
existing: setting.ignore,
resolver: resolver.resolveChannels.bind(resolver),
guild,
allowedMethods: ['add', 'remove', 'set', 'reset']
});
if (!resolved) return {
error: true,
msg: message.format('ERR_INVALID_METHOD', { method: args[0] })
};
index = 'S_INVITEFILTER_IGNORE_' + resolved.method.toUpperCase();
langParams.changed = resolved.resolved.join(', ');
setting.ignore = resolved.result;
} else if (['bypass', 'roleignore', 'ignorerole'].includes(method)) {
const resolved = await resolver.resolveMethod(args, {
existing: setting.bypass,
resolver: resolver.resolveRoles.bind(resolver),
guild,
allowedMethods: ['add', 'remove', 'set', 'reset']
});
if (!resolved) return {
error: true,
msg: message.format('ERR_INVALID_METHOD', { method: args[0] })
};
index = 'S_INVITEFILTER_BYPASS_' + resolved.method.toUpperCase();
langParams.changed = resolved.resolved.map((role) => role.name).join('**, **');
setting.bypass = resolved.result;
} else if (['silent', 'shutup'].includes(method)) {
const bool = resolver.resolveBoolean(args[0]);
if (bool === null) return {
error: true,
msg: message.format('ERR_INVALID_METHOD', { method: args[0] })
};
index = 'S_INVITEFILTER_SILENT_TOGGLE';
langParams.toggle = message.format('ON_OFF_TOGGLE', { toggle: bool }, true);
setting.silent = bool;
} else if (['action', 'actions'].includes(method)) {
const submethod = args.shift().toLowerCase();
const resolved = await resolver.resolveMethod([submethod], {
allowedMethods: ['add', 'remove', 'edit', 'list', 'reset']
});
if (!resolved) return { error: true, msg: message.format('ERR_INVALID_METHOD', { method: submethod }) };
let result = null;
if (resolved.method === 'add') {
if (setting.actions.length > 0) return {
error: true,
msg: message.format('S_INVITEFILTER_ACTION_EXISTS')
};
result = await this._createAction(message, setting);
index = 'S_FILTER_ACTION_ADD';
} else if (resolved.method === 'edit') {
result = await this._editAction(message, setting);
index = 'S_FILTER_ACTION_EDIT';
} else if (resolved.method === 'remove') {
result = await this._removeAction(message, setting);
index = 'S_FILTER_ACTION_REMOVE';
} else if (resolved.method === 'list') {
this._listActions(message, setting);
return;
} else if (resolved.method === 'reset') {
result = {};
setting.actions = [];
index = 'S_FILTER_ACTION_RESET';
}
if (result.error) return result;
//setting.actions.push(result);
//console.log(result);
langParams = {
type: result.type,
duration: result.duration ? resolver.timeAgo(result.duration) : 'INF',
points: result.points || message.format('ON_OFF_TOGGLE', { toggle: false }, true),
force: message.format('ON_OFF_TOGGLE', { toggle: result.force }, true),
prune: message.format('ON_OFF_TOGGLE', { toggle: result.prune }, true),
trigger: result.trigger instanceof Array ? '||' + result.trigger.join(', ') + '||' : result.trigger
};
}
await message.guild._updateSettings({ [this.index]: setting });
return {
error: false,
msg: message.format(index, langParams)
};
}
async _createTrigger(message, action, actionObject) {
actionObject.trigger = 'generic';
}
async fields(guild) {
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: '》 Silent',
value: guild.format('SETTING_STATUS', { bool: Boolean(setting.silent) }, true),
inline: true
},
{
name: '》 Ignored channels',
value: setting.ignore.map((channel) => `<#${channel}>`).join(', ') || '`N/A`',
inline: true
},
{
name: '》 Role bypass',
value: setting.bypass.map((role) => `<@&${role}>`).join(', ') || '`N/A`',
inline: true
},
{
name: '》 Actions',
value: setting.actions.reduce((acc, val) => {
let str = `**${val.type}**`;
if (val.points) str += ` (${val.points} points)`;
if (val.duration) str += ` for ${resolver.timeAgo(val.duration, true, true, true)}`;
if (val.force) str += ` - **FORCE**`;
if (val.prune) str += ` - **PRUNE**`;
str += `\n__**Triggers:**__ ${val.trigger instanceof Array ? val.trigger.join(', ') : '`' + val.trigger + '`'}`; //result.trigger instanceof Array ? result.trigger.join(', ') : result.trigger
acc.push(str);
return acc;
}, []).join('\n') || '`N/A`'
}
];
}
};