Revert to stricter resolver

merge-requests/5/head
Null 2020-04-15 16:00:32 -05:00
parent 81fea6cdd0
commit 11f51b2508
No known key found for this signature in database
GPG Key ID: CFD79C1EC586E7BD
1 changed files with 6 additions and 37 deletions

View File

@ -51,49 +51,18 @@ export default class Util {
} }
public resolveGuildChannel(query: string, { channels }: Guild): AnyGuildChannel | undefined { public resolveGuildChannel(query: string, { channels }: Guild): AnyGuildChannel | undefined {
let queries = query.split(' ').slice(0, 10).join(' '); const nchannels = channels.map(c => c).sort((a: AnyGuildChannel, b: AnyGuildChannel) => a.type - b.type);
const nchannels = channels.map((c) => c).sort((a: AnyGuildChannel, b: AnyGuildChannel) => a.type - b.type); return nchannels.find((c) => (c.id === query || c.name === query || c.name.toLowerCase() === query.toLowerCase() || c.name.toLowerCase().startsWith(query.toLowerCase())));
let channel = nchannels.find((c) => (c.id === queries || c.name === queries || c.name.toLowerCase() === queries.toLowerCase() || c.name.toLowerCase().startsWith(queries.toLowerCase())));
if (!channel && queries.split(' ').length > 0) {
while (!channel && queries.split(' ').length > 1) {
queries = queries.split(' ').slice(0, queries.split(' ').length - 1).join(' ');
// eslint-disable-next-line no-loop-func
channel = nchannels.find((c) => c.id === queries || c.name === queries || c.name.toLowerCase() === queries.toLowerCase() || c.name.toLowerCase().startsWith(queries.toLowerCase()));
}
}
return channel;
} }
public resolveRole(query: string, { roles }: Guild): Role | undefined { public resolveRole(query: string, { roles }: Guild): Role | undefined {
let queries = query.split(' ').slice(0, 10).join(' '); return roles.find((r) => r.id === query || r.name === query || r.name.toLowerCase() === query.toLowerCase() || r.name.toLowerCase().startsWith(query.toLowerCase()));
let role = roles.find((r) => r.id === queries || r.name === queries || r.name.toLowerCase() === queries.toLowerCase() || r.name.toLowerCase().startsWith(queries.toLowerCase()));
if (!role && queries.split(' ').length > 0) {
while (!role && queries.split(' ').length > 1) {
queries = queries.split(' ').slice(0, queries.split(' ').length - 1).join(' ');
// eslint-disable-next-line no-loop-func
role = roles.find((r) => r.id === queries || r.name === queries || r.name.toLowerCase() === queries.toLowerCase() || r.name.toLowerCase().startsWith(queries.toLowerCase()));
}
}
return role;
} }
public resolveMember(query: string, { members }: Guild): Member | undefined { public resolveMember(query: string, { members }: Guild): Member | undefined {
let queries = query.split(' ').slice(0, 10).join(' '); return members.find((m) => m.mention.replace('!', '') === query.replace('!', '') || `${m.username}#${m.discriminator}` === query || m.username === queries || m.id === query || m.nick === query) // Exact match for mention, username+discrim, username and user ID
let member = members.find((m) => m.mention.replace('!', '') === queries.replace('!', '') || `${m.username}#${m.discriminator}` === query || m.username === queries || m.id === queries || m.nick === queries) // Exact match for mention, username+discrim, username and user ID || members.find((m) => `${m.username.toLowerCase()}#${m.discriminator}` === query.toLowerCase() || m.username.toLowerCase() === query.toLowerCase() || (m.nick && m.nick.toLowerCase() === query.toLowerCase())) // Case insensitive match for username+discrim, username
|| members.find((m) => `${m.username.toLowerCase()}#${m.discriminator}` === queries.toLowerCase() || m.username.toLowerCase() === queries.toLowerCase() || (m.nick && m.nick.toLowerCase() === queries.toLowerCase())) // Case insensitive match for username+discrim, username || members.find((m) => m.username.toLowerCase().startsWith(query.toLowerCase()) || (m.nick && m.nick.toLowerCase().startsWith(query.toLowerCase())));
|| members.find((m) => m.username.toLowerCase().startsWith(queries.toLowerCase()) || (m.nick && m.nick.toLowerCase().startsWith(queries.toLowerCase())));
if (queries.split(' ').length > 1 && !member) {
while (!member && queries.split(' ').length > 0) {
queries = queries.split(' ').slice(0, queries.length - 1).join(' ');
// eslint-disable-next-line no-loop-func
member = members.find((m) => m.mention.replace('!', '') === queries.replace('!', '') || `${m.username}#${m.discriminator}` === query || m.username === queries || m.id === queries || m.nick === queries) // Exact match for mention, username+discrim, username and user ID
// eslint-disable-next-line no-loop-func
|| members.find((m) => `${m.username.toLowerCase()}#${m.discriminator}` === queries.toLowerCase() || m.username.toLowerCase() === queries.toLowerCase() || (m.nick && m.nick.toLowerCase() === queries.toLowerCase())) // Case insensitive match for username+discrim, username
// eslint-disable-next-line no-loop-func
|| members.find((m) => m.username.toLowerCase().startsWith(queries.toLowerCase()) || (m.nick && m.nick.toLowerCase().startsWith(queries.toLowerCase())));
}
}
return member;
} }
public async handleError(error: Error, message?: Message, command?: Command, disable?: boolean): Promise<void> { public async handleError(error: Error, message?: Message, command?: Command, disable?: boolean): Promise<void> {