From 35ab708dfc386a8aae23fb5d3d1d1282be337101 Mon Sep 17 00:00:00 2001 From: Null Date: Wed, 15 Apr 2020 10:57:37 -0500 Subject: [PATCH] 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