aboutsummaryrefslogtreecommitdiff
path: root/src/commands/admin/channelPermissions.ts
blob: 08d00686f6793aab80fb80d2bff345506d13fd69 (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
import { Argument, Constants } from 'discord-akairo';
import { GuildChannel, GuildMember, MessageEmbed, Role } from 'discord.js';
import { BushCommand, BushMessage } from '../../lib';

export default class ChannelPermissionsCommand extends BushCommand {
	public constructor() {
		super('channelpermissions', {
			aliases: ['channelperms', 'cperms', 'cperm', 'chanperms', 'chanperm', 'channelpermissions'],
			category: 'admin',
			typing: true,
			description: {
				content: 'Use to mass change the channel ',
				usage: 'ChannelPerms <role_id> <perm> <state>',
				examples: ['ChannelPerms 783794633129197589 read_messages deny']
			},
			args: [
				{
					id: 'target',
					type: Argument.union(Constants.ArgumentTypes.ROLE, Constants.ArgumentTypes.MEMBER),
					match: Constants.ArgumentMatches.PHRASE,
					prompt: {
						start: 'What user/role would you like to change?',
						retry: 'Invalid response. What user/role would you like to change?'
					}
				},
				{
					id: 'permission',
					type: 'permission',
					match: Constants.ArgumentMatches.PHRASE,
					prompt: {
						start: 'What permission would you like to change?',
						retry: '{error} Choose a valid permission.'
					}
				},
				{
					id: 'state',
					type: [
						['true', '1', 'yes', 'enable', 'allow'],
						['false', '0', 'no', 'disable', 'disallow', 'deny'],
						['neutral', 'remove', 'none']
					],
					match: Constants.ArgumentMatches.PHRASE,
					prompt: {
						start: 'What should that permission be set to?',
						retry: '{error} Set the state to either `enable`, `disable`, or `remove`.'
					}
				}
			],
			ratelimit: 4,
			cooldown: 4000,
			clientPermissions: ['MANAGE_CHANNELS', 'SEND_MESSAGES'],
			userPermissions: ['ADMINISTRATOR'],
			channel: 'guild'
		});
	}

	public async exec(
		message: BushMessage,
		{
			target,
			permission,
			state
		}: {
			target: Role | GuildMember;
			permission: string;
			state: 'true' | 'false' | 'neutral';
		}
	): Promise<unknown> {
		const failedChannels = [];
		for (const channel of message.guild.channels.cache.array()) {
			try {
				if (channel.isThread()) return;
				if (channel.permissionsLocked) return;
				const permissionState = state === 'true' ? true : state === 'false' ? false : null;
				await channel.permissionOverwrites.create(
					target.id,
					{ [permission]: permissionState },
					{ reason: 'Changing overwrites for mass channel channel perms command' }
				);
			} catch (e) {
				client.console.debug(e.stack);
				failedChannels.push(channel);
			}
		}
		const failure = failedChannels.map((e: GuildChannel) => `<#${e.id}>`).join(' ');
		if (failure.length > 2000) {
			const paginate: MessageEmbed[] = [];
			for (let i = 0; i < failure.length; i += 2000) {
				paginate.push(new MessageEmbed().setDescription(failure.substring(i, Math.min(failure.length, i + 2000))));
			}
			const normalMessage = `Finished changing perms! Failed channels:`;
			return await client.util.buttonPaginate(message, paginate, normalMessage);
		} else {
			return await message.util.reply({
				content: `Finished changing perms! Failed channels:`,
				embeds: [{ description: failure }]
			});
		}
	}
}