diff --git a/src/class/Util.ts b/src/class/Util.ts index 0493cff..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 } 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,10 +47,48 @@ 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())); + 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 { - 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.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()))); + } + } + return member; } public async handleError(error: Error, message?: Message, command?: Command): Promise {