/[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 107 by rakin, Mon Jul 29 17:28:37 2024 UTC revision 244 by rakin, Mon Jul 29 17:29:11 2024 UTC
# Line 1  Line 1 
1  import { Collection, CommandInteraction, GuildBasedChannel, GuildChannel, Message, Permissions, Role, TextChannel } from 'discord.js';  import { Collection, CommandInteraction, GuildBasedChannel, GuildChannel, Message, Permissions, Role, TextChannel, User } from 'discord.js';
2  import BaseCommand from '../../utils/structures/BaseCommand';  import BaseCommand from '../../utils/structures/BaseCommand';
3  import CommandOptions from '../../types/CommandOptions';  import CommandOptions from '../../types/CommandOptions';
4  import InteractionOptions from '../../types/InteractionOptions';  import InteractionOptions from '../../types/InteractionOptions';
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 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) {
9      if (role) {      if (role) {
10          // 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);
11    
12          await channels.forEach(async channel => {          try {
13              try {              return await client.channelLock.lockAll(channels instanceof Collection ? [...channels.values()] : channels, user, {
14                  if (send) {                  role,
15                      await channel.send({                  sendConfirmation: send,
16                          embeds: [                  reason
17                              new MessageEmbed()              });
18                              .setDescription(':lock: This channel has been locked.')          }
19                          ]          catch (e) {
20                      });              console.log(e);
21                  }          }
   
                 let dbPerms;  
   
                 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);  
   
                 if (allowperms && !denyperms) {  
                     await (dbPerms = 'ALLOW');  
                 }  
                 else if (!allowperms && denyperms) {  
                     await (dbPerms = 'DENY');  
                 }  
                 else if (!allowperms && !denyperms) {  
                     await (dbPerms = 'NULL');  
                 }  
   
                 console.log(dbPerms);  
                   
   
                 await client.db.get('INSERT INTO locks(channel_id, perms, date) VALUES(?, ?, ?)', [channel.id, dbPerms, 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);  
                     }  
                 })  
             }  
             catch (e) {  
                 console.log(e);  
             }  
         });  
22      }      }
23  }  }
24    
# Line 80  export default class LockallCommand exte Line 42  export default class LockallCommand exte
42              return;              return;
43          }          }
44    
45            if (msg instanceof CommandInteraction) {
46                await msg.deferReply({ ephemeral: true });
47            }
48    
49          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);
50    
51          let role: Role = <Role> msg.guild!.roles.everyone;          let role: Role = <Role> msg.guild!.roles.everyone;
52          let lockall: string[] = [], lockallChannels: Collection<string, TextChannel> = new Collection();          let lockall: string[] = [], lockallChannels: TextChannel[] = [];
53            let reason: string | undefined;
54          // 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);
55    
56          if (options.isInteraction) {          if (options.isInteraction) {
57              lockall = options.options.getString('channels')!.split(' ');              if (options.options.getString('channels'))
58                    lockall = options.options.getString('channels')!.split(' ').filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
59    
60              if (options.options.getChannel('role')) {              if (options.options.getRole('role')) {
61                  role = await <Role> options.options.getRole('role');                  role = await <Role> options.options.getRole('role');
62              }              }
63    
64                if (options.options.getString('reason')) {
65                    reason = await <string> options.options.getString('reason');
66                }
67          }          }
68          else {          else {
69              if ((msg as Message).mentions.roles.first()) {              if ((msg as Message).mentions.roles.first()) {
70                  role = await <Role> (msg as Message).mentions.roles.first();                  role = await <Role> (msg as Message).mentions.roles.first();
71              }              }
             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]);  
             }  
72    
73              if (!role) {              if (!role) {
74                  await msg.reply({                  await msg.reply({
# Line 115  export default class LockallCommand exte Line 84  export default class LockallCommand exte
84              }              }
85    
86              if (!raid) {              if (!raid) {
87                  for (const a of options.args) {                  lockall = options.normalArgs.filter(a => /^\d+$/gi.test(a) || a.startsWith('<#'));
88                      if (/^\d+$/g.test(a)) {              }
89                          lockall.push(a);              else {
90                    if (msg.guild!.channels.cache.size < 2) {
91                        await msg.guild?.channels.fetch();
92                    }
93    
94                    const raidChannels = client.config.get('raid').channels;
95    
96                    if (client.config.get('raid').exclude) {
97                        lockall = [];
98    
99                        for await (const [id, { parent, type }] of msg.guild!.channels.cache) {
100                            if (type === 'GUILD_CATEGORY')
101                                continue;
102    
103                            if ((raidChannels.includes(id) || raidChannels.includes(parent?.id))) {
104                                continue;
105                            }
106    
107                            lockall.push(id);
108                      }                      }
109                  }                  }
110                        else {
111                  if ((msg as Message).mentions.channels.first()) {                      // for await (const [id, { parent, type }] of msg.guild!.channels.cache) {
112                      (msg as Message).mentions.channels.forEach(c => {                      //     if (type === 'GUILD_CATEGORY')
113                          if (c instanceof TextChannel)                      //         continue;
114                              lockallChannels.set(c.id, c);  
115                      });                      //     if ((!raidChannels.includes(id) && !raidChannels.includes(parent?.id))) {
116                        //         continue;
117                        //     }
118    
119                        //     lockall.push(id);
120                        // }
121    
122                        lockall = raidChannels;
123                  }                  }
124    
125                    console.log("Raid", lockall);
126                    console.log("Raid 1", client.config.get('raid').channels);
127              }              }
128          }          }
129    
130          let channels = raid ? await msg.guild!.channels.cache.filter(c => (          if (lockall.length === 0 && !raid) {
131              (raid && (              await this.deferReply(msg, {
132                  (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)) ||                  content: "No channel specified!"
133                  (!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))              });
134              ))) && c.type === 'GUILD_TEXT'  
135          ) : null;              return;
136            }
137          if (channels === null && !raid) {  
138              channels = msg.guild!.channels.cache.filter(c2 => (lockall.includes(c2.id) || lockall.includes(c2.parent?.id!)) && c2.type === 'GUILD_TEXT')!;          if (msg.guild!.channels.cache.size < 2) {
139              channels = channels.merge(lockallChannels, c => ({ keep: true, value: c }), c => ({ keep: true, value: c }), (c1, c2) => ({ keep: true, value: c2 }));              await msg.guild?.channels.fetch();
140            }
141    
142            for await (const c of lockall) {
143                console.log(c);
144                const id = c.startsWith('<#') ? c.substring(2, c.length - 1) : c;
145    
146                if (lockallChannels.find(c => c.id === id))
147                    continue;
148                
149                const channel = msg.guild?.channels.cache.get(id);
150    
151                if (!channel) {
152                    continue;
153                }
154                    
155                if (!channel.isText()) {
156                    lockallChannels = [...lockallChannels, ...(msg.guild?.channels.cache.filter(c => c.parent?.id === id) as Collection<string, TextChannel>).values()!];
157                    continue;
158                }
159    
160                lockallChannels.push(channel as TextChannel);
161          }          }
162    
163          await lockAll(client, role, channels as Collection <string, TextChannel>, true);          console.log("Array: ", lockallChannels, lockall);
164    
165            const [success, failure] = (await lockAll(client, role, lockallChannels, msg.member!.user as User, true, reason))!;
166    
167          if (options.isInteraction) {          if (options.isInteraction) {
168              await msg.reply({              await this.deferReply(msg, {
169                  content: "The channels are locked.",                  content: "Locked " + lockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful locks and ${failure} failed locks.` : '')
                 ephemeral: true  
170              });              });
171          }          }
172          else {          else {

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26