From 99d416f3d7cb3329771dcf805a82c964617c87c0 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 11 Apr 2020 11:06:39 +0300 Subject: [PATCH] resolvers --- structure/client/Resolver.js | 116 +++++++++++++++++++++++++++++++++- structure/extensions/Guild.js | 17 +++++ 2 files changed, 130 insertions(+), 3 deletions(-) diff --git a/structure/client/Resolver.js b/structure/client/Resolver.js index 929f9a1..0d3d0f3 100644 --- a/structure/client/Resolver.js +++ b/structure/client/Resolver.js @@ -57,6 +57,14 @@ class Resolver { } + /** + * Resolve several user resolveables + * + * @param {array} [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 + */ async resolveUsers(resolveables = [], strict = false) { if(typeof resolveables === 'string') resolveables = [ resolveables ]; @@ -100,11 +108,20 @@ class Resolver { } - return resolved; + return resolved.length > 0 ? resolved : false; } - async resolveMember(resolveables = [], strict = false, guild) { + /** + * Resolve multiple member resolveables + * + * @param {array} [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 || boolean} an array of resolved members or false if none were resolved + * @memberof Resolver + */ + async resolveMembers(resolveables = [], guild, strict = false) { if(typeof resolveables === 'string') resolveables = [ resolveables ]; if(resolveables.length === 0) return false; @@ -151,7 +168,100 @@ class Resolver { } - return resolved; + return resolved.length > 0 ? resolved : false; + + } + + /** + * + * + * @param {array} [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 + * @returns {array} an array of guild channels + * @memberof Resolver + */ + async resolveChannels(resolveables = [], guild, strict = false) { + + if(typeof resolveables === 'string') resolveables = [ resolveables ]; + if(resolveables.length === 0) return false; + let channels = guild.channels; + let resolved = []; + + for(let resolveable of resolveables) { + + let channel = channels.resolve(resolveable); + if(channel) { + resolved.push(channel); + continue; + } + + let name = /^\#?([a-z0-9\-\_0]*)/i; + let id = /^\<\#([0-9]*)\>/i; + + if(name.test(resolveable)) { + + let match = resolveable.match(name); + let ch = match[1].toLowerCase(); + + let channel = channels.cache.filter(c => { + if(!strict) return c.name.toLowerCase().includes(ch) + return c.name.toLowerCase() === ch; + }).first(1); + + if(channel) resolved.push(channel); + + } else if(id.test(resolveable)) { + + let match = resolveable.match(id); + let ch = match[1]; + + let channel = channels.resolve(ch); + + if(channel) resolved.push(channel); + + } + + } + + return resolved.length > 0 ? resolved : false; + + } + + async resolveRoles(resolveables = [], guild, strict) { + + if(typeof resolveables === 'string') resolveables = [ resolveables ]; + if(resolveables.length === 0) return false; + let roles = guild.roles; + let resolved = []; + + for(let resolveable of resolveables) { + + let id = /^(<@&)?([0-9]{16,22})>?/i; + + if(id.test(resolveable)) { + + let match = resolveable.match(id); + let r_id = match[2]; + + let role = await roles.fetch(r_id).catch(console.error); + + if(role) resolved.push(role); + + } else { + + let role = roles.cache.filter(r => { + if(!strict) return r.name.toLowerCase().includes(resolveable.toLowerCase()); + return r.name.toLowerCase() === resolveable.toLowerCase(); + }).first(1); + + if(role) resolved.push(role); + + } + + } + + return resolved.length > 0 ? reoslved : false; } diff --git a/structure/extensions/Guild.js b/structure/extensions/Guild.js index f146c78..4bbb1a2 100644 --- a/structure/extensions/Guild.js +++ b/structure/extensions/Guild.js @@ -12,6 +12,23 @@ const Guild = Structures.extend('Guild', (Guild) => { } + async resolveMembers(members, strict = false) { + + return await this.client.resolver.resolveMembers(members, this, strict); + } + + async resolveChannels(channels, strict = false) { + + return await this.client.resolver.resolveChannels(channels, this, strict); + + } + + async resolveRoles(roles, strict = false) { + + return await this.client.resolver.resolveRoles(roles, this, strict); + + } + } return ExtendedGuild;