Add channel resolver

merge-requests/4/head
Null 2020-04-15 11:19:46 -05:00
parent 2206a11050
commit 3c6ecb8e76
No known key found for this signature in database
GPG Key ID: CFD79C1EC586E7BD
1 changed files with 15 additions and 1 deletions

View File

@ -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()));