forked from Galactic/galactic-bot
domain resolver and general resolver improvements
This commit is contained in:
parent
e4223205a6
commit
a92601caa9
@ -1,5 +1,6 @@
|
||||
const timestring = require('timestring');
|
||||
const moment = require('moment');
|
||||
const dns = require('dns');
|
||||
|
||||
const { InfractionResolves } = require('../../util/Constants.js');
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
@ -195,7 +196,7 @@ class Resolver {
|
||||
*
|
||||
* @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
|
||||
* @param {Array<String>} [existing=[]] Existing values in the array, valid elements will be appended to this
|
||||
* @param {Array<String>} [existing=[]] Existing values in the array, valid elements will be appended to or removed from this
|
||||
* @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
|
||||
@ -203,7 +204,7 @@ class Resolver {
|
||||
* @returns {Object}
|
||||
* @memberof Resolver
|
||||
*/
|
||||
async resolveMethod(args, valid, existing = [], resolver, guild, caseSensitive = false, allowedMethods = 'all') {
|
||||
async resolveMethod(args, valid, existing = [], { resolver, guild, caseSensitive = false, allowedMethods = 'all' }) {
|
||||
|
||||
const methods = {
|
||||
list: ['view', 'list', '?', 'show'],
|
||||
@ -243,13 +244,17 @@ class Resolver {
|
||||
} else if (methods.add.includes(method) && allowedMethods.includes('add')) {
|
||||
|
||||
const added = [];
|
||||
const failed = [];
|
||||
for (let elem of rest) {
|
||||
if (resolver) {
|
||||
const _resolved = await resolver(elem, false, guild);
|
||||
const [_resolved] = await resolver(elem, false, guild);
|
||||
if (_resolved) {
|
||||
if(!resolved.includes(_resolved[0])) resolved.push(_resolved[0]);
|
||||
elem = _resolved[0].id;
|
||||
} else continue;
|
||||
if (!resolved.includes(_resolved)) resolved.push(_resolved);
|
||||
if (_resolved.id) elem = _resolved.id;
|
||||
} else {
|
||||
failed.push(elem);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if(!caseSensitive) elem = elem.toLowerCase();
|
||||
@ -260,11 +265,12 @@ class Resolver {
|
||||
|
||||
}
|
||||
|
||||
return { rest, method: 'add', changed: added, result: existing, resolved };
|
||||
return { rest, method: 'add', changed: added, result: existing, resolved, failed };
|
||||
|
||||
} else if (methods.remove.includes(method) && allowedMethods.includes('remove')) {
|
||||
|
||||
const removed = [];
|
||||
const failed = [];
|
||||
for (let elem of rest) {
|
||||
|
||||
if (resolver) {
|
||||
@ -272,7 +278,10 @@ class Resolver {
|
||||
if (_resolved) {
|
||||
if (!resolved.includes(_resolved[0])) resolved.push(_resolved[0]);
|
||||
elem = _resolved[0].id;
|
||||
} else continue;
|
||||
} else {
|
||||
failed.push(elem);
|
||||
//continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!caseSensitive) elem = elem.toLowerCase();
|
||||
@ -282,11 +291,12 @@ class Resolver {
|
||||
|
||||
}
|
||||
|
||||
return { rest, method: 'remove', changed: removed, result: existing, resolved };
|
||||
return { rest, method: 'remove', changed: removed, result: existing, resolved, failed };
|
||||
|
||||
} else if (methods.set.includes(method) && allowedMethods.includes('set')) {
|
||||
|
||||
const set = [];
|
||||
const failed = [];
|
||||
for (let elem of rest) {
|
||||
|
||||
if (resolver) {
|
||||
@ -294,7 +304,10 @@ class Resolver {
|
||||
if (_resolved) {
|
||||
if (!resolved.includes(_resolved[0])) resolved.push(_resolved[0]);
|
||||
elem = _resolved[0].id;
|
||||
} else continue;
|
||||
} else {
|
||||
failed.push(elem);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!caseSensitive) elem = elem.toLowerCase();
|
||||
@ -303,7 +316,7 @@ class Resolver {
|
||||
|
||||
}
|
||||
|
||||
return { rest, method: 'set', changed: set, result: set, resolved };
|
||||
return { rest, method: 'set', changed: set, result: set, resolved, failed };
|
||||
|
||||
}
|
||||
|
||||
@ -311,6 +324,49 @@ class Resolver {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve an array of domains
|
||||
*
|
||||
* @param {Array<String>} [links=[]]
|
||||
* @memberof Resolver
|
||||
*/
|
||||
async resolveDomains(links = []) {
|
||||
|
||||
if (!(links instanceof Array)) links = [links];
|
||||
const resolved = [];
|
||||
const linkReg = /(https?:\/\/)?([a-z0-9-]{1,63}\.)?([a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})(\.[a-z0-9-]{2,63})?/iu;
|
||||
const ipReg = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/u; //Very loose IP regex
|
||||
|
||||
for (const link of links) {
|
||||
|
||||
const match = link.match(linkReg);
|
||||
if (!match) continue;
|
||||
|
||||
const domain = (match[2] || '')
|
||||
+ (match[3] || '')
|
||||
+ (match[4] || '')
|
||||
+ (match[5] || '');
|
||||
|
||||
if (ipReg.test(domain)) continue;
|
||||
const result = await this.validateDomain(domain);
|
||||
|
||||
if (result) resolved.push(domain);
|
||||
|
||||
}
|
||||
|
||||
return resolved.length ? resolved : false;
|
||||
|
||||
}
|
||||
|
||||
validateDomain(domain) {
|
||||
return new Promise((resolve) => {
|
||||
dns.resolveAny(domain, (error) => {
|
||||
if (error) resolve(false);
|
||||
resolve(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve several user resolveables
|
||||
*
|
||||
@ -654,58 +710,6 @@ 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
|
||||
/**
|
||||
* Resolves a mod action object from a string
|
||||
*
|
||||
* @param {String} input
|
||||
* @param {Array<String>} [validActions=null]
|
||||
* @returns {Object}
|
||||
* @memberof Resolver
|
||||
*/
|
||||
resolveModAction(input, validActions = null) {
|
||||
|
||||
const points = /(\d{1,3})\s?(points?|pts?|p)/iu;
|
||||
const time = /(\d{1,3}\s?[a-z]{1,7})\s?(\d{1,3}\s?[a-z]{1,7})?/iu;
|
||||
const force = /force/iu;
|
||||
const prune = /prune/iu;
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user