aboutsummaryrefslogtreecommitdiff
path: root/src/lib/extensions/discord-akairo/BushCommandHandler.ts
blob: 09baf2ecc908fb00e7246ed2727ad733f68ac060 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Category, CommandHandler, CommandHandlerOptions } from 'discord-akairo';
import { Collection } from 'discord.js';
import { BushConstants } from '../../utils/BushConstants';
import { BushMessage } from '../discord.js/BushMessage';
import { BushClient } from './BushClient';
import { BushCommand } from './BushCommand';

export type BushCommandHandlerOptions = CommandHandlerOptions;

const CommandHandlerEvents = BushConstants.CommandHandlerEvents;
const BlockedReasons = BushConstants.BlockedReasons;

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 async runPostTypeInhibitors(message: BushMessage, command: BushCommand, slash = false): Promise<boolean> {
		if (command.ownerOnly) {
			const isOwner = this.client.isOwner(message.author);
			if (!isOwner) {
				this.emit(
					slash ? CommandHandlerEvents.SLASH_BLOCKED : CommandHandlerEvents.COMMAND_BLOCKED,
					message,
					command,
					BlockedReasons.OWNER
				);
				return true;
			}
		}

		if (command.superUserOnly) {
			const isSuperUser = this.client.isSuperUser(message.author);
			if (!isSuperUser) {
				this.emit(
					slash ? CommandHandlerEvents.SLASH_BLOCKED : CommandHandlerEvents.COMMAND_BLOCKED,
					message,
					command,
					BlockedReasons.OWNER
				);
				return true;
			}
		}

		if (command.channel === 'guild' && !message.guild) {
			this.emit(
				slash ? CommandHandlerEvents.SLASH_BLOCKED : CommandHandlerEvents.COMMAND_BLOCKED,
				message,
				command,
				BlockedReasons.GUILD
			);
			return true;
		}

		if (command.channel === 'dm' && message.guild) {
			this.emit(
				slash ? CommandHandlerEvents.SLASH_BLOCKED : CommandHandlerEvents.COMMAND_BLOCKED,
				message,
				command,
				BlockedReasons.DM
			);
			return true;
		}
		if (command.restrictedChannels?.length && message.channel) {
			if (!command.restrictedChannels.includes(message.channel.id)) {
				this.emit(CommandHandlerEvents.COMMAND_BLOCKED, message, command, BlockedReasons.RESTRICTED_CHANNEL);
				return true;
			}
		}
		if (command.restrictedGuilds?.length && message.guild) {
			if (!command.restrictedGuilds.includes(message.guild.id)) {
				this.emit(CommandHandlerEvents.COMMAND_BLOCKED, message, command, BlockedReasons.RESTRICTED_GUILD);
				return true;
			}
		}
		if (await this.runPermissionChecks(message, command)) {
			return true;
		}
		const reason = this.inhibitorHandler ? await this.inhibitorHandler.test('post', message, command) : null;
		if (reason != null) {
			this.emit(CommandHandlerEvents.COMMAND_BLOCKED, message, command, reason);
			return true;
		}
		return !!this.runCooldowns(message, command);
	}
}