fixes to method resolver

This commit is contained in:
Erik 2020-06-10 17:48:26 +03:00
parent eb1df69a6e
commit 9f79b62e72

View File

@ -68,15 +68,17 @@ class Resolver {
* @param {Array<String>} args The incoming arguments with the first element being the method ex. ['add','ban','kick'] * @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>} 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 this
* @param {Function} resolver One of the resolver functions used to resolve the passed values into objects (should always be one of the mass resolver due to the nature of this method, might break otherwise) NOTE: REMEMBER TO BIND 'this' ARG! * @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
* @returns {Object} * @returns {Object}
* @memberof Resolver * @memberof Resolver
*/ */
async resolveMethod(args, valid, existing = [], resolver) { async resolveMethod(args, valid, existing = [], resolver, guild) {
const methods = { const methods = {
list: ['view', 'list', '?', 'show'], list: ['view', 'list', '?', 'show'],
add: ['add', '+'], add: ['add', '+'],
set: ['set', '='],
remove: ['remove', 'delete', '-'], remove: ['remove', 'delete', '-'],
reset: ['clear', 'reset'], reset: ['clear', 'reset'],
off: ['off', 'disable', 'false', 'no', 'n', 'f'], off: ['off', 'disable', 'false', 'no', 'n', 'f'],
@ -90,18 +92,18 @@ class Resolver {
let resolved = []; let resolved = [];
if (methods.reset.includes(method)) return { method: 'reset' }; if (methods.reset.includes(method)) return { method: 'reset' };
if (methods.off.includes(method)) return { method: 'off' };
if (methods.on.includes(method)) return { method: 'on' };
if (!rest.length) { if (!rest.length) {
if (methods.list.includes(method)) return { method: 'list' };
if (methods.add.includes(method)) return { method: 'add' }; if (methods.add.includes(method)) return { method: 'add' };
if (methods.remove.includes(method)) return { method: 'remove' }; if (methods.remove.includes(method)) return { method: 'remove' };
if (methods.off.includes(method)) return { method: 'off' }; if (methods.set.includes(method)) return { method: 'set' };
if (methods.on.includes(method)) return { method: 'on' };
} }
if (methods.list.includes(method)) { if (methods.list.includes(method)) {
if (resolver) resolved = resolver(existing); if (resolver) resolved = await resolver(existing, false, guild);
return { method: 'list', resolved }; return { method: 'list', resolved };
} else if (methods.add.includes(method)) { } else if (methods.add.includes(method)) {
@ -110,7 +112,7 @@ class Resolver {
for (let elem of rest) { for (let elem of rest) {
if (resolver) { if (resolver) {
const _resolved = await resolver(elem); const _resolved = await resolver(elem, false, guild);
if (_resolved) { if (_resolved) {
if(!resolved.includes(_resolved[0])) resolved.push(_resolved[0]); if(!resolved.includes(_resolved[0])) resolved.push(_resolved[0]);
elem = _resolved[0].id; elem = _resolved[0].id;
@ -134,7 +136,7 @@ class Resolver {
for (let elem of rest) { for (let elem of rest) {
if (resolver) { if (resolver) {
const _resolved = await resolver(elem); const _resolved = await resolver(elem, false, guild);
if (_resolved) { if (_resolved) {
if (!resolved.includes(_resolved[0])) resolved.push(_resolved[0]); if (!resolved.includes(_resolved[0])) resolved.push(_resolved[0]);
elem = _resolved[0].id; elem = _resolved[0].id;
@ -150,6 +152,23 @@ class Resolver {
return { rest, method: 'remove', changed: removed, result: existing, resolved }; return { rest, method: 'remove', changed: removed, result: existing, resolved };
} else if (methods.set.includes(method)) {
const set = [];
for (let elem of rest) {
if (resolver) {
const _resolved = await resolver(elem, false, guild);
if (_resolved) {
if (!resolved.includes(_resolved[0])) resolved.push(_resolved[0]);
elem = _resolved[0].id;
}
}
}
return { rest, method: 'set', changed: set, result: set, resolved };
} }
return false; return false;
@ -239,13 +258,13 @@ class Resolver {
const m = resolveable.match(/^@?([\S\s]{1,32})#([0-9]{4})/u); const m = resolveable.match(/^@?([\S\s]{1,32})#([0-9]{4})/u);
const username = m[1].toLowerCase(); const username = m[1].toLowerCase();
const discrim = m[2].toLowerCase(); const discrim = m[2].toLowerCase();
const user = users.cache.filter((u) => u.username.toLowerCase() === username && u.discriminator === discrim).first(); const user = users.cache.sort((a, b) => a.name.length - b.name.length).filter((u) => u.username.toLowerCase() === username && u.discriminator === discrim).first();
if(user) resolved.push(user); if(user) resolved.push(user);
} else if(!strict) { } else if(!strict) {
const name = resolveable.toLowerCase(); const name = resolveable.toLowerCase();
const user = users.cache.filter((u) => u.username.toLowerCase().includes(name)).first(); const user = users.cache.sort((a, b) => a.name.length - b.name.length).filter((u) => u.username.toLowerCase().includes(name)).first();
if(user) resolved.push(user); if(user) resolved.push(user);
} }
@ -315,7 +334,7 @@ class Resolver {
} else if((/^@?([\S\s]{1,32})/u).test(resolveable) && guild && !strict) { } else if((/^@?([\S\s]{1,32})/u).test(resolveable) && guild && !strict) {
const nickname = resolveable.match(/^@?([\S\s]{1,32})/u)[0].toLowerCase(); const nickname = resolveable.match(/^@?([\S\s]{1,32})/u)[0].toLowerCase();
const member = members.cache.filter((m) => m && m.user && const member = members.cache.sort((a, b) => a.name.length - b.name.length).filter((m) => m && m.user &&
((!m.nickname ? false : m.nickname.toLowerCase() === nickname) || ((!m.nickname ? false : m.nickname.toLowerCase() === nickname) ||
(!m.nickname ? false : m.nickname.toLowerCase().includes(nickname)) || (!m.nickname ? false : m.nickname.toLowerCase().includes(nickname)) ||
m.user.username.toLowerCase().includes(nickname) || m.user.username.toLowerCase().includes(nickname) ||
@ -381,7 +400,7 @@ class Resolver {
const match = resolveable.match(name); const match = resolveable.match(name);
const ch = match[1].toLowerCase(); const ch = match[1].toLowerCase();
const channel = CM.cache.filter(filter).filter((c) => { const channel = CM.cache.sort((a, b) => a.name.length - b.name.length).filter(filter).filter((c) => {
if (!strict) return c.name.toLowerCase().includes(ch); if (!strict) return c.name.toLowerCase().includes(ch);
return c.name.toLowerCase() === ch; return c.name.toLowerCase() === ch;
}).first(); }).first();
@ -437,7 +456,7 @@ class Resolver {
} else { } else {
const role = roles.cache.filter((r) => { const role = roles.cache.sort((a, b) => a.name.length - b.name.length).filter((r) => {
if(!strict) return r.name.toLowerCase().includes(resolveable.toLowerCase()); if(!strict) return r.name.toLowerCase().includes(resolveable.toLowerCase());
return r.name.toLowerCase() === resolveable.toLowerCase(); return r.name.toLowerCase() === resolveable.toLowerCase();
}).first(); }).first();