aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/lockdown.ts
blob: 12247634505bb29366cff9524b6b8c674e7d3316 (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
151
152
153
154
155
156
157
158
159
160
161
import {
	AllowedMentions,
	BushCommand,
	clientSendAndPermCheck,
	colors,
	ConfirmationPrompt,
	emojis,
	format,
	type ArgType,
	type CommandMessage,
	type OptArgType,
	type SlashMessage
} from '#lib';
import assert from 'assert';
import {
	ApplicationCommandOptionType,
	Collection,
	Constants,
	NewsChannel,
	PermissionFlagsBits,
	TextChannel,
	ThreadChannel,
	VoiceChannel
} 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: 'textBasedChannel',
					prompt: 'What channel would you like to lockdown?',
					slashType: ApplicationCommandOptionType.Channel,
					channelTypes: Constants.TextBasedChannelTypes,
					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) => clientSendAndPermCheck(m, [PermissionFlagsBits.ManageChannels]),
			userPermissions: [PermissionFlagsBits.ManageChannels],
			lock: 'channel'
		});
	}

	public override async exec(
		message: CommandMessage | SlashMessage,
		args: {
			channel: OptArgType<'textBasedChannel'>;
			reason: OptArgType<'string'>;
			all: ArgType<'flag'>;
		}
	) {
		return await LockdownCommand.lockdownOrUnlockdown(message, args, 'lockdown');
	}

	public static async lockdownOrUnlockdown(
		message: CommandMessage | SlashMessage,
		args: {
			channel: OptArgType<'textBasedChannel'>;
			reason: OptArgType<'string'>;
			all: ArgType<'flag'>;
		},
		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(`${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 TextChannel ||
				channel instanceof NewsChannel ||
				channel instanceof ThreadChannel ||
				channel instanceof VoiceChannel
			)
		)
			return await message.util.reply(
				`${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(`${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: `${emojis.error} The following channels failed to ${action}:`,
				embeds: [
					{
						description: response.map((e, c) => `<#${c}> : ${e.message}`).join('\n'),
						color: colors.warn
					}
				]
			});
		} else {
			let messageResponse;
			if (response === 'all not chosen and no channel specified') {
				messageResponse = `${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 = `${emojis.error} Some of the channels configured to be ${actionFormatted} cannot be resolved: ${channels}}`;
			} else if (response === 'no channels configured') {
				messageResponse = `${emojis.error} The all option is selected but there are no channels configured to be locked down.`;
			} else if (response === 'moderator not found') {
				messageResponse = `${emojis.error} For some reason I could not resolve you?`;
			} else if (response.startsWith('success: ')) {
				const num = Number.parseInt(response.replace('success: ', ''));
				messageResponse = `${emojis.success} Successfully ${
					action === 'lockdown' ? 'locked down' : 'unlocked'
				} **${num}** channel${num > 0 ? 's' : ''}.`;
			} else {
				return `${emojis.error} An error occurred: ${format.input(response)}}`;
			}

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