aboutsummaryrefslogtreecommitdiff
path: root/src/inhibitors/checks/cannotSend.ts
blob: e9eab9845614749a772849d6c093f4d3906b4e7e (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
import { BotCommand, BotInhibitor, InhibitorReason, InhibitorType, type SlashMessage } from '#lib';
import { type Message } from 'discord.js';

export default class CannotSendInhibitor extends BotInhibitor {
	public constructor() {
		super(InhibitorReason.CannotSend, {
			reason: InhibitorReason.CannotSend,
			type: InhibitorType.Post,
			priority: 1000
		});
	}

	public async exec(message: Message | SlashMessage, command: BotCommand): Promise<boolean> {
		// let it error if it is the owner
		if (this.client.isOwner(message.author)) return false;
		if (!message.inGuild() || !message.channel) return false;
		if (command.skipSendCheck) return false;

		if (!message.guild.members.me) throw new Error(`Client member not cached in ${message.guild.name} (${message.guild.id})`);

		// doesn't apply to slash commands
		if (message.util?.isSlash) return false;

		const sendPerm = message.channel.isThread() ? 'SendMessages' : 'SendMessagesInThreads';

		const perms = message.channel.permissionsFor(message.guild.members.me);

		if (perms == null) {
			// todo: remove once forum channels are fixed
			if (message.channel.isThread() && message.channel.parent == null) {
				return false;
			} else {
				return true;
			}
		}

		return !perms.has(sendPerm);
	}
}