aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/slowmode.ts
blob: 641f88e0798e56144e2b3df24ce996d255e9ff52 (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
import {
	Arg,
	BushCommand,
	clientSendAndPermCheck,
	emojis,
	format,
	humanizeDuration,
	userGuildPermCheck,
	type CommandMessage,
	type OptArgType,
	type SlashMessage
} from '#lib';
import assert from 'assert';
import { Argument } from 'discord-akairo';
import { ApplicationCommandOptionType, ChannelType, PermissionFlagsBits } from 'discord.js';

export default class SlowmodeCommand extends BushCommand {
	public constructor() {
		super('slowmode', {
			aliases: ['slowmode', 'slow'],
			category: 'moderation',
			description: 'A command to set the slowmode of a channel.',
			usage: ['slowmode <length> [channel]'],
			examples: ['slowmode 3'],
			args: [
				{
					id: 'length',
					description: 'The amount of time to set the slowmode of a channel to.',
					type: Argument.union('duration', 'durationSeconds', 'off', 'none', 'disable'),
					readableType: "duration|durationSeconds|'off'|'none'|'disable'",
					prompt: 'What would you like to set the slowmode to?',
					retry: '{error} Please set the slowmode to a valid length.',
					optional: true,
					slashType: ApplicationCommandOptionType.Integer
				},
				{
					id: 'channel',
					description: 'The channel to change the slowmode of, defaults to the current channel.',
					type: 'channel',
					prompt: 'What channel would you like to change?',
					retry: '{error} Choose a valid channel.',
					optional: true,
					slashType: ApplicationCommandOptionType.Channel,
					channelTypes: [ChannelType.GuildText, ChannelType.GuildPrivateThread, ChannelType.GuildPublicThread]
				}
			],
			slash: true,
			channel: 'guild',
			clientPermissions: (m) =>
				clientSendAndPermCheck(m, [PermissionFlagsBits.ManageChannels, PermissionFlagsBits.EmbedLinks], true),
			userPermissions: (m) => userGuildPermCheck(m, [PermissionFlagsBits.ManageMessages])
		});
	}

	public override async exec(
		message: CommandMessage | SlashMessage,
		args: {
			length: OptArgType<'duration' | 'durationSeconds'> | 'off' | 'none' | 'disable';
			channel: OptArgType<'channel'>;
		}
	) {
		assert(message.inGuild());

		args.channel ??= message.channel!;

		if (
			args.channel.type !== ChannelType.GuildText &&
			args.channel.type !== ChannelType.GuildNews &&
			args.channel.type !== ChannelType.GuildVoice &&
			!args.channel.isThread()
		)
			return await message.util.reply(`${emojis.error} <#${args.channel.id}> is not a text or thread channel.`);

		args.length =
			typeof args.length === 'string' && !(['off', 'none', 'disable'] as const).includes(args.length)
				? await Arg.cast('duration', message, args.length)
				: args.length;

		const length2: number = (['off', 'none', 'disable'] as const).includes(args.length) || args.length === null ? 0 : args.length;

		const setSlowmode = await args.channel
			.setRateLimitPerUser(length2 / 1000, `Changed by ${message.author.tag} (${message.author.id}).`)
			.catch(() => {});
		if (!setSlowmode)
			return await message.util.reply(`${emojis.error} There was an error changing the slowmode of <#${args.channel.id}>.`);
		else
			return await message.util.reply(
				`${emojis.success} Successfully changed the slowmode of <#${args.channel.id}> ${
					length2 ? `to ${format.input(humanizeDuration(length2))}` : '**off**'
				}.`
			);
	}
}