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

Annotation of /trunk/src/commands/moderation/UnlockallCommand.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: 4758 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 unlockAll(client: DiscordClient, role: Role, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions, channels: Collection <string, TextChannel>, force: boolean) {
9     if (role) {
10     channels.forEach(async channel => {
11     try {
12     client.db.get('SELECT * FROM locks WHERE channel_id = ?', [channel.id], async (err: any, data: any) => {
13     if (data || force) {
14 rakin 103 await channel.send({
15     embeds: [
16     new MessageEmbed()
17     .setDescription(':closed_lock_with_key: This channel has been unlocked.')
18     ]
19     });
20    
21 rakin 51 let perm;
22 rakin 103 const data1 = data?.perms;
23 rakin 51
24     if (data1) {
25 rakin 103 if (data1 === 'DENY') {
26 rakin 51 await (perm = false);
27     }
28 rakin 103 else if (data1 === 'NULL') {
29 rakin 51 await (perm = null);
30     }
31 rakin 103 else if (data1 === 'ALLOW') {
32 rakin 51 await (perm = true);
33     }
34     }
35    
36     if (force) {
37     await (perm = true);
38     }
39    
40     await console.log(channel.name);
41    
42     try {
43     await channel.permissionOverwrites.edit(role, {
44     SEND_MESSAGES: perm,
45     });
46    
47     }
48     catch (e) {
49     console.log(e);
50     }
51    
52 rakin 103 await console.log(perm);
53 rakin 51
54     if (data) {
55     await client.db.get('DELETE FROM locks WHERE id = ?', [data?.id], async (err: any) => {});
56     }
57     }
58     });
59     }
60     catch(e) {
61     console.log(e);
62     }
63     });
64     }
65     }
66    
67     export default class UnlockallCommand extends BaseCommand {
68     supportsInteractions: boolean = true;
69    
70     constructor() {
71     super('unlockall', 'moderation', []);
72     }
73    
74     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
75 rakin 103 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
76     await msg.reply({
77     embeds: [
78     new MessageEmbed()
79     .setColor('#f14a60')
80     .setDescription(`This command requires at least one argument.`)
81     ]
82     });
83    
84     return;
85     }
86    
87 rakin 51 const raid = options.isInteraction ? options.options.getBoolean('raid') === true : (options.options.indexOf('--raid') !== -1);
88    
89     let role: Role = <Role> msg.guild!.roles.everyone;
90 rakin 103 let unlockall: string[] = [];
91 rakin 51 const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
92    
93     if (options.isInteraction) {
94     if (options.options.getChannel('role')) {
95     role = await <Role> options.options.getRole('role');
96     }
97     }
98     else {
99     if ((msg as Message).mentions.roles.first()) {
100 rakin 103 role = await <Role> (msg as Message).mentions.roles.first();
101     }
102     else if (options.options.includes('-r') && options.normalArgs[options.options.indexOf('-r') + 1]) {
103     role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[options.options.indexOf('-r') + 1]);
104     }
105 rakin 51
106     if (!role) {
107     await msg.reply({
108     embeds: [
109     new MessageEmbed()
110     .setColor('#f14a60')
111     .setDescription(`Invalid role given.`)
112     ],
113     ephemeral: true
114     });
115    
116     return;
117     }
118 rakin 103
119     for (const a of options.args) {
120     if (/^\d+$/g.test(a)) {
121     unlockall.push(a);
122     }
123     }
124 rakin 51 }
125    
126 rakin 103 let channels = raid ? await msg.guild!.channels.cache.filter(c => (
127 rakin 51 (raid && (
128     (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)) ||
129     (!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))
130     ))) && c.type === 'GUILD_TEXT'
131 rakin 103 ) : null;
132 rakin 51
133 rakin 103 if (channels === null && !raid) {
134     channels = msg.guild!.channels.cache.filter(c2 => (unlockall.includes(c2.id) || unlockall.includes(c2.parent?.id!)) && c2.type === 'GUILD_TEXT')!;
135     }
136    
137 rakin 51 await unlockAll(client, role, msg, options, channels as Collection <string, TextChannel>, force);
138    
139     if (options.isInteraction) {
140     await msg.reply({
141     content: "The channels are unlocked.",
142     ephemeral: true
143     });
144     }
145     else {
146     await (msg as Message).react('🔓');
147     }
148     }
149     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26