aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moderation/roleCommand.ts
blob: d5948c3df5a94c4f751d177cc5422fc0dd75ff34 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* eslint-disable @typescript-eslint/no-empty-function */
import { BushCommand } from '../../lib/extensions/BushCommand';
import AllowedMentions from '../../lib/utils/AllowedMentions';
import { Message, Role, GuildMember } from 'discord.js';
import { ApplicationCommandOptionType } from 'discord-api-types';

export default class RoleCommand extends BushCommand {
	private roleWhitelist: Record<string, string[]> = {
		'Partner': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'Suggester': [
			'*',
			'Admin Perms',
			'Sr. Moderator',
			'Moderator',
			'Helper',
			'Trial Helper',
			'Contributor'
		],
		'Level Locked': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'No Files': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'No Reactions': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'No Links': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'No Bots': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'No VC': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'No Giveaways': [
			'*',
			'Admin Perms',
			'Sr. Moderator',
			'Moderator',
			'Helper'
		],
		'No Support': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'Giveaway Donor': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'Giveaway (200m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'Giveaway (100m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'Giveaway (50m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'Giveaway (25m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'Giveaway (10m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'Giveaway (5m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator'],
		'Giveaway (1m)': ['*', 'Admin Perms', 'Sr. Moderator', 'Moderator']
	};
	constructor() {
		super('role', {
			aliases: ['role', 'addrole', 'removerole'],
			category: 'moderation',
			description: {
				content: "Manages users' roles.",
				usage: 'role <add|remove> <user> <role>',
				examples: ['role add tyman adminperms']
			},
			clientPermissions: ['MANAGE_ROLES', 'EMBED_LINKS', 'SEND_MESSAGES'],
			channel: 'guild',
			typing: true,
			args: [
				{
					id: 'user',
					type: 'member',
					prompt: {
						start: `What user do you want to add/remove the role on?`,
						retry: `<:error:837123021016924261> Choose a valid user to add/remove the role on.`
					}
				},
				{
					id: 'role',
					type: 'role',
					match: 'restContent',
					prompt: {
						start: `What role do you want to add/remove?`,
						retry: `<:error:837123021016924261> Choose a valid role to add/remove.`
					}
				}
			],
			slashCommandOptions: [
				{
					type: ApplicationCommandOptionType.USER,
					name: 'user',
					description: 'The user to add/remove the role on',
					required: true
				},
				{
					type: ApplicationCommandOptionType.ROLE,
					name: 'role',
					description: 'The role to add/remove',
					required: true
				}
			]
		});
	}

	public async exec(
		message: Message,
		{ user, role }: { user: GuildMember; role: Role }
	): Promise<unknown> {
		if (
			!message.member.permissions.has('MANAGE_ROLES') &&
			!this.client.ownerID.includes(message.author.id)
		) {
			const mappedRole = this.client.util.moulberryBushRoleMap.find(
				(m) => m.id === role.id
			);
			if (!mappedRole || !this.roleWhitelist[mappedRole.name]) {
				return message.util.reply(
					`<:error:837123021016924261> <@&${role.id}> is not whitelisted, and you do not have manage roles permission.`,
					{
						allowedMentions: AllowedMentions.none()
					}
				);
			}
			const allowedRoles = this.roleWhitelist[mappedRole.name].map((r) => {
				return this.client.util.moulberryBushRoleMap.find((m) => m.name === r)
					.id;
			});
			if (
				!message.member.roles.cache.some((role) =>
					allowedRoles.includes(role.id)
				)
			) {
				return message.util.reply(
					`<:error:837123021016924261> <@&${role.id}> is whitelisted, but you do not have any of the roles required to manage it.`,
					{
						allowedMentions: AllowedMentions.none()
					}
				);
			}
		}
		if (!this.client.ownerID.includes(message.author.id)) {
			if (role.comparePositionTo(message.member.roles.highest) >= 0) {
				return message.util.reply(
					`<:error:837123021016924261> <@&${role.id}> is higher or equal to your highest role.`,
					{
						allowedMentions: AllowedMentions.none()
					}
				);
			}
			if (role.comparePositionTo(message.guild.me.roles.highest) >= 0) {
				return message.util.reply(
					`<:error:837123021016924261> <@&${role.id}> is higher or equal to my highest role.`,
					{
						allowedMentions: AllowedMentions.none()
					}
				);
			}
			if (role.managed) {
				await message.util.reply(
					`<:error:837123021016924261> <@&${role.id}> is managed by an integration and cannot be managed.`,
					{
						allowedMentions: AllowedMentions.none()
					}
				);
			}
		}
		// No checks if the user has MANAGE_ROLES
		if (user.roles.cache.has(role.id)) {
			try {
				await user.roles.remove(role.id);
			} catch {
				return message.util.reply(
					`<:error:837123021016924261> Could not remove <@&${role.id}> from <@${user.id}>.`,
					{ allowedMentions: AllowedMentions.none() }
				);
			}
			return message.util.reply(
				`<:checkmark:837109864101707807> Successfully removed <@&${role.id}> from <@${user.id}>!`,
				{ allowedMentions: AllowedMentions.none() }
			);
		} else {
			try {
				await user.roles.add(role.id);
			} catch {
				return message.util.reply(
					`<:error:837123021016924261> Could not add <@&${role.id}> to <@${user.id}>.`,
					{ allowedMentions: AllowedMentions.none() }
				);
			}
			return message.util.reply(
				`<:checkmark:837109864101707807> Successfully added <@&${role.id}> to <@${user.id}>!`,
				{ allowedMentions: AllowedMentions.none() }
			);
		}
	}
}