/[sudobot]/branches/4.x/src/commands/moderation/UnlockallCommand.ts
ViewVC logotype

Annotation of /branches/4.x/src/commands/moderation/UnlockallCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months, 1 week ago) by rakinar2
File MIME type: application/typescript
File size: 5400 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     import { Collection, CommandInteraction, Message, Permissions, Role, TextChannel } from 'discord.js';
21     import BaseCommand from '../../utils/structures/BaseCommand';
22     import CommandOptions from '../../types/CommandOptions';
23     import InteractionOptions from '../../types/InteractionOptions';
24     import DiscordClient from '../../client/Client';
25     import MessageEmbed from '../../client/MessageEmbed';
26    
27     export async function unlockAll(client: DiscordClient, role: Role, channels: TextChannel[], force: boolean, reason?: string) {
28     try {
29     return await client.channelLock.unlockAll(channels, {
30     force,
31     role,
32     reason,
33     sendConfirmation: true
34     });
35     }
36     catch (e) {
37     console.log(e);
38     }
39     }
40    
41     export default class UnlockallCommand extends BaseCommand {
42     supportsInteractions: boolean = true;
43     permissions = [Permissions.FLAGS.MANAGE_CHANNELS];
44    
45     constructor() {
46     super('unlockall', 'moderation', []);
47     }
48    
49     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
50     if (!options.isInteraction && typeof options.args[0] === 'undefined') {
51     await msg.reply({
52     embeds: [
53     new MessageEmbed()
54     .setColor('#f14a60')
55     .setDescription(`This command requires at least one argument.`)
56     ]
57     });
58    
59     return;
60     }
61    
62     if (msg instanceof CommandInteraction) {
63     await msg.deferReply({ ephemeral: true });
64     }
65    
66     const raid = options.isInteraction ? options.options.getBoolean('raid') === true : (options.options.indexOf('--raid') !== -1);
67    
68     let role: Role = <Role> msg.guild!.roles.everyone;
69     let unlockall: string[] = [], unlockallChannels: TextChannel[] = [];
70     const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
71     let reason: string | undefined;
72    
73     if (options.isInteraction) {
74     if (options.options.getString('channels'))
75     unlockall = options.options.getString('channels')!.split(' ').filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
76    
77     if (options.options.getRole('role')) {
78     role = await <Role> options.options.getRole('role');
79     }
80    
81     if (options.options.getString('reason')) {
82     reason = await <string> options.options.getString('reason');
83     }
84     }
85     else {
86     if ((msg as Message).mentions.roles.first()) {
87     role = await <Role> (msg as Message).mentions.roles.first();
88     }
89    
90     if (!role) {
91     await msg.reply({
92     embeds: [
93     new MessageEmbed()
94     .setColor('#f14a60')
95     .setDescription(`Invalid role given.`)
96     ],
97     ephemeral: true
98     });
99    
100     return;
101     }
102    
103     if (!raid) {
104     unlockall = options.normalArgs.filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
105     console.log("Unlock", unlockall);
106     }
107     else {
108     if (msg.guild!.channels.cache.size < 2) {
109     await msg.guild?.channels.fetch();
110     }
111    
112     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;
113     }
114     }
115    
116     if (msg.guild!.channels.cache.size < 2) {
117     await msg.guild?.channels.fetch();
118     }
119    
120     if (unlockall.length === 0 && !raid) {
121     await this.deferReply(msg, {
122     content: "No channel specified!"
123     });
124    
125     return;
126     }
127    
128     for await (const c of unlockall) {
129     console.log(c);
130     const id = c.startsWith('<#') ? c.substring(2, c.length - 1) : c;
131    
132     if (unlockallChannels.find(c => c.id === id))
133     continue;
134    
135     const channel = msg.guild?.channels.cache.get(id);
136    
137     if (!channel) {
138     continue;
139     }
140    
141     if (!channel.isText()) {
142     unlockallChannels = [...unlockallChannels, ...(msg.guild?.channels.cache.filter(c => c.parent?.id === id) as Collection<string, TextChannel>).values()!];
143     continue;
144     }
145    
146     unlockallChannels.push(channel as TextChannel);
147     }
148    
149     const [success, failure] = (await unlockAll(client, role, unlockallChannels, force, reason))!;
150    
151     if (options.isInteraction) {
152     await this.deferReply(msg, {
153     content: "Unlocked " + unlockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful unlocks and ${failure} failed unlocks.` : '')
154     });
155     }
156     else {
157     await (msg as Message).react('🔓');
158     }
159     }
160     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26