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

Annotation of /trunk/src/commands/moderation/LockallCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 103 - (hide annotations)
Mon Jul 29 17:28:36 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5824 byte(s)
Improved channel locking
1 rakin 51 import { Collection, CommandInteraction, GuildBasedChannel, GuildChannel, Message, Permissions, Role, TextChannel } from 'discord.js';
2     import BaseCommand from '../../utils/structures/BaseCommand';
3     import CommandOptions from '../../types/CommandOptions';
4     import InteractionOptions from '../../types/InteractionOptions';
5     import DiscordClient from '../../client/Client';
6     import MessageEmbed from '../../client/MessageEmbed';
7    
8     export async function lockAll(client: DiscordClient, role: Role, channels: Collection <string, TextChannel>, send: boolean = true) {
9     if (role) {
10 rakin 103 // const gen = await channels.first()!.guild.roles.fetch(client.config.props[channels.first()!.guild.id].gen_role);
11 rakin 51
12     await channels.forEach(async channel => {
13     try {
14     if (send) {
15     await channel.send({
16     embeds: [
17     new MessageEmbed()
18     .setDescription(':lock: This channel has been locked.')
19     ]
20     });
21     }
22    
23     let dbPerms;
24    
25     let overWrites = await channel.permissionOverwrites.cache.get(role.id);
26     let allowperms = await overWrites?.allow?.has(Permissions.FLAGS.SEND_MESSAGES);
27     let denyperms = await overWrites?.deny?.has(Permissions.FLAGS.SEND_MESSAGES);
28    
29     if (allowperms && !denyperms) {
30     await (dbPerms = 'ALLOW');
31     }
32     else if (!allowperms && denyperms) {
33     await (dbPerms = 'DENY');
34     }
35     else if (!allowperms && !denyperms) {
36     await (dbPerms = 'NULL');
37     }
38    
39 rakin 103 console.log(dbPerms);
40 rakin 51
41 rakin 103
42     await client.db.get('INSERT INTO locks(channel_id, perms, date) VALUES(?, ?, ?)', [channel.id, dbPerms, new Date().toISOString()], async (err: any) => {
43 rakin 51 if (err)
44     console.log(err);
45    
46     try {
47     await channel.permissionOverwrites.edit(role, {
48     SEND_MESSAGES: false,
49     });
50     }
51     catch (e) {
52     console.log(e);
53     }
54     })
55     }
56     catch (e) {
57     console.log(e);
58     }
59     });
60     }
61     }
62    
63     export default class LockallCommand extends BaseCommand {
64     supportsInteractions: boolean = true;
65    
66     constructor() {
67     super('lockall', 'moderation', []);
68     }
69    
70     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
71 rakin 103 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
72     await msg.reply({
73     embeds: [
74     new MessageEmbed()
75     .setColor('#f14a60')
76     .setDescription(`This command requires at least one argument.`)
77     ]
78     });
79    
80     return;
81     }
82    
83 rakin 51 const raid = options.isInteraction ? options.options.getBoolean('raid') === true : (options.options.indexOf('--raid') !== -1);
84    
85     let role: Role = <Role> msg.guild!.roles.everyone;
86 rakin 103 let lockall: string[] = [];
87 rakin 51 // const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
88    
89     if (options.isInteraction) {
90 rakin 103 lockall = options.options.getString('channels')!.split(' ');
91    
92 rakin 51 if (options.options.getChannel('role')) {
93     role = await <Role> options.options.getRole('role');
94     }
95     }
96     else {
97     if ((msg as Message).mentions.roles.first()) {
98     role = await <Role> (msg as Message).mentions.roles.first();
99     }
100 rakin 103 else if (options.options.includes('-r') && options.normalArgs[options.options.indexOf('-r') + 1]) {
101     role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[options.options.indexOf('-r') + 1]);
102 rakin 51 }
103    
104     if (!role) {
105     await msg.reply({
106     embeds: [
107     new MessageEmbed()
108     .setColor('#f14a60')
109     .setDescription(`Invalid role given.`)
110     ],
111     ephemeral: true
112     });
113    
114     return;
115     }
116 rakin 103
117     for (const a of options.args) {
118     if (/^\d+$/g.test(a)) {
119     lockall.push(a);
120     }
121     }
122 rakin 51 }
123    
124 rakin 103 let channels = raid ? await msg.guild!.channels.cache.filter(c => (
125 rakin 51 (raid && (
126     (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)) ||
127     (!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))
128     ))) && c.type === 'GUILD_TEXT'
129 rakin 103 ) : null;
130    
131     if (channels === null && !raid) {
132     channels = msg.guild!.channels.cache.filter(c2 => (lockall.includes(c2.id) || lockall.includes(c2.parent?.id!)) && c2.type === 'GUILD_TEXT')!;
133     }
134    
135 rakin 51 await lockAll(client, role, channels as Collection <string, TextChannel>, true);
136    
137     if (options.isInteraction) {
138     await msg.reply({
139     content: "The channels are locked.",
140     ephemeral: true
141     });
142     }
143     else {
144     await (msg as Message).react('🔒');
145     }
146     }
147     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26