resolve mod action for filter settings

misc things for other resolvers, added allowedmethods to resolvemethod
This commit is contained in:
Erik 2020-09-20 01:45:23 +03:00
parent ad4383ee8c
commit 5edd6963fe

View File

@ -2,6 +2,7 @@ const timestring = require('timestring');
const moment = require('moment');
const { InfractionResolves } = require('../../util/Constants.js');
const { Guild, GuildChannel, Role, User } = require('discord.js');
class Resolver {
@ -173,6 +174,7 @@ class Resolver {
/**
* Resolves methods used primarily for settings, also deals with appending the arguments into existing lists
* TODO: Refactor to use an options object instead of an ungodly amount of args
*
* @param {Array<String>} args The incoming arguments with the first element being the method ex. ['add','ban','kick']
* @param {Array<String>} valid An array of items to compare to, if an argument doesn't exist in this array it'll be skipped over
@ -180,10 +182,11 @@ class Resolver {
* @param {Function} resolver One of the resolver functions used to resolve the passed values into objects (should always be one of the mass resolvers due to the nature of this method, might break otherwise) NOTE: REMEMBER TO BIND 'this' ARG!
* @param {Guild} guild The guild for the resolver to use when resolving
* @param {Boolean} caseSensitive Whether or not to preserve case
* @param {Array || String} [allowedMethods='all'] Which methods to allow to be resolved
* @returns {Object}
* @memberof Resolver
*/
async resolveMethod(args, valid, existing = [], resolver, guild, caseSensitive = false) {
async resolveMethod(args, valid, existing = [], resolver, guild, caseSensitive = false, allowedMethods = 'all') {
const methods = {
list: ['view', 'list', '?', 'show'],
@ -195,28 +198,30 @@ class Resolver {
on: ['on', 'enable', 'true', 'yes', 'y', 't']
};
if(allowedMethods === 'all') allowedMethods = Object.keys(methods);
if (!args.length) return false;
// eslint-disable-next-line prefer-const
let [method, ...rest] = args;
method = method.toLowerCase();
let resolved = [];
if (methods.reset.includes(method)) return { method: 'reset', result: [], changed: [], resolved };
if (methods.off.includes(method)) return { method: 'off' };
if (methods.on.includes(method)) return { method: 'on' };
if (methods.reset.includes(method) && allowedMethods.includes('reset')) return { method: 'reset', result: [], changed: [], resolved };
if (methods.off.includes(method) && allowedMethods.includes('off')) return { method: 'off' };
if (methods.on.includes(method) && allowedMethods.includes('on')) return { method: 'on' };
if (!rest.length) {
if (methods.add.includes(method)) return { method: 'add' };
if (methods.remove.includes(method)) return { method: 'remove' };
if (methods.set.includes(method)) return { method: 'set' };
if (methods.add.includes(method) && allowedMethods.includes('add')) return { method: 'add' };
if (methods.remove.includes(method) && allowedMethods.includes('remove')) return { method: 'remove' };
if (methods.set.includes(method) && allowedMethods.includes('set')) return { method: 'set' };
}
if (methods.list.includes(method)) {
if (methods.list.includes(method) && allowedMethods.includes('list')) {
if (resolver) resolved = await resolver(existing, false, guild);
return { method: 'list', resolved };
} else if (methods.add.includes(method)) {
} else if (methods.add.includes(method) && allowedMethods.includes('add')) {
const added = [];
for (let elem of rest) {
@ -238,7 +243,7 @@ class Resolver {
return { rest, method: 'add', changed: added, result: existing, resolved };
} else if (methods.remove.includes(method)) {
} else if (methods.remove.includes(method) && allowedMethods.includes('remove')) {
const removed = [];
for (let elem of rest) {
@ -260,7 +265,7 @@ class Resolver {
return { rest, method: 'remove', changed: removed, result: existing, resolved };
} else if (methods.set.includes(method)) {
} else if (methods.set.includes(method) && allowedMethods.includes('set')) {
const set = [];
for (let elem of rest) {
@ -290,9 +295,9 @@ class Resolver {
/**
* Resolve several user resolveables
*
* @param {array<string>} [resolveables=[]] an array of user resolveables (name, id, tag)
* @param {boolean} [strict=false] whether or not to attempt resolving by partial usernames
* @returns {array || boolean} Array of resolved users or false if none were resolved
* @param {Array<String>} [resolveables=[]] an array of user resolveables (name, id, tag)
* @param {Boolean} [strict=false] whether or not to attempt resolving by partial usernames
* @returns {Promise<Array<User>> || boolean} Array of resolved users or false if none were resolved
* @memberof Resolver
*/
async resolveUsers(resolveables = [], strict = false) {
@ -358,10 +363,10 @@ class Resolver {
/**
* Resolve multiple member resolveables
*
* @param {array<string>} [resolveables=[]] an array of member resolveables (name, nickname, tag, id)
* @param {boolean} [strict=false] whether or not to attempt resolving by partial matches
* @param {Array<String>} [resolveables=[]] an array of member resolveables (name, nickname, tag, id)
* @param {Boolean} [strict=false] whether or not to attempt resolving by partial matches
* @param {Guild} guild the guild in which to look for members
* @returns {array<GuildMember> || boolean} an array of resolved members or false if none were resolved
* @returns {Promise<Array<GuildMember>> || Promise<Boolean>} an array of resolved members or false if none were resolved
* @memberof Resolver
*/
async resolveMembers(resolveables = [], strict = false, guild = null) {
@ -432,11 +437,11 @@ class Resolver {
/**
* Resolve multiple channels
*
* @param {array<string>} [resolveables=[]] an array of channel resolveables (name, id)
* @param {guild} guild the guild in which to look for channels
* @param {boolean} [strict=false] whether or not partial names are resolved
* @param {function} [filter=()] filter the resolving channels
* @returns {array<GuildChannel> || false} an array of guild channels or false if none were resolved
* @param {Array<String>} [resolveables=[]] an array of channel resolveables (name, id)
* @param {Guild} guild the guild in which to look for channels
* @param {Boolean} [strict=false] whether or not partial names are resolved
* @param {Function} [filter=()] filter the resolving channels
* @returns {Promise<Array<GuildChannel>> || Promise<Boolean>} an array of guild channels or false if none were resolved
* @memberof Resolver
*/
async resolveChannels(resolveables = [], strict = false, guild = null, filter = () => true) {
@ -499,10 +504,10 @@ class Resolver {
/**
* Resolve multiple roles
*
* @param {array<string>} [resolveables=[]] an array of roles resolveables (name, id)
* @param {Array<String>} [resolveables=[]] an array of roles resolveables (name, id)
* @param {Guild} guild the guild in which to look for roles
* @param {boolean} [strict=false] whether or not partial names are resolved
* @returns {array<GuildRole> || false} an array of roles or false if none were resolved
* @param {Boolean} [strict=false] whether or not partial names are resolved
* @returns {Promise<Array<Role>> || Promise<Boolean>} an array of roles or false if none were resolved
* @memberof Resolver
*/
async resolveRoles(resolveables = [], strict = false, guild = null) {
@ -597,9 +602,9 @@ class Resolver {
/**
* Iterate through several arguments attempting to resolve them with the passed resolvers
*
* @param {Array<string>} [args=[]]
* @param {Array<String>} [args=[]]
* @param {Array<Function>} [resolvers=[]]
* @param {boolean} strict
* @param {Boolean} strict
* @param {Guild} guild
* @returns
*/
@ -630,6 +635,50 @@ class Resolver {
return { parsed, parameters };
}
// 'wordfilter actions word mute 5 min 1 point force' // time reg: (\d{1,3}\s?[a-z]{1,7} ?){1,2} | points reg: \d{1,3}\s?(points?|pts?|p) -- parse points first
resolveModAction(input, validActions = null) {
const points = /(\d{1,3})\s?(points?|pts?|p)/i;
const time = /(\d{1,3}\s?[a-z]{1,7})\s?(\d{1,3}\s?[a-z]{1,7})?/i;
const force = /force/i;
const prune = /prune/i;
const result = { action: null, duration: null, points: null, force: false, prune: false };
const [action, ...rest] = input.split(' ');
const inf = this.resolveInfraction(action);
if (!inf || (validActions && !validActions.includes(inf))) return { error: true, template: 'ERR_INVALID_INFRACTION' };
input = rest.join(' ');
result.action = inf;
if (force.test(input)) {
result.force = true;
input = input.replace(force, '');
}
if (prune.test(input)) {
result.prune = true;
input = input.replace(prune, '');
}
if (points.test(input)) {
const match = input.match(points);
const pts = parseInt(match[1]);
if(isNaN(pts) || pts < 0 || pts > 100) return { error: true, template: 'S_AUTOMOD_INVALID_POINTS' };
result.points = pts;
input = input.replace(match[0], '');
}
if (time.test(input)) {
const match = input.match(time);
const tiem = this.resolveTime(match[0]);
if(!tiem) return { error: true, template: 'ERR_INVALID_TIMESTRING' };
result.duration = tiem;
}
return result;
}
}