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

Diff of /trunk/src/commands/moderation/LockallCommand.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, User } 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 lockAll(client: DiscordClient, role: Role, channels: Collection <string, TextChannel>, send: boolean = true) {  export async function lockAll(client: DiscordClient, role: Role, channels: Collection <string, TextChannel> | TextChannel[], user: User, send: boolean = true, reason?: string) {
28      if (role) {      if (role) {
29          let role1 = await channels.first()!.guild.roles.fetch(client.config.props[channels.first()!.guild.id].gen_role);          // const gen = await channels.first()!.guild.roles.fetch(client.config.props[channels.first()!.guild.id].gen_role);
         const gen = await channels.first()!.guild.roles.fetch(client.config.props[channels.first()!.guild.id].gen_role);  
   
         await channels.forEach(async channel => {  
             try {  
                 if (send) {  
                     await channel.send({  
                         embeds: [  
                             new MessageEmbed()  
                             .setDescription(':lock: This channel has been locked.')  
                         ]  
                     });  
                 }  
   
                 let dbPerms;  
                 let dbPerms1;  
   
                 let overWrites = await channel.permissionOverwrites.cache.get(role.id);  
                 let allowperms = await overWrites?.allow?.has(Permissions.FLAGS.SEND_MESSAGES);  
                 let denyperms = await overWrites?.deny?.has(Permissions.FLAGS.SEND_MESSAGES);  
   
                 let overWrites1 = await channel.permissionOverwrites.cache.get(role1!.id);  
                 let allowperms1 = await overWrites1?.allow?.has(Permissions.FLAGS.SEND_MESSAGES);  
                 let denyperms1 = await overWrites1?.deny?.has(Permissions.FLAGS.SEND_MESSAGES);  
   
                 if (allowperms && !denyperms) {  
                     await (dbPerms = 'ALLOW');  
                 }  
                 else if (!allowperms && denyperms) {  
                     await (dbPerms = 'DENY');  
                 }  
                 else if (!allowperms && !denyperms) {  
                     await (dbPerms = 'NULL');  
                 }  
30    
31                  if (allowperms1 && !denyperms1) {          try {
32                      await (dbPerms1 = 'ALLOW');              return await client.channelLock.lockAll(channels instanceof Collection ? [...channels.values()] : channels, user, {
33                  }                  role,
34                  else if (!allowperms1 && denyperms1) {                  sendConfirmation: send,
35                      await (dbPerms1 = 'DENY');                  reason
36                  }              });
37                  else if (!allowperms1 && !denyperms1) {          }
38                      await (dbPerms1 = 'NULL');          catch (e) {
39                  }              console.log(e);
40                            }
                 await client.db.get('INSERT INTO locks(channel_id, perms, date) VALUES(?, ?, ?)', [channel.id, dbPerms + ',' + dbPerms1, new Date().toISOString()], async (err: any) => {  
                     if (err)  
                         console.log(err);  
                       
                     try {  
                         await channel.permissionOverwrites.edit(role, {  
                             SEND_MESSAGES: false,  
                         });  
                     }  
                     catch (e) {  
                         console.log(e);  
                     }  
   
                     try {  
                         await channel.permissionOverwrites.edit(gen!, {  
                             SEND_MESSAGES: false,  
                         });  
                     }  
                     catch (e) {  
                         console.log(e);  
                     }  
                 })  
             }  
             catch (e) {  
                 console.log(e);  
             }  
         });  
41      }      }
42  }  }
43    
# Line 90  export default class LockallCommand exte Line 49  export default class LockallCommand exte
49      }      }
50    
51      async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {      async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
52            if (!options.isInteraction && typeof options.args[0] === 'undefined') {
53                await msg.reply({
54                    embeds: [
55                        new MessageEmbed()
56                        .setColor('#f14a60')
57                        .setDescription(`This command requires at least one argument.`)
58                    ]
59                });
60    
61                return;
62            }
63    
64            if (msg instanceof CommandInteraction) {
65                await msg.deferReply({ ephemeral: true });
66            }
67    
68          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);
69    
70          let role: Role = <Role> msg.guild!.roles.everyone;          let role: Role = <Role> msg.guild!.roles.everyone;
71            let lockall: string[] = [], lockallChannels: TextChannel[] = [];
72            let reason: string | undefined;
73          // 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);
74    
75          if (options.isInteraction) {          if (options.isInteraction) {
76              if (options.options.getChannel('role')) {              if (options.options.getString('channels'))
77                    lockall = options.options.getString('channels')!.split(' ').filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
78    
79                if (options.options.getRole('role')) {
80                  role = await <Role> options.options.getRole('role');                  role = await <Role> options.options.getRole('role');
81              }              }
82    
83                if (options.options.getString('reason')) {
84                    reason = await <string> options.options.getString('reason');
85                }
86          }          }
87          else {          else {
88              if ((msg as Message).mentions.roles.first()) {              if ((msg as Message).mentions.roles.first()) {
89                  role = await <Role> (msg as Message).mentions.roles.first();                  role = await <Role> (msg as Message).mentions.roles.first();
90              }              }
             else if (options.normalArgs[0] && options.normalArgs[0] !== 'everyone') {  
                 role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[0]);  
             }  
91    
92              if (!role) {              if (!role) {
93                  await msg.reply({                  await msg.reply({
# Line 120  export default class LockallCommand exte Line 101  export default class LockallCommand exte
101            
102                  return;                  return;
103              }              }
104    
105                if (!raid) {
106                    lockall = options.normalArgs.filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
107                }
108                else {
109                    if (msg.guild!.channels.cache.size < 2) {
110                        await msg.guild?.channels.fetch();
111                    }
112    
113                    const raidChannels = client.config.get('raid').channels;
114    
115                    if (client.config.get('raid').exclude) {
116                        lockall = [];
117    
118                        for await (const [id, { parent, type }] of msg.guild!.channels.cache) {
119                            if (type === 'GUILD_CATEGORY')
120                                continue;
121    
122                            if ((raidChannels.includes(id) || raidChannels.includes(parent?.id))) {
123                                continue;
124                            }
125    
126                            lockall.push(id);
127                        }
128                    }
129                    else {
130                        // for await (const [id, { parent, type }] of msg.guild!.channels.cache) {
131                        //     if (type === 'GUILD_CATEGORY')
132                        //         continue;
133    
134                        //     if ((!raidChannels.includes(id) && !raidChannels.includes(parent?.id))) {
135                        //         continue;
136                        //     }
137    
138                        //     lockall.push(id);
139                        // }
140    
141                        lockall = raidChannels;
142                    }
143    
144                    console.log("Raid", lockall);
145                    console.log("Raid 1", client.config.get('raid').channels);
146                }
147            }
148    
149            if (lockall.length === 0 && !raid) {
150                await this.deferReply(msg, {
151                    content: "No channel specified!"
152                });
153    
154                return;
155            }
156    
157            if (msg.guild!.channels.cache.size < 2) {
158                await msg.guild?.channels.fetch();
159            }
160    
161            for await (const c of lockall) {
162                console.log(c);
163                const id = c.startsWith('<#') ? c.substring(2, c.length - 1) : c;
164    
165                if (lockallChannels.find(c => c.id === id))
166                    continue;
167                
168                const channel = msg.guild?.channels.cache.get(id);
169    
170                if (!channel) {
171                    continue;
172                }
173                    
174                if (!channel.isText()) {
175                    lockallChannels = [...lockallChannels, ...(msg.guild?.channels.cache.filter(c => c.parent?.id === id) as Collection<string, TextChannel>).values()!];
176                    continue;
177                }
178    
179                lockallChannels.push(channel as TextChannel);
180          }          }
181    
182          const channels: Collection <string, GuildBasedChannel> = await msg.guild!.channels.cache.filter(c => (          console.log("Array: ", lockallChannels, lockall);
183              (!raid && (client.config.props[msg.guild!.id].lockall.indexOf(c.id) !== -1 || client.config.props[msg.guild!.id].lockall.indexOf(c.parent?.id) !== -1)) ||  
184              (raid && (          const [success, failure] = (await lockAll(client, role, lockallChannels, msg.member!.user as User, true, reason))!;
                 (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'  
         );  
         await lockAll(client, role, channels as Collection <string, TextChannel>, true);  
185    
186          if (options.isInteraction) {          if (options.isInteraction) {
187              await msg.reply({              await this.deferReply(msg, {
188                  content: "The channels are locked.",                  content: "Locked " + lockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful locks and ${failure} failed locks.` : '')
                 ephemeral: true  
189              });              });
190          }          }
191          else {          else {

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26