From f26bee8c7ac321ee56a03a86a929881bbe70f896 Mon Sep 17 00:00:00 2001 From: Null Date: Wed, 15 Apr 2020 10:42:08 -0500 Subject: [PATCH] Improve resolver further Use startsWith over includes for better accuracy, allow iterations on the query to avoid accidental user input screwing up resolver --- src/class/Util.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/class/Util.ts b/src/class/Util.ts index 0493cff..4883278 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -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 {