aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/highlight-matches.ts
blob: aa8308c01d295ed82e5820bc1bfe57d722257201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { BushCommand, ButtonPaginator, type ArgType, type BushMessage, type BushSlashMessage } from '#lib';
import assert from 'assert';
import { ArgumentGeneratorReturn } from 'discord-akairo';
import { APIEmbed } from 'discord-api-types/v9';
import { highlightCommandArgs, highlightSubcommands } from './highlight-!.js';

export default class HighlightMatchesCommand extends BushCommand {
	public constructor() {
		super('highlight-matches', {
			aliases: [],
			category: 'utilities',
			description: highlightSubcommands.matches,
			usage: [],
			examples: [],
			clientPermissions: [],
			userPermissions: []
		});
	}

	public override *args(): ArgumentGeneratorReturn {
		const phrase: ArgType<'string'> = yield {
			type: 'string',
			match: 'rest',
			prompt: {
				start: highlightCommandArgs.matches[0].description,
				retry: highlightCommandArgs.matches[0].retry,
				optional: !highlightCommandArgs.matches[0].required
			}
		};

		return { phrase };
	}

	public override async exec(message: BushMessage | BushSlashMessage, args: { phrase: ArgType<'string'> }) {
		assert(message.inGuild());

		const res = await client.highlightManager.checkPhrase(message.guild.id, message.author.id, args.phrase);

		if (!res.size) return await message.util.reply(`${util.emojis.error} You are not highlighting any words`);

		const lines = res.map(
			(passed, hl) => `${passed ? util.emojis.check : util.emojis.cross} ${hl.regex ? `/${hl.word}/gi` : hl.word}`
		);
		const chunked = util.chunk(lines, 10);

		const pages = chunked.map(
			(chunk): APIEmbed => ({
				title: `Matches`,
				description: chunk.join('\n'),
				color: util.colors.default
			})
		);

		return pages.length > 1 ? await ButtonPaginator.send(message, pages) : message.util.send({ embeds: pages });
	}
}