galactic-bot/structure/client/Resolver.js

470 lines
18 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-useless-escape */
2020-04-08 18:08:46 +02:00
class Resolver {
constructor(client) {
this.client = client;
}
components(str = '', type, exact = true) {
2020-04-08 18:08:46 +02:00
const string = str.toLowerCase();
const components = this.client.registry.components
.filter((c) => c.type === type)
.filter(exact ? filterExact(string) : filterInexact(string)) //eslint-disable-line no-use-before-define
2020-04-08 18:08:46 +02:00
.array();
return components || [];
}
2020-05-24 23:39:37 +02:00
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', 't' ];
const falsey = [ 'off', 'false', 'no', 'disable', 'n', 'f' ];
if(truthy.includes(input)) {
return true;
} else if (falsey.includes(input)) {
return false;
}
return null;
}
2020-05-24 00:10:01 +02:00
/**
* Resolves methods used primarily for settings, also deals with appending the arguments into existing lists
*
* @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
2020-05-24 23:39:37 +02:00
* @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!
2020-05-24 00:10:01 +02:00
* @returns {Object}
* @memberof Resolver
*/
2020-05-24 23:39:37 +02:00
async resolveMethod(args, valid, existing = [], resolver) {
2020-05-24 00:10:01 +02:00
const methods = {
2020-05-24 23:39:37 +02:00
list: ['view', 'list', '?', 'show'],
2020-05-24 00:10:01 +02:00
add: ['add', '+'],
2020-05-24 23:39:37 +02:00
remove: ['remove', 'delete', '-'],
reset: ['clear', 'reset'],
off: ['off', 'disable'],
on: ['on', 'enable']
2020-05-24 00:10:01 +02:00
};
if (!args.length) return false;
// eslint-disable-next-line prefer-const
let [method, ...rest] = args;
method = method.toLowerCase();
2020-05-24 23:39:37 +02:00
let resolved = [];
2020-05-24 00:10:01 +02:00
if (!rest.length) {
2020-05-24 23:39:37 +02:00
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' };
2020-05-24 00:10:01 +02:00
}
if (methods.list.includes(method)) {
2020-05-24 23:39:37 +02:00
if (resolver) resolved = resolver(existing);
return { method: 'list', resolved };
2020-05-24 00:10:01 +02:00
} else if (methods.add.includes(method)) {
2020-05-24 23:39:37 +02:00
2020-05-24 00:10:01 +02:00
const added = [];
for (let elem of rest) {
2020-05-24 23:39:37 +02:00
if (resolver) {
const _resolved = await resolver(elem);
if (_resolved) {
if(!resolved.includes(_resolved[0])) resolved.push(_resolved[0]);
elem = _resolved[0].id;
}
}
2020-05-24 00:10:01 +02:00
elem = elem.toLowerCase();
if (existing.includes(elem) || valid && !valid.includes(elem)) continue;
2020-05-24 23:39:37 +02:00
2020-05-24 00:10:01 +02:00
added.push(elem);
existing.push(elem);
2020-05-24 23:39:37 +02:00
2020-05-24 00:10:01 +02:00
}
2020-05-24 23:39:37 +02:00
return { rest, method: 'add', changed: added, result: existing, resolved };
2020-05-24 00:10:01 +02:00
} else if (methods.remove.includes(method)) {
2020-05-24 23:39:37 +02:00
2020-05-24 00:10:01 +02:00
const removed = [];
for (let elem of rest) {
2020-05-24 23:39:37 +02:00
if (resolver) {
const _resolved = await resolver(elem);
if (_resolved) {
if (!resolved.includes(_resolved[0])) resolved.push(_resolved[0]);
elem = _resolved[0].id;
}
}
2020-05-24 00:10:01 +02:00
elem = elem.toLowerCase();
if (!existing.includes(elem) || removed.includes(elem) || valid && !valid.includes(elem)) continue;
removed.push(elem);
existing.splice(existing.indexOf(elem), 1);
2020-05-24 23:39:37 +02:00
2020-05-24 00:10:01 +02:00
}
2020-05-24 23:39:37 +02:00
return { rest, method: 'remove', changed: removed, result: existing, resolved };
2020-05-24 00:10:01 +02:00
}
return false;
}
2020-04-08 18:08:46 +02:00
async resolveMemberAndUser(string, guild) {
const str = string.toLowerCase();
const index = guild ? guild.members.cache : this.client.users.cache;
2020-04-08 18:08:46 +02:00
let member = null;
if((/<@!?(\d{17,21})>/iyu).test(str)) { //mentions
const matches = (/<@!?(\d{17,21})>/iuy).exec(str);
2020-04-08 18:08:46 +02:00
member = index.get(matches[1]);
if(!member) {
try {
member = await index.fetch(matches[1]);
} catch(e) {
try {
member = await this.client.users.cache.fetch(matches[1]);
2020-04-08 18:08:46 +02:00
} catch(e) {} //eslint-disable-line no-empty
} //eslint-disable-line no-empty
}
} else if((/\d{17,21}/iuy).test(str)) { //id
const matches = (/(\d{17,21})/iuy).exec(str);
2020-04-08 18:08:46 +02:00
member = index.get(matches[1]);
if(!member) {
try {
member = await index.fetch(matches[1]);
} catch(e) {
try {
member = await this.client.users.cache.fetch(matches[1]);
2020-04-08 18:08:46 +02:00
} catch(e) {} //eslint-disable-line no-empty
} //eslint-disable-line no-empty
}
} else if((/(.{2,32})#(\d{4})/iuy).test(str)) { //username#discrim
const matches = (/(.{2,32})#(\d{4})/iuy).exec(str);
2020-04-08 18:08:46 +02:00
member = guild
? guild.members.cache.filter((m) => m.user.username === matches[1] && m.user.discriminator === matches[2]).first()
: this.client.users.cache.filter((u) => u.username === matches[1] && u.discriminator === matches[2]).first();
2020-04-08 18:08:46 +02:00
}
2020-04-08 18:08:46 +02:00
return member || null;
}
2020-04-11 10:06:39 +02:00
/**
* 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
* @memberof Resolver
*/
2020-04-09 11:18:14 +02:00
async resolveUsers(resolveables = [], strict = false) {
2020-04-08 18:08:46 +02:00
if(typeof resolveables === 'string') resolveables = [ resolveables ];
if(resolveables.length === 0) return false;
const { users } = this.client;
const resolved = [];
2020-04-08 18:08:46 +02:00
for(const resolveable of resolveables) {
2020-04-08 18:08:46 +02:00
if((/<@!?([0-9]{17,21})>/u).test(resolveable)) {
2020-04-08 18:08:46 +02:00
const [, id] = resolveable.match(/<@!?([0-9]{17,21})>/u);
const user = await users.fetch(id).catch((err) => {
if(err.code === 10013) return false;
this.client.logger.warn(err); return false;
});
2020-04-08 18:08:46 +02:00
if(user) resolved.push(user);
} else if((/(id:)?([0-9]{17,21})/u).test(resolveable)) {
2020-04-08 18:08:46 +02:00
const [,, id] = resolveable.match(/(id:)?([0-9]{17,21})/u);
const user = await users.fetch(id).catch((err) => {
if(err.code === 10013) return false;
this.client.logger.warn(err); return false;
});
2020-04-08 18:08:46 +02:00
if(user) resolved.push(user);
} else if((/^@?([\S\s]{1,32})#([0-9]{4})/u).test(resolveable)) {
2020-04-08 18:08:46 +02:00
const m = resolveable.match(/^@?([\S\s]{1,32})#([0-9]{4})/u);
const username = m[1].toLowerCase();
const discrim = m[2].toLowerCase();
const user = users.cache.filter((u) => u.username.toLowerCase() === username && u.discriminator === discrim).first();
2020-04-08 18:08:46 +02:00
if(user) resolved.push(user);
2020-04-09 11:18:14 +02:00
} else if(!strict) {
const name = resolveable.toLowerCase();
const user = users.cache.filter((u) => u.username.toLowerCase().includes(name)).first();
2020-04-09 11:18:14 +02:00
if(user) resolved.push(user);
2020-04-08 18:08:46 +02:00
}
}
return resolved.length ? resolved : false;
2020-04-08 18:08:46 +02:00
}
async resolveUser(resolveable, strict) {
2020-05-22 22:13:47 +02:00
if (!resolveable) return false;
2020-05-24 23:39:37 +02:00
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);
2020-05-17 13:04:58 +02:00
return result ? result[0] : false;
2020-05-22 22:13:47 +02:00
}
2020-04-11 10:06:39 +02:00
/**
* 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 {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
* @memberof Resolver
*/
async resolveMembers(resolveables = [], guild, strict = false) {
2020-04-08 18:08:46 +02:00
if(typeof resolveables === 'string') resolveables = [ resolveables ];
if(resolveables.length === 0) return false;
const { members } = guild;
const resolved = [];
2020-04-08 18:08:46 +02:00
for(const resolveable of resolveables) {
2020-04-08 18:08:46 +02:00
if((/<@!?([0-9]{17,21})>/u).test(resolveable)) {
2020-04-08 18:08:46 +02:00
const [, id] = resolveable.match(/<@!?([0-9]{17,21})>/u);
const member = await members.fetch(id).catch((err) => {
if(err.code === 10007) return false;
this.client.logger.warn(err); return false;
});
2020-04-08 18:08:46 +02:00
if(member) resolved.push(member);
} else if((/(id:)?([0-9]{17,21})/u).test(resolveable)) {
2020-04-09 11:18:14 +02:00
const [,, id] = resolveable.match(/(id:)?([0-9]{17,21})/u);
const member = await members.fetch(id).catch((err) => {
if(err.code === 10007) return false;
this.client.logger.warn(err); return false;
});
2020-04-09 11:18:14 +02:00
if(member) resolved.push(member);
} else if((/^@?([\S\s]{1,32})#([0-9]{4})/u).test(resolveable)) {
2020-04-09 11:18:14 +02:00
const m = resolveable.match(/^@?([\S\s]{1,32})#([0-9]{4})/u);
const username = m[1].toLowerCase();
const discrim = m[2].toLowerCase();
const member = members.cache.filter((m) => m.user.username.toLowerCase() === username && m.user.discriminator === discrim).first();
2020-04-09 11:18:14 +02:00
if(member) resolved.push(member);
} else if((/^@?([\S\s]{1,32})/u).test(resolveable) && guild && !strict) {
2020-04-09 11:18:14 +02:00
const nickname = resolveable.match(/^@?([\S\s]{1,32})/u)[0].toLowerCase();
const member = members.cache.filter((m) => m && m.user &&
((!m.nickname ? false : m.nickname.toLowerCase() === nickname) ||
2020-04-09 11:18:14 +02:00
(!m.nickname ? false : m.nickname.toLowerCase().includes(nickname)) ||
m.user.username.toLowerCase().includes(nickname) ||
m.user.username.toLowerCase() === nickname)).first();
2020-04-09 11:18:14 +02:00
if(member) resolved.push(member);
}
2020-04-08 18:08:46 +02:00
}
2020-04-11 10:06:39 +02:00
return resolved.length > 0 ? resolved : false;
}
async resolveMember(resolveable, guild, strict) {
2020-05-22 22:13:47 +02:00
if (!resolveable) return false;
2020-05-24 23:39:37 +02:00
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);
2020-05-17 13:04:58 +02:00
return result ? result[0] : false;
}
2020-04-11 10:06:39 +02:00
/**
2020-04-11 10:11:22 +02:00
* Resolve multiple channels
2020-04-11 10:06:39 +02:00
*
* @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
2020-04-11 10:11:22 +02:00
* @returns {array<GuildChannel> || false} an array of guild channels or false if none were resolved
2020-04-11 10:06:39 +02:00
* @memberof Resolver
*/
resolveChannels(resolveables = [], guild, strict = false, filter = () => true) {
2020-04-11 10:06:39 +02:00
if(typeof resolveables === 'string') resolveables = [ resolveables ];
if(resolveables.length === 0) return false;
const CM = guild.channels;
const resolved = [];
2020-04-11 10:06:39 +02:00
for(const resolveable of resolveables) {
2020-04-11 10:06:39 +02:00
const channel = CM.resolve(resolveable);
if(channel && filter(channel)) {
2020-04-11 10:06:39 +02:00
resolved.push(channel);
continue;
}
const name = /^#?([a-z0-9\-_0]*)/iu;
const id = /^<#([0-9]*)>/iu;
2020-04-11 10:06:39 +02:00
if (id.test(resolveable)) {
const match = resolveable.match(id);
const [, ch] = match;
const channel = CM.resolve(ch);
if (channel && filter(channel)) resolved.push(channel);
} else if (name.test(resolveable)) {
2020-04-11 10:06:39 +02:00
const match = resolveable.match(name);
const ch = match[1].toLowerCase();
const channel = CM.cache.filter(filter).filter((c) => {
if (!strict) return c.name.toLowerCase().includes(ch);
2020-04-11 10:06:39 +02:00
return c.name.toLowerCase() === ch;
})
.first();
2020-04-11 10:06:39 +02:00
if(channel) resolved.push(channel);
}
2020-04-11 10:06:39 +02:00
}
return resolved.length > 0 ? resolved : false;
}
2020-05-17 13:04:58 +02:00
resolveChannel(resolveable, guild, strict, filter) {
2020-05-17 13:04:58 +02:00
2020-05-22 22:13:47 +02:00
if (!resolveable) return false;
2020-05-24 23:39:37 +02:00
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, filter);
2020-05-17 13:04:58 +02:00
return result ? result[0] : false;
}
2020-04-11 10:06:39 +02:00
2020-04-11 10:11:22 +02:00
/**
* Resolve multiple roles
*
* @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
* @memberof Resolver
*/
async resolveRoles(resolveables = [], guild, strict = false) {
2020-04-11 10:06:39 +02:00
if(typeof resolveables === 'string') resolveables = [ resolveables ];
if(resolveables.length === 0) return false;
const { roles } = guild;
const resolved = [];
2020-04-11 10:06:39 +02:00
for(const resolveable of resolveables) {
2020-04-11 10:06:39 +02:00
const id = /^(<@&)?([0-9]{16,22})>?/iu;
2020-04-11 10:06:39 +02:00
if(id.test(resolveable)) {
const match = resolveable.match(id);
const [,, rId] = match;
2020-04-11 10:06:39 +02:00
const role = await roles.fetch(rId).catch(this.client.logger.error);
2020-04-11 10:06:39 +02:00
if(role) resolved.push(role);
} else {
const role = roles.cache.filter((r) => {
2020-04-11 10:06:39 +02:00
if(!strict) return r.name.toLowerCase().includes(resolveable.toLowerCase());
return r.name.toLowerCase() === resolveable.toLowerCase();
}).first();
2020-04-11 10:06:39 +02:00
if(role) resolved.push(role);
}
}
return resolved.length > 0 ? resolved : false;
2020-04-09 11:18:14 +02:00
2020-04-08 18:08:46 +02:00
}
2020-05-17 13:04:58 +02:00
async resolveRole(resolveable, guild, strict) {
2020-05-22 22:13:47 +02:00
if (!resolveable) return false;
2020-05-24 23:39:37 +02:00
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);
2020-05-17 13:04:58 +02:00
return result ? result[0] : false;
}
2020-04-08 18:08:46 +02:00
}
2020-04-09 16:30:52 +02:00
module.exports = Resolver;
const filterExact = (search) => (comp) => comp.id.toLowerCase() === search ||
2020-04-08 18:08:46 +02:00
comp.resolveable.toLowerCase() === search ||
comp.aliases && (comp.aliases.some((ali) => `${comp.type}:${ali}`.toLowerCase() === search) ||
comp.aliases.some((ali) => ali.toLowerCase() === search));
2020-04-08 18:08:46 +02:00
const filterInexact = (search) => (comp) => comp.id.toLowerCase().includes(search) ||
2020-04-08 18:08:46 +02:00
comp.resolveable.toLowerCase().includes(search) ||
comp.aliases && (comp.aliases.some((ali) => `${comp.type}:${ali}`.toLowerCase().includes(search)) ||
comp.aliases.some((ali) => ali.toLowerCase().includes(search)));