aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/highlight-unblock.ts
blob: d70fb2861844ec05b169daa084b41c4982f0bc18 (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
57
58
59
60
61
62
63
64
65
66
import { AllowedMentions, BushCommand, emojis, type ArgType, type CommandMessage, type SlashMessage } from '#lib';
import assert from 'assert';
import { Argument, ArgumentGeneratorReturn } from 'discord-akairo';
import { Channel, GuildMember, User } from 'discord.js';
import { UnblockResult } from '../../lib/common/HighlightManager.js';
import { highlightSubcommands } from './highlight-!.js';

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

	public override *args(): ArgumentGeneratorReturn {
		const target: ArgType<'member' | 'textBasedChannel'> = yield {
			type: Argument.union('member', 'textBasedChannel'),
			match: 'rest',
			prompt: {
				start: highlightSubcommands.unblock.options[0].start,
				retry: highlightSubcommands.unblock.options[0].options[0].retry,
				optional: !highlightSubcommands.unblock.options[0].options[0].retry
			}
		};

		return { target };
	}

	public override async exec(message: CommandMessage | SlashMessage, args: { target: ArgType<'member' | 'textBasedChannel'> }) {
		assert(message.inGuild());

		if (args.target instanceof User && message.util.isSlashMessage(message))
			args.target = message.interaction.options.getMember('target')!;

		if (!args.target) return message.util.reply(`${emojis.error} Could not resolve member.`);

		if (!(args.target instanceof GuildMember || args.target instanceof Channel))
			return await message.util.reply(`${emojis.error} You can only unblock users or channels.`);

		if (args.target instanceof Channel && !args.target.isTextBased())
			return await message.util.reply(`${emojis.error} You can only unblock text-based channels.`);

		const res = await this.client.highlightManager.removeBlock(message.guildId, message.author.id, args.target);

		/* eslint-disable @typescript-eslint/no-base-to-string */
		const content = (() => {
			switch (res) {
				case UnblockResult.NOT_BLOCKED:
					return `${emojis.error} ${args.target} is not blocked so cannot be unblock.`;
				case UnblockResult.ERROR:
					return `${emojis.error} An error occurred while unblocking ${args.target}.`;
				case UnblockResult.SUCCESS:
					return `${emojis.success} Successfully allowed ${args.target} to trigger your highlights.`;
			}
		})();
		/* eslint-enable @typescript-eslint/no-base-to-string */

		return await message.util.reply({ content, allowedMentions: AllowedMentions.none() });
	}
}