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

Annotation of /branches/3.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 ago) by rakinar2
File MIME type: application/typescript
File size: 5332 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, 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    
44     constructor() {
45     super('unlockall', 'moderation', []);
46     }
47    
48     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
49     if (!options.isInteraction && typeof options.args[0] === 'undefined') {
50     await msg.reply({
51     embeds: [
52     new MessageEmbed()
53     .setColor('#f14a60')
54     .setDescription(`This command requires at least one argument.`)
55     ]
56     });
57    
58     return;
59     }
60    
61     if (msg instanceof CommandInteraction) {
62     await msg.deferReply({ ephemeral: true });
63     }
64    
65     const raid = options.isInteraction ? options.options.getBoolean('raid') === true : (options.options.indexOf('--raid') !== -1);
66    
67     let role: Role = <Role> msg.guild!.roles.everyone;
68     let unlockall: string[] = [], unlockallChannels: TextChannel[] = [];
69     const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
70     let reason: string | undefined;
71    
72     if (options.isInteraction) {
73     if (options.options.getString('channels'))
74     unlockall = options.options.getString('channels')!.split(' ').filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
75    
76     if (options.options.getRole('role')) {
77     role = await <Role> options.options.getRole('role');
78     }
79    
80     if (options.options.getString('reason')) {
81     reason = await <string> options.options.getString('reason');
82     }
83     }
84     else {
85     if ((msg as Message).mentions.roles.first()) {
86     role = await <Role> (msg as Message).mentions.roles.first();
87     }
88    
89     if (!role) {
90     await msg.reply({
91     embeds: [
92     new MessageEmbed()
93     .setColor('#f14a60')
94     .setDescription(`Invalid role given.`)
95     ],
96     ephemeral: true
97     });
98    
99     return;
100     }
101    
102     if (!raid) {
103     unlockall = options.normalArgs.filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
104     console.log("Unlock", unlockall);
105     }
106     else {
107     if (msg.guild!.channels.cache.size < 2) {
108     await msg.guild?.channels.fetch();
109     }
110    
111     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;
112     }
113     }
114    
115     if (msg.guild!.channels.cache.size < 2) {
116     await msg.guild?.channels.fetch();
117     }
118    
119     if (unlockall.length === 0 && !raid) {
120     await this.deferReply(msg, {
121     content: "No channel specified!"
122     });
123    
124     return;
125     }
126    
127     for await (const c of unlockall) {
128     console.log(c);
129     const id = c.startsWith('<#') ? c.substring(2, c.length - 1) : c;
130    
131     if (unlockallChannels.find(c => c.id === id))
132     continue;
133    
134     const channel = msg.guild?.channels.cache.get(id);
135    
136     if (!channel) {
137     continue;
138     }
139    
140     if (!channel.isText()) {
141     unlockallChannels = [...unlockallChannels, ...(msg.guild?.channels.cache.filter(c => c.parent?.id === id) as Collection<string, TextChannel>).values()!];
142     continue;
143     }
144    
145     unlockallChannels.push(channel as TextChannel);
146     }
147    
148     const [success, failure] = (await unlockAll(client, role, unlockallChannels, force, reason))!;
149    
150     if (options.isInteraction) {
151     await this.deferReply(msg, {
152     content: "Unlocked " + unlockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful unlocks and ${failure} failed unlocks.` : '')
153     });
154     }
155     else {
156     await (msg as Message).react('🔓');
157     }
158     }
159     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26