aboutsummaryrefslogtreecommitdiff
path: root/src/lib/extensions/discord-akairo/BushCommandHandler.ts
blob: 44a096697f76cfbc594951daba13cc63751590d4 (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
import { Category, CommandHandler, CommandHandlerEvents, CommandHandlerOptions } from 'discord-akairo';
import { Collection, PermissionString } from 'discord.js';
import { BushConstants } from '../../utils/BushConstants';
import { BushMessage } from '../discord.js/BushMessage';
import { BushClient } from './BushClient';
import { BushCommand } from './BushCommand';
import { BushSlashMessage } from './BushSlashMessage';

export type BushCommandHandlerOptions = CommandHandlerOptions;

const commandHandlerEvents = BushConstants.CommandHandlerEvents;

export interface BushCommandHandlerEvents extends CommandHandlerEvents {
	commandBlocked: [message: BushMessage, command: BushCommand, reason: string];

	missingPermissions: [message: BushMessage, command: BushCommand, type: 'client' | 'user', missing: Array<PermissionString>];

	slashBlocked: [message: BushSlashMessage, command: BushCommand, reason: string];

	slashMissingPermissions: [
		message: BushSlashMessage,
		command: BushCommand,
		type: 'client' | 'user',
		missing: Array<PermissionString>
	];
}

export class BushCommandHandler extends CommandHandler {
	public declare client: BushClient;
	public declare modules: Collection<string, BushCommand>;
	public declare categories: Collection<string, Category<string, BushCommand>>;
	public constructor(client: BushClient, options: CommandHandlerOptions) {
		super(client, options);
	}

	public override async runPostTypeInhibitors(message: BushMessage, command: BushCommand, slash = false): Promise<boolean> {
		const reason = this.inhibitorHandler ? await this.inhibitorHandler.test('post', message, command) : null;
		if (reason != null) {
			this.emit(slash ? commandHandlerEvents.SLASH_BLOCKED : commandHandlerEvents.COMMAND_BLOCKED, message, command, reason);
			return true;
		}

		if (await this.runPermissionChecks(message, command)) {
			return true;
		}

		return !!this.runCooldowns(message, command);
	}
}