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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 51 by rakin, Mon Jul 29 17:28:23 2024 UTC revision 393 by rakin, Mon Jul 29 17:29:59 2024 UTC
# Line 1  Line 1 
1  import { Collection, CommandInteraction, GuildBasedChannel, GuildChannel, Message, Permissions, Role, TextChannel } from 'discord.js';  /**
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';  import BaseCommand from '../../utils/structures/BaseCommand';
22  import CommandOptions from '../../types/CommandOptions';  import CommandOptions from '../../types/CommandOptions';
23  import InteractionOptions from '../../types/InteractionOptions';  import InteractionOptions from '../../types/InteractionOptions';
24  import DiscordClient from '../../client/Client';  import DiscordClient from '../../client/Client';
25  import MessageEmbed from '../../client/MessageEmbed';  import MessageEmbed from '../../client/MessageEmbed';
26    
27  export async function unlockAll(client: DiscordClient, role: Role, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions, channels: Collection <string, TextChannel>, force: boolean) {  export async function unlockAll(client: DiscordClient, role: Role, channels: TextChannel[], force: boolean, reason?: string) {
28      if (role) {      try {
29          const gen = await msg.guild!.roles.fetch(client.config.props[msg.guild!.id].gen_role);          return await client.channelLock.unlockAll(channels, {
30                force,
31          channels.forEach(async channel => {              role,
32              try {              reason,
33                  await channel.send({              sendConfirmation: true
                     embeds: [  
                         new MessageEmbed()  
                         .setDescription(':closed_lock_with_key: This channel has been unlocked.')  
                     ]  
                 });  
   
                 client.db.get('SELECT * FROM locks WHERE channel_id = ?', [channel.id], async (err: any, data: any) => {  
                     if (data || force) {  
                         let perm1;  
                         let perm;  
                         const data1 = data?.perms?.split(',');  
   
                         if (data1) {  
                             if (data1[0] === 'DENY') {  
                                 await (perm = false);  
                             }  
                             else if (data1[0] === 'NULL') {  
                                 await (perm = null);  
                             }  
                             else if (data1[0] === 'ALLOW') {  
                                 await (perm = true);  
                             }  
   
                             if (data1[1] === 'DENY') {  
                                 await (perm1 = false);  
                             }  
                             else if (data1[1] === 'NULL') {  
                                 await (perm1 = null);  
                             }  
                             else if (data1[1] === 'ALLOW') {  
                                 await (perm1 = true);  
                             }  
                         }  
                           
                         if (force) {  
                             await (perm1 = true);  
                             await (perm = true);  
                         }  
   
                         await console.log(channel.name);  
   
                         try {  
                             await channel.permissionOverwrites.edit(role, {  
                                 SEND_MESSAGES: perm,  
                             });  
   
                             await channel.permissionOverwrites.edit(gen!, {  
                                 SEND_MESSAGES: perm1,  
                             });  
                         }  
                         catch (e) {  
                             console.log(e);  
                         }  
   
                         await console.log(perm, perm1);  
   
                         if (data) {  
                             await client.db.get('DELETE FROM locks WHERE id = ?', [data?.id], async (err: any) => {});  
                         }  
                     }  
                 });  
             }  
             catch(e) {  
                 console.log(e);  
             }  
34          });          });
35      }      }
36        catch (e) {
37            console.log(e);        
38        }
39  }  }
40    
41  export default class UnlockallCommand extends BaseCommand {  export default class UnlockallCommand extends BaseCommand {
# Line 89  export default class UnlockallCommand ex Line 46  export default class UnlockallCommand ex
46      }      }
47    
48      async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {      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);          const raid = options.isInteraction ? options.options.getBoolean('raid') === true : (options.options.indexOf('--raid') !== -1);
66    
67          let role: Role = <Role> msg.guild!.roles.everyone;          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);          const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
70            let reason: string | undefined;
71    
72          if (options.isInteraction) {          if (options.isInteraction) {
73              if (options.options.getChannel('role')) {              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');                  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 {          else {
85              if ((msg as Message).mentions.roles.first()) {              if ((msg as Message).mentions.roles.first()) {
86                  role = await <Role> (msg as Message).mentions.roles.first();                  role = await <Role> (msg as Message).mentions.roles.first();
87              }              }
             else if (options.normalArgs[0] && options.normalArgs[0] !== 'everyone') {  
                 role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[0]);  
             }  
88    
89              if (!role) {              if (!role) {
90                  await msg.reply({                  await msg.reply({
# Line 119  export default class UnlockallCommand ex Line 98  export default class UnlockallCommand ex
98            
99                  return;                  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          const channels: Collection <string, GuildBasedChannel> = await msg.guild!.channels.cache.filter(c => (          if (msg.guild!.channels.cache.size < 2) {
116              (!raid && (client.config.props[msg.guild!.id].lockall.indexOf(c.id) !== -1 || client.config.props[msg.guild!.id].lockall.indexOf(c.parent?.id) !== -1)) ||              await msg.guild?.channels.fetch();
117              (raid && (          }
118                  (client.config.props[msg.guild!.id].raid.exclude && (client.config.props[msg.guild!.id].raid.channels.indexOf(c.id) === -1 && client.config.props[msg.guild!.id].raid.channels.indexOf(c.parent?.id) === -1)) ||  
119                  (!client.config.props[msg.guild!.id].raid.exclude && (client.config.props[msg.guild!.id].raid.channels.indexOf(c.id) !== -1 || client.config.props[msg.guild!.id].raid.channels.indexOf(c.parent?.id) !== -1))          if (unlockall.length === 0 && !raid) {
120              ))) && c.type === 'GUILD_TEXT'              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          await unlockAll(client, role, msg, options, channels as Collection <string, TextChannel>, force);          const [success, failure] = (await unlockAll(client, role, unlockallChannels, force, reason))!;
149    
150          if (options.isInteraction) {          if (options.isInteraction) {
151              await msg.reply({              await this.deferReply(msg, {
152                  content: "The channels are unlocked.",                  content: "Unlocked " + unlockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful unlocks and ${failure} failed unlocks.` : '')
153                  ephemeral: true              });
             });  
154          }          }
155          else {          else {
156              await (msg as Message).react('🔓');              await (msg as Message).react('🔓');

Legend:
Removed from v.51  
changed lines
  Added in v.393

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26