galactic-bot/middleware/client/Resolver.js

134 lines
4.8 KiB
JavaScript
Raw Normal View History

2020-04-08 18:08:46 +02:00
class Resolver {
constructor(client) {
this.client = client;
}
components(str = '', type, exact = true) { //used for CommandHandler
const string = str.toLowerCase();
const components = this.client.registry.components
.filter(c => c.type === type)
.filter(exact ? filterExact(string) : filterInexact(string))
.array();
return components || [];
}
async resolveMemberAndUser(string, guild) {
const str = string.toLowerCase();
const index = guild ? guild.members : this.client.users;
let member = null;
if(/<@!?(\d{17,21})>/iy.test(str)) { //mentions
const matches = /<@!?(\d{17,21})>/iy.exec(str);
member = index.get(matches[1]);
if(!member) {
try {
member = await index.fetch(matches[1]);
} catch(e) {
try {
member = await this.client.users.fetch(matches[1]);
} catch(e) {} //eslint-disable-line no-empty
} //eslint-disable-line no-empty
}
} else if(/\d{17,21}/iy.test(str)) { //id
const matches = /(\d{17,21})/iy.exec(str);
member = index.get(matches[1]);
if(!member) {
try {
member = await index.fetch(matches[1]);
} catch(e) {
try {
member = await this.client.users.fetch(matches[1]);
} catch(e) {} //eslint-disable-line no-empty
} //eslint-disable-line no-empty
}
} else if(/(.{2,32})#(\d{4})/iy.test(str)) { //username#discrim
const matches = /(.{2,32})#(\d{4})/iy.exec(str);
member = guild
? guild.members.filter(m=>m.user.username === matches[1] && m.user.discriminator === matches[2]).first()
: this.client.users.filter(u=>u.username === matches[1] && u.discriminator === matches[2]).first();
}
return member || null;
}
async resolveUsers(resolveables = []) {
if(typeof resolveables === 'string') resolveables = [ resolveables ];
if(resolveables.length === 0) return false;
let users = this.client.users;
let resolved = [];
for(let resolveable of resolveables) {
if(/<@!?([0-9]{17,21})>/.test(resolveable)) {
let id = resolveable.match(/<@!?([0-9]{17,21})>/)[1];
let user = await users.fetch(id).catch(err => { if(err.code === 10013) return false; else { console.warn(err); return false; } });
if(user) resolved.push(user);
} else if(/(id\:)?([0-9]{17,21})/.test(resolveable)) {
let id = resolveable.match(/(id\:)?([0-9]{17,21})/)[2];
let user = await users.fetch(id).catch(err => { if(err.code === 10013) return false; else { console.warn(err); return false; } });
if(user) resolved.push(user);
} else if(/^\@?([\S\s]{1,32})\#([0-9]{4})/.test(resolveable)) {
let m = resolveable.match(/^\@?([\S\s]{1,32})\#([0-9]{4})/);
let username = m[1].toLowerCase();
let discrim = m[2].toLowerCase();
let user = users.cache.filter(u => {
return u.username.toLowerCase() === username && u.discriminator === discrim
}).first(1);
if(user) resolved.push(user);
}
}
return resolved;
}
async resolveMember(resolveables = [], strict = false, guild) {
if(typeof resolveables === 'string') resolveables = [ resolveables ];
if(resolveables.length === 0) return false;
let members = guild.members;
let resolved = [];
for(let resolveable of resolveables) {
if(/<@!?([0-9]{17,21})>/.test(resolveable)) {
let id = resolveable.match(/<@!?([0-9]{17,21})>/)[1];
let member = await members.fetch(id).catch(err => { if(err.code === 10007) return false; else { console.warn(err); return false; } });
if(member) resolved.push(member);
}
}
}
}
const filterExact = (search) => {
return comp => comp.id.toLowerCase() === search ||
comp.resolveable.toLowerCase() === search ||
(comp.aliases && (comp.aliases.some(ali => `${comp.type}:${ali}`.toLowerCase() === search) ||
comp.aliases.some(ali => ali.toLowerCase() === search)));
};
const filterInexact = (search) => {
return comp => comp.id.toLowerCase().includes(search) ||
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))));
};