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, channels: TextChannel[], force: boolean, reason?: string) { |
9 |
try { |
10 |
return await client.channelLock.unlockAll(channels, { |
11 |
force, |
12 |
role, |
13 |
reason, |
14 |
sendConfirmation: true |
15 |
}); |
16 |
} |
17 |
catch (e) { |
18 |
console.log(e); |
19 |
} |
20 |
} |
21 |
|
22 |
export default class UnlockallCommand extends BaseCommand { |
23 |
supportsInteractions: boolean = true; |
24 |
|
25 |
constructor() { |
26 |
super('unlockall', 'moderation', []); |
27 |
} |
28 |
|
29 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
30 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
31 |
await msg.reply({ |
32 |
embeds: [ |
33 |
new MessageEmbed() |
34 |
.setColor('#f14a60') |
35 |
.setDescription(`This command requires at least one argument.`) |
36 |
] |
37 |
}); |
38 |
|
39 |
return; |
40 |
} |
41 |
|
42 |
if (msg instanceof CommandInteraction) { |
43 |
await msg.deferReply({ ephemeral: true }); |
44 |
} |
45 |
|
46 |
const raid = options.isInteraction ? options.options.getBoolean('raid') === true : (options.options.indexOf('--raid') !== -1); |
47 |
|
48 |
let role: Role = <Role> msg.guild!.roles.everyone; |
49 |
let unlockall: string[] = [], unlockallChannels: TextChannel[] = []; |
50 |
const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1); |
51 |
let reason: string | undefined; |
52 |
|
53 |
if (options.isInteraction) { |
54 |
if (options.options.getString('channels')) |
55 |
unlockall = options.options.getString('channels')!.split(' ').filter(a => /^\d+$/gi.test(a) || a.startsWith('<#')); |
56 |
|
57 |
if (options.options.getRole('role')) { |
58 |
role = await <Role> options.options.getRole('role'); |
59 |
} |
60 |
|
61 |
if (options.options.getString('reason')) { |
62 |
reason = await <string> options.options.getString('reason'); |
63 |
} |
64 |
} |
65 |
else { |
66 |
if ((msg as Message).mentions.roles.first()) { |
67 |
role = await <Role> (msg as Message).mentions.roles.first(); |
68 |
} |
69 |
|
70 |
if (!role) { |
71 |
await msg.reply({ |
72 |
embeds: [ |
73 |
new MessageEmbed() |
74 |
.setColor('#f14a60') |
75 |
.setDescription(`Invalid role given.`) |
76 |
], |
77 |
ephemeral: true |
78 |
}); |
79 |
|
80 |
return; |
81 |
} |
82 |
|
83 |
if (!raid) { |
84 |
unlockall = options.normalArgs.filter(a => /^\d+$/gi.test(a) || a.startsWith('<#')); |
85 |
console.log("Unlock", unlockall); |
86 |
} |
87 |
else { |
88 |
if (msg.guild!.channels.cache.size < 2) { |
89 |
await msg.guild?.channels.fetch(); |
90 |
} |
91 |
|
92 |
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; |
93 |
} |
94 |
} |
95 |
|
96 |
if (msg.guild!.channels.cache.size < 2) { |
97 |
await msg.guild?.channels.fetch(); |
98 |
} |
99 |
|
100 |
if (unlockall.length === 0 && !raid) { |
101 |
await this.deferReply(msg, { |
102 |
content: "No channel specified!" |
103 |
}); |
104 |
|
105 |
return; |
106 |
} |
107 |
|
108 |
for await (const c of unlockall) { |
109 |
console.log(c); |
110 |
const id = c.startsWith('<#') ? c.substring(2, c.length - 1) : c; |
111 |
|
112 |
if (unlockallChannels.find(c => c.id === id)) |
113 |
continue; |
114 |
|
115 |
const channel = msg.guild?.channels.cache.get(id); |
116 |
|
117 |
if (!channel) { |
118 |
continue; |
119 |
} |
120 |
|
121 |
if (!channel.isText()) { |
122 |
unlockallChannels = [...unlockallChannels, ...(msg.guild?.channels.cache.filter(c => c.parent?.id === id) as Collection<string, TextChannel>).values()!]; |
123 |
continue; |
124 |
} |
125 |
|
126 |
unlockallChannels.push(channel as TextChannel); |
127 |
} |
128 |
|
129 |
const [success, failure] = (await unlockAll(client, role, unlockallChannels, force, reason))!; |
130 |
|
131 |
if (options.isInteraction) { |
132 |
await this.deferReply(msg, { |
133 |
content: "Unlocked " + unlockallChannels.length + " channel(s)." + (failure > 0 ? ` ${success} successful unlocks and ${failure} failed unlocks.` : '') |
134 |
}); |
135 |
} |
136 |
else { |
137 |
await (msg as Message).react('🔓'); |
138 |
} |
139 |
} |
140 |
} |