From f26bee8c7ac321ee56a03a86a929881bbe70f896 Mon Sep 17 00:00:00 2001 From: Null Date: Wed, 15 Apr 2020 10:42:08 -0500 Subject: [PATCH 1/5] 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 { From 35ab708dfc386a8aae23fb5d3d1d1282be337101 Mon Sep 17 00:00:00 2001 From: Null Date: Wed, 15 Apr 2020 10:57:37 -0500 Subject: [PATCH 2/5] Add ability to resolve roles --- src/class/Util.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/class/Util.ts b/src/class/Util.ts index 4883278..b54062e 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -1,5 +1,5 @@ import signale from 'signale'; -import { Member, Message, Guild, PrivateChannel, GroupChannel } from 'eris'; +import {Member, Message, Guild, PrivateChannel, GroupChannel, Role} from 'eris'; import { Client, Command, RichEmbed } from '.'; import { statusMessages as emotes } from '../configs/emotes.json'; @@ -47,6 +47,18 @@ export default class Util { } } + public resolveRole(query: string, { roles }: Guild): Role | undefined { + let queries = query.split(' ').slice(0, 10).join(' '); + 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(' '); + 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 { 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 From 104cd698625345dc417c004e1b9e5862829a6564 Mon Sep 17 00:00:00 2001 From: Null Date: Wed, 15 Apr 2020 10:59:22 -0500 Subject: [PATCH 3/5] Fix member resolver --- src/class/Util.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/class/Util.ts b/src/class/Util.ts index b54062e..674255b 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -64,8 +64,8 @@ export default class Util { 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) { + if (queries.split(' ').length > 1 && !member) { + while (!member && queries.split(' ').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 From 2206a1105076d182b695a7ed380c2acb8c1dac2f Mon Sep 17 00:00:00 2001 From: Null Date: Wed, 15 Apr 2020 11:02:13 -0500 Subject: [PATCH 4/5] Ignore eslint loops warning/error --- src/class/Util.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/class/Util.ts b/src/class/Util.ts index 674255b..a64eb89 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -53,6 +53,7 @@ export default class Util { 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())); } } @@ -67,6 +68,7 @@ export default class Util { 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 || 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()))); From 3c6ecb8e760ca8af28914432257f80cf677e1398 Mon Sep 17 00:00:00 2001 From: Null Date: Wed, 15 Apr 2020 11:19:46 -0500 Subject: [PATCH 5/5] Add channel resolver --- src/class/Util.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/class/Util.ts b/src/class/Util.ts index a64eb89..a53eb30 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -1,5 +1,5 @@ import signale from 'signale'; -import {Member, Message, Guild, PrivateChannel, GroupChannel, Role} from 'eris'; +import { Member, Message, Guild, PrivateChannel, GroupChannel, Role, AnyGuildChannel } from 'eris'; import { Client, Command, RichEmbed } from '.'; import { statusMessages as emotes } from '../configs/emotes.json'; @@ -47,6 +47,20 @@ export default class Util { } } + 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); + 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 { let queries = query.split(' ').slice(0, 10).join(' '); let role = roles.find((r) => r.id === queries || r.name === queries || r.name.toLowerCase() === queries.toLowerCase() || r.name.toLowerCase().startsWith(queries.toLowerCase()));