1 |
import { Collection, CommandInteraction, GuildBasedChannel, GuildChannel, Message, Permissions, Role, TextChannel } from 'discord.js'; |
/** |
2 |
|
* This file is part of SudoBot. |
3 |
|
* |
4 |
|
* Copyright (C) 2021-2022 OSN Inc. |
5 |
|
* |
6 |
|
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
|
* under the terms of the GNU Affero General Public License as published by |
8 |
|
* the Free Software Foundation, either version 3 of the License, or |
9 |
|
* (at your option) any later version. |
10 |
|
* |
11 |
|
* SudoBot is distributed in the hope that it will be useful, but |
12 |
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
* GNU Affero General Public License for more details. |
15 |
|
* |
16 |
|
* You should have received a copy of the GNU Affero General Public License |
17 |
|
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
|
*/ |
19 |
|
|
20 |
|
import { Collection, CommandInteraction, Message, Role, TextChannel } from 'discord.js'; |
21 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
import BaseCommand from '../../utils/structures/BaseCommand'; |
22 |
import CommandOptions from '../../types/CommandOptions'; |
import CommandOptions from '../../types/CommandOptions'; |
23 |
import InteractionOptions from '../../types/InteractionOptions'; |
import InteractionOptions from '../../types/InteractionOptions'; |
24 |
import DiscordClient from '../../client/Client'; |
import DiscordClient from '../../client/Client'; |
25 |
import MessageEmbed from '../../client/MessageEmbed'; |
import MessageEmbed from '../../client/MessageEmbed'; |
26 |
|
|
27 |
export async function unlockAll(client: DiscordClient, role: Role, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions, channels: Collection <string, TextChannel>, force: boolean) { |
export async function unlockAll(client: DiscordClient, role: Role, channels: TextChannel[], force: boolean, reason?: string) { |
28 |
if (role) { |
try { |
29 |
channels.forEach(async channel => { |
return await client.channelLock.unlockAll(channels, { |
30 |
try { |
force, |
31 |
client.db.get('SELECT * FROM locks WHERE channel_id = ?', [channel.id], async (err: any, data: any) => { |
role, |
32 |
if (data || force) { |
reason, |
33 |
await channel.send({ |
sendConfirmation: true |
|
embeds: [ |
|
|
new MessageEmbed() |
|
|
.setDescription(':closed_lock_with_key: This channel has been unlocked.') |
|
|
] |
|
|
}); |
|
|
|
|
|
let perm; |
|
|
const data1 = data?.perms; |
|
|
|
|
|
if (data1) { |
|
|
if (data1 === 'DENY') { |
|
|
await (perm = false); |
|
|
} |
|
|
else if (data1 === 'NULL') { |
|
|
await (perm = null); |
|
|
} |
|
|
else if (data1 === 'ALLOW') { |
|
|
await (perm = true); |
|
|
} |
|
|
} |
|
|
|
|
|
if (force) { |
|
|
await (perm = true); |
|
|
} |
|
|
|
|
|
await console.log(channel.name); |
|
|
|
|
|
try { |
|
|
await channel.permissionOverwrites.edit(role, { |
|
|
SEND_MESSAGES: perm, |
|
|
}); |
|
|
|
|
|
} |
|
|
catch (e) { |
|
|
console.log(e); |
|
|
} |
|
|
|
|
|
await console.log(perm); |
|
|
|
|
|
if (data) { |
|
|
await client.db.get('DELETE FROM locks WHERE id = ?', [data?.id], async (err: any) => {}); |
|
|
} |
|
|
} |
|
|
}); |
|
|
} |
|
|
catch(e) { |
|
|
console.log(e); |
|
|
} |
|
34 |
}); |
}); |
35 |
} |
} |
36 |
|
catch (e) { |
37 |
|
console.log(e); |
38 |
|
} |
39 |
} |
} |
40 |
|
|
41 |
export default class UnlockallCommand extends BaseCommand { |
export default class UnlockallCommand extends BaseCommand { |
58 |
return; |
return; |
59 |
} |
} |
60 |
|
|
61 |
|
if (msg instanceof CommandInteraction) { |
62 |
|
await msg.deferReply({ ephemeral: true }); |
63 |
|
} |
64 |
|
|
65 |
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); |
66 |
|
|
67 |
let role: Role = <Role> msg.guild!.roles.everyone; |
let role: Role = <Role> msg.guild!.roles.everyone; |
68 |
let unlockall: string[] = [], unlockallChannels: Collection<string, TextChannel> = new Collection(); |
let unlockall: string[] = [], unlockallChannels: TextChannel[] = []; |
69 |
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); |
70 |
|
let reason: string | undefined; |
71 |
|
|
72 |
if (options.isInteraction) { |
if (options.isInteraction) { |
73 |
if (options.options.getChannel('role')) { |
if (options.options.getString('channels')) |
74 |
|
unlockall = options.options.getString('channels')!.split(' ').filter(a => /^\d+$/gi.test(a) || a.startsWith('<#')); |
75 |
|
|
76 |
|
if (options.options.getRole('role')) { |
77 |
role = await <Role> options.options.getRole('role'); |
role = await <Role> options.options.getRole('role'); |
78 |
} |
} |
79 |
|
|
80 |
|
if (options.options.getString('reason')) { |
81 |
|
reason = await <string> options.options.getString('reason'); |
82 |
|
} |
83 |
} |
} |
84 |
else { |
else { |
85 |
if ((msg as Message).mentions.roles.first()) { |
if ((msg as Message).mentions.roles.first()) { |
86 |
role = await <Role> (msg as Message).mentions.roles.first(); |
role = await <Role> (msg as Message).mentions.roles.first(); |
87 |
} |
} |
|
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]); |
|
|
} |
|
88 |
|
|
89 |
if (!role) { |
if (!role) { |
90 |
await msg.reply({ |
await msg.reply({ |
100 |
} |
} |
101 |
|
|
102 |
if (!raid) { |
if (!raid) { |
103 |
for (const a of options.args) { |
unlockall = options.normalArgs.filter(a => /^\d+$/gi.test(a) || a.startsWith('<#')); |
104 |
if (/^\d+$/g.test(a)) { |
console.log("Unlock", unlockall); |
105 |
unlockall.push(a); |
} |
106 |
} |
else { |
107 |
} |
if (msg.guild!.channels.cache.size < 2) { |
108 |
|
await msg.guild?.channels.fetch(); |
|
if ((msg as Message).mentions.channels.first()) { |
|
|
(msg as Message).mentions.channels.forEach(c => { |
|
|
if (c instanceof TextChannel) |
|
|
unlockallChannels.set(c.id, c); |
|
|
}); |
|
109 |
} |
} |
110 |
|
|
111 |
|
unlockall = client.config.get('raid').exclude ? msg.guild?.channels.cache.filter(c => !client.config.get('raid').channels.includes(c.id) && !client.config.get('raid').channels.includes(c.parent?.id)) : client.config.get('raid').channels; |
112 |
} |
} |
113 |
} |
} |
114 |
|
|
115 |
let channels = raid ? await msg.guild!.channels.cache.filter(c => ( |
if (msg.guild!.channels.cache.size < 2) { |
116 |
(raid && ( |
await msg.guild?.channels.fetch(); |
|
(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)) || |
|
|
(!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)) |
|
|
))) && c.type === 'GUILD_TEXT' |
|
|
) : null; |
|
|
|
|
|
if (channels === null && !raid) { |
|
|
channels = msg.guild!.channels.cache.filter(c2 => (unlockall.includes(c2.id) || unlockall.includes(c2.parent?.id!)) && c2.type === 'GUILD_TEXT')!; |
|
|
channels = channels.merge(unlockallChannels, c => ({ keep: true, value: c }), c => ({ keep: true, value: c }), (c1, c2) => ({ keep: true, value: c2 })); |
|
117 |
} |
} |
118 |
|
|
119 |
await unlockAll(client, role, msg, options, channels as Collection <string, TextChannel>, force); |
if (unlockall.length === 0 && !raid) { |
120 |
|
await this.deferReply(msg, { |
121 |
|
content: "No channel specified!" |
122 |
|
}); |
123 |
|
|
124 |
|
return; |
125 |
|
} |
126 |
|
|
127 |
|
for await (const c of unlockall) { |
128 |
|
console.log(c); |
129 |
|
const id = c.startsWith('<#') ? c.substring(2, c.length - 1) : c; |
130 |
|
|
131 |
|
if (unlockallChannels.find(c => c.id === id)) |
132 |
|
continue; |
133 |
|
|
134 |
|
const channel = msg.guild?.channels.cache.get(id); |
135 |
|
|
136 |
|
if (!channel) { |
137 |
|
continue; |
138 |
|
} |
139 |
|
|
140 |
|
if (!channel.isText()) { |
141 |
|
unlockallChannels = [...unlockallChannels, ...(msg.guild?.channels.cache.filter(c => c.parent?.id === id) as Collection<string, TextChannel>).values()!]; |
142 |
|
continue; |
143 |
|
} |
144 |
|
|
145 |
|
unlockallChannels.push(channel as TextChannel); |
146 |
|
} |
147 |
|
|
148 |
|
const [success, failure] = (await unlockAll(client, role, unlockallChannels, force, reason))!; |
149 |
|
|
150 |
if (options.isInteraction) { |
if (options.isInteraction) { |
151 |
await msg.reply({ |
await this.deferReply(msg, { |
152 |
content: "The channels are unlocked.", |
content: "Unlocked " + unlockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful unlocks and ${failure} failed unlocks.` : '') |
153 |
ephemeral: true |
}); |
|
}); |
|
154 |
} |
} |
155 |
else { |
else { |
156 |
await (msg as Message).react('🔓'); |
await (msg as Message).react('🔓'); |