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

Contents of /trunk/src/commands/moderation/UnlockallCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 107 - (show 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 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 await channel.send({
15 embeds: [
16 new MessageEmbed()
17 .setDescription(':closed_lock_with_key: This channel has been unlocked.')
18 ]
19 });
20
21 let perm;
22 const data1 = data?.perms;
23
24 if (data1) {
25 if (data1 === 'DENY') {
26 await (perm = false);
27 }
28 else if (data1 === 'NULL') {
29 await (perm = null);
30 }
31 else if (data1 === 'ALLOW') {
32 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 await console.log(perm);
53
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 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 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 let unlockall: string[] = [], unlockallChannels: Collection<string, TextChannel> = new Collection();
91 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 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
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
119 if (!raid) {
120 for (const a of options.args) {
121 if (/^\d+$/g.test(a)) {
122 unlockall.push(a);
123 }
124 }
125
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 }
133 }
134
135 let channels = raid ? await msg.guild!.channels.cache.filter(c => (
136 (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 ) : null;
141
142 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 channels = channels.merge(unlockallChannels, c => ({ keep: true, value: c }), c => ({ keep: true, value: c }), (c1, c2) => ({ keep: true, value: c2 }));
145 }
146
147 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