-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSyncCommand.ts
More file actions
62 lines (51 loc) · 2.03 KB
/
Copy pathSyncCommand.ts
File metadata and controls
62 lines (51 loc) · 2.03 KB
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
import { PermissionFlagsBits } from 'discord.js';
import { ExtendedClient } from '@structures/Client.js';
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
import { Embeds } from '@utils/Embeds.js';
import { Constants } from '@utils/Constants.js';
export default class SyncCommand extends Command {
constructor() {
super({
name: 'sync',
description: "Cleanup each member's roles",
requiredPermissions: [PermissionFlagsBits.Administrator],
checkFlags: CommandCheckFlags.Guild,
});
}
public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
const guild = context.guild!;
const members = await guild.members.fetch();
const communityRole = guild.roles.cache.get(Constants.ROLES.COMMUNITY);
const ignoredRoles = new Set(
[
guild.id, // @everyone role
guild.roles.premiumSubscriberRole?.id,
guild.roles.cache.get(Constants.ROLES.UPDATES)?.id,
guild.roles.cache.get(Constants.ROLES.QOTD_PING)?.id,
guild.roles.cache.get(Constants.ROLES.SUPPORT)?.id,
].filter((id) => id != null)
);
await context.reply({
embeds: [Embeds.info(`Starting sync for ${members.size} members...`)],
});
for (const member of members.values()) {
const realRoles = member.roles.cache.filter(
(r) => !ignoredRoles.has(r.id) && r.id !== guild.id
);
const highestRealRole = realRoles.size
? realRoles.reduce((a, b) => (b.position > a.position ? b : a))
: null;
const chosenRole = highestRealRole ?? communityRole;
const rolesToRemove = realRoles.filter((r) => r.id !== chosenRole?.id);
if (rolesToRemove.size > 0) {
await member.roles.remove(rolesToRemove).catch(() => {});
}
if (chosenRole && !member.roles.cache.has(chosenRole.id)) {
await member.roles.add(chosenRole).catch(() => {});
}
}
await context.editReply({
embeds: [Embeds.success(`Finished sync for all ${members.size} members!`)],
});
}
}