/[sudobot]/trunk/src/commands/moderation/UnlockallCommand.ts
ViewVC logotype

Annotation of /trunk/src/commands/moderation/UnlockallCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 244 - (hide annotations)
Mon Jul 29 17:29:11 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 4654 byte(s)
refactor: improve channel locking systems
1 rakin 51 import { Collection, CommandInteraction, GuildBasedChannel, GuildChannel, Message, Permissions, Role, TextChannel } from 'discord.js';
2     import BaseCommand from '../../utils/structures/BaseCommand';
3     import CommandOptions from '../../types/CommandOptions';
4     import InteractionOptions from '../../types/InteractionOptions';
5     import DiscordClient from '../../client/Client';
6     import MessageEmbed from '../../client/MessageEmbed';
7    
8 rakin 244 export async function unlockAll(client: DiscordClient, role: Role, channels: TextChannel[], force: boolean, reason?: string) {
9     try {
10     return await client.channelLock.unlockAll(channels, {
11     force,
12     role,
13     reason,
14     sendConfirmation: true
15 rakin 51 });
16     }
17 rakin 244 catch (e) {
18     console.log(e);
19     }
20 rakin 51 }
21    
22     export default class UnlockallCommand extends BaseCommand {
23     supportsInteractions: boolean = true;
24    
25     constructor() {
26     super('unlockall', 'moderation', []);
27     }
28    
29     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
30 rakin 103 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
31     await msg.reply({
32     embeds: [
33     new MessageEmbed()
34     .setColor('#f14a60')
35     .setDescription(`This command requires at least one argument.`)
36     ]
37     });
38    
39     return;
40     }
41    
42 rakin 244 if (msg instanceof CommandInteraction) {
43     await msg.deferReply({ ephemeral: true });
44     }
45    
46 rakin 51 const raid = options.isInteraction ? options.options.getBoolean('raid') === true : (options.options.indexOf('--raid') !== -1);
47    
48     let role: Role = <Role> msg.guild!.roles.everyone;
49 rakin 244 let unlockall: string[] = [], unlockallChannels: TextChannel[] = [];
50 rakin 51 const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
51 rakin 244 let reason: string | undefined;
52 rakin 51
53     if (options.isInteraction) {
54 rakin 244 if (options.options.getString('channels'))
55     unlockall = options.options.getString('channels')!.split(' ').filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
56    
57     if (options.options.getRole('role')) {
58 rakin 51 role = await <Role> options.options.getRole('role');
59     }
60 rakin 244
61     if (options.options.getString('reason')) {
62     reason = await <string> options.options.getString('reason');
63     }
64 rakin 51 }
65     else {
66     if ((msg as Message).mentions.roles.first()) {
67 rakin 103 role = await <Role> (msg as Message).mentions.roles.first();
68     }
69 rakin 51
70     if (!role) {
71     await msg.reply({
72     embeds: [
73     new MessageEmbed()
74     .setColor('#f14a60')
75     .setDescription(`Invalid role given.`)
76     ],
77     ephemeral: true
78     });
79    
80     return;
81     }
82 rakin 103
83 rakin 107 if (!raid) {
84 rakin 244 unlockall = options.normalArgs.filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
85     console.log("Unlock", unlockall);
86     }
87     else {
88     if (msg.guild!.channels.cache.size < 2) {
89     await msg.guild?.channels.fetch();
90 rakin 103 }
91 rakin 244
92     unlockall = client.config.get('raid').exclude ? msg.guild?.channels.cache.filter(c => !client.config.get('raid').channels.includes(c.id) && !client.config.get('raid').channels.includes(c.parent?.id)) : client.config.get('raid').channels;
93 rakin 103 }
94 rakin 51 }
95    
96 rakin 244 if (msg.guild!.channels.cache.size < 2) {
97     await msg.guild?.channels.fetch();
98     }
99 rakin 51
100 rakin 244 if (unlockall.length === 0 && !raid) {
101     await this.deferReply(msg, {
102     content: "No channel specified!"
103     });
104    
105     return;
106 rakin 103 }
107    
108 rakin 244 for await (const c of unlockall) {
109     console.log(c);
110     const id = c.startsWith('<#') ? c.substring(2, c.length - 1) : c;
111    
112     if (unlockallChannels.find(c => c.id === id))
113     continue;
114    
115     const channel = msg.guild?.channels.cache.get(id);
116 rakin 51
117 rakin 244 if (!channel) {
118     continue;
119     }
120    
121     if (!channel.isText()) {
122     unlockallChannels = [...unlockallChannels, ...(msg.guild?.channels.cache.filter(c => c.parent?.id === id) as Collection<string, TextChannel>).values()!];
123     continue;
124     }
125    
126     unlockallChannels.push(channel as TextChannel);
127     }
128    
129     const [success, failure] = (await unlockAll(client, role, unlockallChannels, force, reason))!;
130    
131 rakin 51 if (options.isInteraction) {
132 rakin 244 await this.deferReply(msg, {
133     content: "Unlocked " + unlockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful unlocks and ${failure} failed unlocks.` : '')
134     });
135 rakin 51 }
136     else {
137     await (msg as Message).react('🔓');
138     }
139     }
140     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26