Improve resolver further

Use startsWith over includes for better accuracy, allow iterations on the query to avoid accidental user input screwing up resolver
merge-requests/4/head
Null 2020-04-15 10:42:08 -05:00
parent 4a94e56842
commit f26bee8c7a
No known key found for this signature in database
GPG Key ID: CFD79C1EC586E7BD
1 changed files with 13 additions and 3 deletions

View File

@ -48,9 +48,19 @@ export default class Util {
}
public resolveMember(query: string, { members }: Guild): Member | undefined {
return members.find((m) => m.mention.replace('!', '') === query.replace('!', '') || `${m.username}#${m.discriminator}` === query || m.username === query || m.id === query || m.nick === query) // 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().includes(query.toLowerCase()) || (m.nick && m.nick.toLowerCase().includes(query.toLowerCase())));
let queries = query.split(' ').slice(0, 10).join(' ');
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}` === 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(queries.toLowerCase()) || (m.nick && m.nick.toLowerCase().startsWith(queries.toLowerCase())));
if (queries.length > 1 && !member) {
while (!member && queries.length > 0) {
queries = queries.split(' ').slice(0, queries.length - 1).join(' ');
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}` === 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(queries.toLowerCase()) || (m.nick && m.nick.toLowerCase().startsWith(queries.toLowerCase())));
}
}
return member;
}
public async handleError(error: Error, message?: Message, command?: Command): Promise<void> {