/[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 393 - (hide annotations)
Mon Jul 29 17:29:59 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 5332 byte(s)
style: add license comments (#77)

* style: add license commits

* fix: shebang errors
1 rakin 393 /**
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 rakin 344 import { Collection, CommandInteraction, Message, Role, TextChannel } from 'discord.js';
21 rakin 51 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 rakin 244 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 rakin 51 });
35     }
36 rakin 244 catch (e) {
37     console.log(e);
38     }
39 rakin 51 }
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 rakin 103 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 rakin 244 if (msg instanceof CommandInteraction) {
62     await msg.deferReply({ ephemeral: true });
63     }
64    
65 rakin 51 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 rakin 244 let unlockall: string[] = [], unlockallChannels: TextChannel[] = [];
69 rakin 51 const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
70 rakin 244 let reason: string | undefined;
71 rakin 51
72     if (options.isInteraction) {
73 rakin 244 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 rakin 51 role = await <Role> options.options.getRole('role');
78     }
79 rakin 244
80     if (options.options.getString('reason')) {
81     reason = await <string> options.options.getString('reason');
82     }
83 rakin 51 }
84     else {
85     if ((msg as Message).mentions.roles.first()) {
86 rakin 103 role = await <Role> (msg as Message).mentions.roles.first();
87     }
88 rakin 51
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 rakin 103
102 rakin 107 if (!raid) {
103 rakin 244 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 rakin 103 }
110 rakin 244
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 rakin 103 }
113 rakin 51 }
114    
115 rakin 244 if (msg.guild!.channels.cache.size < 2) {
116     await msg.guild?.channels.fetch();
117     }
118 rakin 51
119 rakin 244 if (unlockall.length === 0 && !raid) {
120     await this.deferReply(msg, {
121     content: "No channel specified!"
122     });
123    
124     return;
125 rakin 103 }
126    
127 rakin 244 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 rakin 51
136 rakin 244 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 rakin 51 if (options.isInteraction) {
151 rakin 244 await this.deferReply(msg, {
152     content: "Unlocked " + unlockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful unlocks and ${failure} failed unlocks.` : '')
153     });
154 rakin 51 }
155     else {
156     await (msg as Message).react('🔓');
157     }
158     }
159     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26