From 3c6ecb8e760ca8af28914432257f80cf677e1398 Mon Sep 17 00:00:00 2001 From: Null Date: Wed, 15 Apr 2020 11:19:46 -0500 Subject: [PATCH] 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()));