/[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 107 by rakin, Mon Jul 29 17:28:37 2024 UTC revision 244 by rakin, Mon Jul 29 17:29:11 2024 UTC
# Line 5  import InteractionOptions from '../../ty Line 5  import InteractionOptions from '../../ty
5  import DiscordClient from '../../client/Client';  import DiscordClient from '../../client/Client';
6  import MessageEmbed from '../../client/MessageEmbed';  import MessageEmbed from '../../client/MessageEmbed';
7    
8  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) {
9      if (role) {      try {
10          channels.forEach(async channel => {          return await client.channelLock.unlockAll(channels, {
11              try {              force,
12                  client.db.get('SELECT * FROM locks WHERE channel_id = ?', [channel.id], async (err: any, data: any) => {              role,
13                      if (data || force) {              reason,
14                          await channel.send({              sendConfirmation: true
                             embeds: [  
                                 new MessageEmbed()  
                                 .setDescription(':closed_lock_with_key: This channel has been unlocked.')  
                             ]  
                         });  
                           
                         let perm;  
                         const data1 = data?.perms;  
   
                         if (data1) {  
                             if (data1 === 'DENY') {  
                                 await (perm = false);  
                             }  
                             else if (data1 === 'NULL') {  
                                 await (perm = null);  
                             }  
                             else if (data1 === 'ALLOW') {  
                                 await (perm = true);  
                             }  
                         }  
                           
                         if (force) {  
                             await (perm = true);  
                         }  
   
                         await console.log(channel.name);  
   
                         try {  
                             await channel.permissionOverwrites.edit(role, {  
                                 SEND_MESSAGES: perm,  
                             });  
   
                         }  
                         catch (e) {  
                             console.log(e);  
                         }  
   
                         await console.log(perm);  
   
                         if (data) {  
                             await client.db.get('DELETE FROM locks WHERE id = ?', [data?.id], async (err: any) => {});  
                         }  
                     }  
                 });  
             }  
             catch(e) {  
                 console.log(e);  
             }  
15          });          });
16      }      }
17        catch (e) {
18            console.log(e);        
19        }
20  }  }
21    
22  export default class UnlockallCommand extends BaseCommand {  export default class UnlockallCommand extends BaseCommand {
# Line 84  export default class UnlockallCommand ex Line 39  export default class UnlockallCommand ex
39              return;              return;
40          }          }
41    
42            if (msg instanceof CommandInteraction) {
43                await msg.deferReply({ ephemeral: true });
44            }
45    
46          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);
47    
48          let role: Role = <Role> msg.guild!.roles.everyone;          let role: Role = <Role> msg.guild!.roles.everyone;
49          let unlockall: string[] = [], unlockallChannels: Collection<string, TextChannel> = new Collection();          let unlockall: string[] = [], unlockallChannels: TextChannel[] = [];
50          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);
51            let reason: string | undefined;
52    
53          if (options.isInteraction) {          if (options.isInteraction) {
54              if (options.options.getChannel('role')) {              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                  role = await <Role> options.options.getRole('role');                  role = await <Role> options.options.getRole('role');
59              }              }
60    
61                if (options.options.getString('reason')) {
62                    reason = await <string> options.options.getString('reason');
63                }
64          }          }
65          else {          else {
66              if ((msg as Message).mentions.roles.first()) {              if ((msg as Message).mentions.roles.first()) {
67                  role = await <Role> (msg as Message).mentions.roles.first();                  role = await <Role> (msg as Message).mentions.roles.first();
68              }              }
             else if (options.options.includes('-r') && options.normalArgs[options.options.indexOf('-r') + 1]) {  
                 role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[options.options.indexOf('-r') + 1]);  
             }  
69    
70              if (!role) {              if (!role) {
71                  await msg.reply({                  await msg.reply({
# Line 117  export default class UnlockallCommand ex Line 81  export default class UnlockallCommand ex
81              }              }
82    
83              if (!raid) {              if (!raid) {
84                  for (const a of options.args) {                  unlockall = options.normalArgs.filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
85                      if (/^\d+$/g.test(a)) {                  console.log("Unlock", unlockall);
86                          unlockall.push(a);              }
87                      }              else {
88                  }                  if (msg.guild!.channels.cache.size < 2) {
89                            await msg.guild?.channels.fetch();
                 if ((msg as Message).mentions.channels.first()) {  
                     (msg as Message).mentions.channels.forEach(c => {  
                         if (c instanceof TextChannel)  
                             unlockallChannels.set(c.id, c);  
                     });  
90                  }                  }
91    
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              }              }
94          }          }
95    
96          let channels = raid ? await msg.guild!.channels.cache.filter(c => (          if (msg.guild!.channels.cache.size < 2) {
97              (raid && (              await msg.guild?.channels.fetch();
                 (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)) ||  
                 (!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))  
             ))) && c.type === 'GUILD_TEXT'  
         ) : null;  
   
         if (channels === null && !raid) {  
             channels = msg.guild!.channels.cache.filter(c2 => (unlockall.includes(c2.id) || unlockall.includes(c2.parent?.id!)) && c2.type === 'GUILD_TEXT')!;  
             channels = channels.merge(unlockallChannels, c => ({ keep: true, value: c }), c => ({ keep: true, value: c }), (c1, c2) => ({ keep: true, value: c2 }));  
98          }          }
99    
100          await unlockAll(client, role, msg, options, channels as Collection <string, TextChannel>, force);          if (unlockall.length === 0 && !raid) {
101                await this.deferReply(msg, {
102                    content: "No channel specified!"
103                });
104    
105                return;
106            }
107    
108            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    
117                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          if (options.isInteraction) {          if (options.isInteraction) {
132              await msg.reply({              await this.deferReply(msg, {
133                  content: "The channels are unlocked.",                  content: "Unlocked " + unlockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful unlocks and ${failure} failed unlocks.` : '')
134                  ephemeral: true              });
             });  
135          }          }
136          else {          else {
137              await (msg as Message).react('🔓');              await (msg as Message).react('🔓');

Legend:
Removed from v.107  
changed lines
  Added in v.244

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26