From 44ead5407cc49e01f56f223b9e6097a2cc57db8a Mon Sep 17 00:00:00 2001 From: Navy Date: Mon, 25 May 2020 00:39:37 +0300 Subject: [PATCH] formatting bs and some other bs --- structure/client/Resolver.js | 86 +++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 11 deletions(-) diff --git a/structure/client/Resolver.js b/structure/client/Resolver.js index e50be86..86cad48 100644 --- a/structure/client/Resolver.js +++ b/structure/client/Resolver.js @@ -18,6 +18,35 @@ class Resolver { } + timeAgo(diff, extraMin = false, extraHours = false, extraDays = false) { + + diff = parseInt(diff); + if (isNaN(diff)) return 'that ain\'t it chief (not a number)'; + + const years = ~~(diff / 60 / 60 / 24 / 365), + months = ~~(diff / 60 / 60 / 24 / 30.4), + weeks = extraDays ? ~~(diff / 60 / 60 / 24 / 7) : (diff / 60 / 60 / 24 / 7).toFixed(), + days = extraHours ? ~~(diff / 60 / 60 / 24) : (diff / 60 / 60 / 24).toFixed(), + hours = extraMin ? ~~(diff / 60 / 60) : (diff / 60 / 60).toFixed(), + minutes = (diff / 60).toFixed(); + + if (days > 365) { + return `${years > 0 ? years : 1} year${years > 1 ? 's' : ''} ${months % 12 > 0 ? `${months % 12} month${months % 12 > 1 ? 's' : ''}` : ''}`; + } else if (weeks > 4) { + return `${months} month${months % 12 > 1 ? 's' : ''} ${days % 30 > 0 ? `${days % 30} day${days % 30 > 1 ? 's' : ''}` : ''}`; + } else if (days >= 7) { + return `${weeks} week${weeks > 1 ? 's' : ''} ${extraDays && days % 7 > 0 ? `${days % 7} day${days % 7 > 1 ? 's' : ''}` : ''}`; + } else if (hours >= 24) { + return `${days} day${days > 1 ? 's' : ''} ${extraHours && hours % 24 > 0 ? `${hours % 24} hour${hours % 24 > 1 ? 's' : ''}` : ''}`; + } else if (minutes >= 60) { + return `${hours} hour${hours > 1 ? 's' : ''} ${extraMin && minutes % 60 > 0 ? `${minutes % 60} minute${minutes % 60 > 1 ? 's' : ''}` : ''}`; + } else if (diff >= 60) { + return `${minutes} minute${minutes > 1 ? 's' : ''}`; + } + return diff.toFixed() + ` second${diff.toFixed() !== 1 ? 's' : ''}`; + + } + resolveBoolean(input) { input = input.toLowerCase(); const truthy = [ 'on', 'true', 'yes', 'enable', 'y' ]; @@ -38,52 +67,83 @@ class Resolver { * @param {Array} args The incoming arguments with the first element being the method ex. ['add','ban','kick'] * @param {Array} valid An array of items to compare to, if an argument doesn't exist in this array it'll be skipped over * @param {Array} [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) + * @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! * @returns {Object} * @memberof Resolver */ - resolveMethod(args, valid, existing = [], resolver) { + async resolveMethod(args, valid, existing = [], resolver) { const methods = { - list: ['view', 'list', '?'], + list: ['view', 'list', '?', 'show'], add: ['add', '+'], - remove: ['remove', 'delete', '-'] + remove: ['remove', 'delete', '-'], + reset: ['clear', 'reset'] }; if (!args.length) return false; // eslint-disable-next-line prefer-const let [method, ...rest] = args; method = method.toLowerCase(); + let resolved = []; if (!rest.length) { - if (methods.list.includes(method)) return { method: 'list', rest }; - if (methods.add.includes(method)) return { method: 'add', rest }; - if (methods.remove.includes(method)) return { method: 'remove', rest }; + if (methods.list.includes(method)) return { method: 'list' }; + if (methods.add.includes(method)) return { method: 'add' }; + if (methods.remove.includes(method)) return { method: 'remove' }; + if (methods.reset.includes(method)) return { method: 'reset' }; } if (methods.list.includes(method)) { - if(resolver) - return { method: 'list' }; + + if (resolver) resolved = resolver(existing); + return { method: 'list', resolved }; + } else if (methods.add.includes(method)) { + const added = []; for (let elem of rest) { + + if (resolver) { + const _resolved = await resolver(elem); + if (_resolved) { + if(!resolved.includes(_resolved[0])) resolved.push(_resolved[0]); + elem = _resolved[0].id; + } + } + elem = elem.toLowerCase(); if (existing.includes(elem) || valid && !valid.includes(elem)) continue; + added.push(elem); existing.push(elem); + + } - return { rest, method: 'add', changed: added, result: existing }; + return { rest, method: 'add', changed: added, result: existing, resolved }; + } else if (methods.remove.includes(method)) { + const removed = []; for (let elem of rest) { + + if (resolver) { + const _resolved = await resolver(elem); + if (_resolved) { + if (!resolved.includes(_resolved[0])) resolved.push(_resolved[0]); + elem = _resolved[0].id; + } + } + elem = elem.toLowerCase(); if (!existing.includes(elem) || removed.includes(elem) || valid && !valid.includes(elem)) continue; removed.push(elem); existing.splice(existing.indexOf(elem), 1); + } - return { rest, method: 'remove', changed: removed, result: existing }; + return { rest, method: 'remove', changed: removed, result: existing, resolved }; + } return false; @@ -193,6 +253,7 @@ class Resolver { async resolveUser(resolveable, strict) { if (!resolveable) return false; + if (resolveable instanceof Array) throw new Error('Resolveable cannot be of type Array, use resolveUsers for resolving arrays of users'); const result = await this.resolveUsers([ resolveable ], strict); return result ? result[0] : false; @@ -265,6 +326,7 @@ class Resolver { async resolveMember(resolveable, guild, strict) { if (!resolveable) return false; + if (resolveable instanceof Array) throw new Error('Resolveable cannot be of type Array, use resolveMembers for resolving arrays of members'); const result = await this.resolveMembers([ resolveable ], guild, strict); return result ? result[0] : false; @@ -329,6 +391,7 @@ class Resolver { resolveChannel(resolveable, guild, strict) { if (!resolveable) return false; + if (resolveable instanceof Array) throw new Error('Resolveable cannot be of type Array, use resolveChannels for resolving arrays of channels'); const result = this.resolveChannels([resolveable], guild, strict); return result ? result[0] : false; @@ -383,6 +446,7 @@ class Resolver { async resolveRole(resolveable, guild, strict) { if (!resolveable) return false; + if (resolveable instanceof Array) throw new Error('Resolveable cannot be of type Array, use resolveRoles for resolving arrays of roles'); const result = await this.resolveRoles([resolveable], guild, strict); return result ? result[0] : false;