resolvers

This commit is contained in:
Erik 2020-04-11 11:06:39 +03:00
parent 8116a5a004
commit 99d416f3d7
2 changed files with 130 additions and 3 deletions

View File

@ -57,6 +57,14 @@ class Resolver {
} }
/**
* 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
*/
async resolveUsers(resolveables = [], strict = false) { async resolveUsers(resolveables = [], strict = false) {
if(typeof resolveables === 'string') resolveables = [ resolveables ]; 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<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) {
if(typeof resolveables === 'string') resolveables = [ resolveables ]; if(typeof resolveables === 'string') resolveables = [ resolveables ];
if(resolveables.length === 0) return false; if(resolveables.length === 0) return false;
@ -151,7 +168,100 @@ class Resolver {
} }
return resolved; return resolved.length > 0 ? resolved : false;
}
/**
*
*
* @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
* @returns {array<GuildChannel>} 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;
} }

View File

@ -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; return ExtendedGuild;