/[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 107 - (hide annotations)
Mon Jul 29 17:28:37 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5341 byte(s)
Updated help information
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 107 let unlockall: string[] = [], unlockallChannels: Collection<string, TextChannel> = new Collection();
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 rakin 107 if (!raid) {
120     for (const a of options.args) {
121     if (/^\d+$/g.test(a)) {
122     unlockall.push(a);
123     }
124 rakin 103 }
125 rakin 107
126     if ((msg as Message).mentions.channels.first()) {
127     (msg as Message).mentions.channels.forEach(c => {
128     if (c instanceof TextChannel)
129     unlockallChannels.set(c.id, c);
130     });
131     }
132 rakin 103 }
133 rakin 51 }
134    
135 rakin 103 let channels = raid ? await msg.guild!.channels.cache.filter(c => (
136 rakin 51 (raid && (
137     (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)) ||
138     (!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))
139     ))) && c.type === 'GUILD_TEXT'
140 rakin 103 ) : null;
141 rakin 51
142 rakin 103 if (channels === null && !raid) {
143     channels = msg.guild!.channels.cache.filter(c2 => (unlockall.includes(c2.id) || unlockall.includes(c2.parent?.id!)) && c2.type === 'GUILD_TEXT')!;
144 rakin 107 channels = channels.merge(unlockallChannels, c => ({ keep: true, value: c }), c => ({ keep: true, value: c }), (c1, c2) => ({ keep: true, value: c2 }));
145 rakin 103 }
146    
147 rakin 51 await unlockAll(client, role, msg, options, channels as Collection <string, TextChannel>, force);
148    
149     if (options.isInteraction) {
150     await msg.reply({
151     content: "The channels are unlocked.",
152     ephemeral: true
153     });
154     }
155     else {
156     await (msg as Message).react('🔓');
157     }
158     }
159     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26