/[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 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          const gen = await msg.guild!.roles.fetch(client.config.props[msg.guild!.id].gen_role);          return await client.channelLock.unlockAll(channels, {
11                force,
12          channels.forEach(async channel => {              role,
13              try {              reason,
14                  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);  
             }  
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 89  export default class UnlockallCommand ex Line 27  export default class UnlockallCommand ex
27      }      }
28    
29      async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {      async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
30            if (!options.isInteraction && typeof options.args[0] === 'undefined') {
31                await msg.reply({
32                    embeds: [
33                        new MessageEmbed()
34                        .setColor('#f14a60')
35                        .setDescription(`This command requires at least one argument.`)
36                    ]
37                });
38    
39                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: 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.normalArgs[0] && options.normalArgs[0] !== 'everyone') {  
                 role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[0]);  
             }  
69    
70              if (!role) {              if (!role) {
71                  await msg.reply({                  await msg.reply({
# Line 119  export default class UnlockallCommand ex Line 79  export default class UnlockallCommand ex
79            
80                  return;                  return;
81              }              }
82    
83                if (!raid) {
84                    unlockall = options.normalArgs.filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
85                    console.log("Unlock", unlockall);
86                }
87                else {
88                    if (msg.guild!.channels.cache.size < 2) {
89                        await msg.guild?.channels.fetch();
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          const channels: Collection <string, GuildBasedChannel> = await msg.guild!.channels.cache.filter(c => (          if (msg.guild!.channels.cache.size < 2) {
97              (!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();
98              (raid && (          }
99                  (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)) ||  
100                  (!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) {
101              ))) && c.type === 'GUILD_TEXT'              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          await unlockAll(client, role, msg, options, channels as Collection <string, TextChannel>, force);          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.51  
changed lines
  Added in v.244

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26