aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/lockdown.ts
blob: 36f32407af35e48e7d0729af26cd3c0e666d9737 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {
	AllowedMentions,
	BushCommand,
	BushNewsChannel,
	BushTextChannel,
	BushThreadChannel,
	ConfirmationPrompt,
	type ArgType,
	type BushMessage,
	type BushSlashMessage,
	type OptionalArgType
} from '#lib';
import assert from 'assert';
import { ApplicationCommandOptionType, ChannelType, Collection, PermissionFlagsBits } from 'discord.js';

export default class LockdownCommand extends BushCommand {
	public constructor() {
		super('lockdown', {
			aliases: ['lockdown', 'lock'],
			category: 'moderation',
			description: 'Allows you to lockdown a channel or all configured channels.',
			usage: ['lockdown [channel] [reason] [--all]'],
			examples: ['lockdown', 'lockdown --all'],
			args: [
				{
					id: 'channel',
					description: 'Specify a different channel to lockdown instead of the one you trigger the command in.',
					type: util.arg.union('textChannel', 'newsChannel', 'threadChannel'),
					prompt: 'What channel would you like to lockdown?',
					slashType: ApplicationCommandOptionType.Channel,
					channelTypes: [
						ChannelType.GuildText,
						ChannelType.GuildNews,
						ChannelType.GuildNewsThread,
						ChannelType.GuildPublicThread,
						ChannelType.GuildPrivateThread
					],
					optional: true
				},
				{
					id: 'reason',
					description: 'The reason for the lockdown.',
					type: 'string',
					match: 'rest',
					prompt: 'What is the reason for the lockdown?',
					slashType: ApplicationCommandOptionType.String,
					optional: true
				},
				{
					id: 'all',
					description: 'Whether or not to lock all configured channels.',
					match: 'flag',
					flag: '--all',
					prompt: 'Would you like to lockdown all configured channels?',
					slashType: ApplicationCommandOptionType.Boolean,
					optional: true
				}
			],
			slash: true,
			channel: 'guild',
			clientPermissions: (m) => util.clientSendAndPermCheck(m, [PermissionFlagsBits.ManageChannels]),
			userPermissions: [PermissionFlagsBits.ManageChannels],
			lock: 'channel'
		});
	}

	public override async exec(
		message: BushMessage | BushSlashMessage,
		args: {
			channel: OptionalArgType<'textChannel'> | OptionalArgType<'newsChannel'> | OptionalArgType<'threadChannel'>;
			reason: OptionalArgType<'string'>;
			all: ArgType<'boolean'>;
		}
	) {
		return await LockdownCommand.lockdownOrUnlockdown(message, args, 'lockdown');
	}

	public static async lockdownOrUnlockdown(
		message: BushMessage | BushSlashMessage,
		args: {
			channel: OptionalArgType<'textChannel'> | OptionalArgType<'newsChannel'> | OptionalArgType<'threadChannel'>;
			reason: OptionalArgType<'string'>;
			all: ArgType<'boolean'>;
		},
		action: 'lockdown' | 'unlockdown'
	) {
		assert(message.inGuild());
		if (message.util.isSlashMessage(message)) await message.interaction.deferReply();

		if (args.channel && args.all)
			return await message.util.reply(`${util.emojis.error} You can't specify a channel and set all to true at the same time.`);

		const channel = args.channel ?? message.channel;

		if (!(channel instanceof BushTextChannel || channel instanceof BushNewsChannel || channel instanceof BushThreadChannel))
			return await message.util.reply(
				`${util.emojis.error} You can only ${action} text channels, news channels, and thread channels.`
			);

		if (args.all) {
			const confirmation = await ConfirmationPrompt.send(message, {
				content: `Are you sure you want to ${action} all channels?`
			});
			if (!confirmation) return message.util.sendNew(`${util.emojis.error} Lockdown cancelled.`);
		}

		const response = await message.guild.lockdown({
			moderator: message.author,
			channel: channel ?? undefined,
			reason: args.reason ?? undefined,
			all: args.all ?? false,
			unlock: action === 'unlockdown'
		});

		if (response instanceof Collection) {
			return await message.util.sendNew({
				content: `${util.emojis.error} The following channels failed to ${action}:`,
				embeds: [
					{
						description: response.map((e, c) => `<#${c}> : ${e.message}`).join('\n'),
						color: util.colors.warn
					}
				]
			});
		} else {
			let messageResponse;
			if (response === 'all not chosen and no channel specified') {
				messageResponse = `${util.emojis.error} You must specify a channel to ${action}.`;
			} else if (response.startsWith('invalid channel configured: ')) {
				const channels = response.replace('invalid channel configured: ', '');
				const actionFormatted = `${action.replace('down', '')}ed`;
				messageResponse = `${util.emojis.error} Some of the channels configured to be ${actionFormatted} cannot be resolved: ${channels}}`;
			} else if (response === 'no channels configured') {
				messageResponse = `${util.emojis.error} The all option is selected but there are no channels configured to be locked down.`;
			} else if (response === 'moderator not found') {
				messageResponse = `${util.emojis.error} For some reason I could not resolve you?`;
			} else if (response.startsWith('success: ')) {
				const num = Number.parseInt(response.replace('success: ', ''));
				messageResponse = `${util.emojis.success} Successfully ${
					action === 'lockdown' ? 'locked down' : 'unlocked'
				} **${num}** channel${num > 0 ? 's' : ''}.`;
			} else {
				return `${util.emojis.error} An error occurred: ${util.format.input(response)}}`;
			}

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