aboutsummaryrefslogtreecommitdiff
path: root/src/commands/admin/roleAll.ts
blob: dda536f76af840580c8a5d06510147c1f47b6c31 (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
import {
	AllowedMentions,
	BushCommand,
	clientSendAndPermCheck,
	emojis,
	type ArgType,
	type CommandMessage,
	type SlashMessage
} from '#lib';
import assert from 'assert/strict';
import { ApplicationCommandOptionType, PermissionFlagsBits, type GuildMember } from 'discord.js';

export default class RoleAllCommand extends BushCommand {
	public constructor() {
		super('roleAll', {
			aliases: ['role-all', 'rall'],
			category: 'admin',
			description: 'Give a role to every member on the server.',
			usage: ['role-all <role> [--bots]'],
			examples: ['role-all 783794633129197589 --bots'],
			args: [
				{
					id: 'role',
					description: 'The role to assigned to every member on the server.',
					type: 'role',
					prompt: 'What role would you like to give to every member on the server?',
					retry: '{error} Pick a valid role.',
					slashType: ApplicationCommandOptionType.Role
				},
				{
					id: 'bots',
					description: 'Also give the role to bots.',
					match: 'flag',
					prompt: 'Would you like to also give roles to bots?',
					flag: '--bots',
					slashType: ApplicationCommandOptionType.Boolean,
					optional: true
				}
			],
			channel: 'guild',
			clientPermissions: (m) => clientSendAndPermCheck(m, [PermissionFlagsBits.ManageRoles]),
			userPermissions: [PermissionFlagsBits.Administrator],
			typing: true,
			slash: true,
			lock: 'guild'
		});
	}

	public override async exec(message: CommandMessage | SlashMessage, args: { role: ArgType<'role'>; bots: ArgType<'flag'> }) {
		assert(message.inGuild());
		if (!message.member!.permissions.has(PermissionFlagsBits.Administrator) && !message.member!.user.isOwner())
			return await message.util.reply(`${emojis.error} You must have admin perms to use this command.`);
		if (message.util.isSlashMessage(message)) await message.interaction.deferReply();

		if (args.role.comparePositionTo(message.guild.members.me!.roles.highest) >= 0 && !args.role) {
			return await message.util.reply(`${emojis.error} I cannot assign a role higher or equal to my highest role.`);
		}

		let members = await message.guild.members.fetch();

		members = members.filter((member: GuildMember) => {
			try {
				if (member.user.bot && !args.bots) return false;
				if (member.roles.cache.has(args.role.id)) return false;
			} catch {
				return false;
			}
			return true;
		});

		await message.util.reply(`${emojis.loading} adding roles to ${members.size} members`);

		const promises = members.map((member: GuildMember) => {
			return member.roles.add(args.role, `RoleAll Command - triggered by ${message.author.tag} (${message.author.id})`);
		});

		const failed = (await Promise.allSettled(promises)).filter((val) => val.status === 'rejected');

		if (!failed.length) {
			await message.util.sendNew({
				content: `${emojis.success} Finished adding <@&${args.role.id}> to **${members.size}** member${
					members.size > 1 ? 's' : ''
				}.`,
				allowedMentions: AllowedMentions.none()
			});
		} else {
			const array = [...members.values()];
			await message.util.sendNew({
				content: `${emojis.warn} Finished adding <@&${args.role.id}> to **${members.size - failed.length}** member${
					members.size - failed.length > 1 ? 's' : ''
				}! Failed members:\n${failed.map((_, index) => `<@${array[index].id}>`).join(' ')}`,
				allowedMentions: AllowedMentions.none()
			});
		}
	}
}