1 |
rakin |
64 |
import { CategoryChannel, CommandInteraction, Message, TextChannel, Permissions, PermissionFlags, PermissionString, GuildChannel, Role, AutocompleteInteraction, Interaction, Collection } from 'discord.js'; |
2 |
rakin |
62 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
|
|
import DiscordClient from '../../client/Client'; |
4 |
|
|
import CommandOptions from '../../types/CommandOptions'; |
5 |
|
|
import InteractionOptions from '../../types/InteractionOptions'; |
6 |
|
|
import MessageEmbed from '../../client/MessageEmbed'; |
7 |
|
|
import getUser from '../../utils/getUser'; |
8 |
|
|
import History from '../../automod/History'; |
9 |
|
|
import { fetchEmoji } from '../../utils/Emoji'; |
10 |
|
|
import getRole, { getRoleRaw } from '../../utils/getRole'; |
11 |
|
|
import { channelMention } from '@discordjs/builders'; |
12 |
rakin |
64 |
import AutoCompleteOptions from '../../types/AutoCompleteOptions'; |
13 |
rakin |
62 |
|
14 |
rakin |
64 |
export default class SetChPermsCommand extends BaseCommand { |
15 |
rakin |
62 |
supportsInteractions: boolean = true; |
16 |
|
|
|
17 |
|
|
constructor() { |
18 |
|
|
super('setchperms', 'moderation', []); |
19 |
|
|
} |
20 |
|
|
|
21 |
rakin |
64 |
async autoComplete(client: DiscordClient, interaction: AutocompleteInteraction, options: AutoCompleteOptions) { |
22 |
|
|
if (interaction.commandName === this.getName()) { |
23 |
|
|
const focused = interaction.options.getFocused(true); |
24 |
|
|
|
25 |
|
|
console.log(focused); |
26 |
|
|
|
27 |
|
|
if (focused.name === 'permission') { |
28 |
|
|
const { FLAGS } = Permissions; |
29 |
|
|
const responseArray = []; |
30 |
|
|
const perms: (keyof typeof FLAGS)[] = [ |
31 |
|
|
'SEND_MESSAGES', |
32 |
|
|
'ATTACH_FILES', |
33 |
|
|
'EMBED_LINKS', |
34 |
|
|
'MANAGE_MESSAGES', |
35 |
|
|
'MENTION_EVERYONE', |
36 |
|
|
'USE_APPLICATION_COMMANDS', |
37 |
|
|
'USE_EXTERNAL_EMOJIS', |
38 |
|
|
'USE_EXTERNAL_STICKERS' |
39 |
|
|
]; |
40 |
|
|
|
41 |
|
|
for await (const key of perms) { |
42 |
|
|
if (key.includes(focused.value.toString())) { |
43 |
|
|
responseArray.push({ |
44 |
|
|
name: key, |
45 |
|
|
value: key |
46 |
|
|
}); |
47 |
|
|
} |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
console.log(responseArray); |
51 |
|
|
|
52 |
|
|
await interaction.respond(responseArray); |
53 |
|
|
} |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
58 |
rakin |
62 |
if (!options.isInteraction && typeof options.args[3] === 'undefined') { |
59 |
|
|
await msg.reply({ |
60 |
|
|
embeds: [ |
61 |
|
|
new MessageEmbed() |
62 |
|
|
.setColor('#f14a60') |
63 |
|
|
.setDescription(`This command requires at least four arguments.`) |
64 |
|
|
] |
65 |
|
|
}); |
66 |
|
|
|
67 |
|
|
return; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
if (msg instanceof CommandInteraction) |
71 |
|
|
await msg.deferReply(); |
72 |
|
|
else { |
73 |
|
|
msg = await msg.reply({ |
74 |
|
|
embeds: [ |
75 |
|
|
new MessageEmbed() |
76 |
|
|
.setColor('GOLD') |
77 |
|
|
.setDescription((await fetchEmoji('loading'))?.toString() + ' Working...') |
78 |
|
|
] |
79 |
|
|
}); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
const { FLAGS } = Permissions; |
83 |
|
|
|
84 |
|
|
let channels: (TextChannel | CategoryChannel)[] = []; |
85 |
|
|
let permKey: PermissionString; |
86 |
|
|
let permValue: null | boolean = null; |
87 |
|
|
let role: Role; |
88 |
|
|
|
89 |
|
|
if (options.isInteraction) { |
90 |
|
|
channels.push(<TextChannel | CategoryChannel> options.options.getChannel('channel')); |
91 |
|
|
|
92 |
|
|
if (channels[0].type !== 'GUILD_CATEGORY' && channels[0].type !== 'GUILD_TEXT') { |
93 |
|
|
await this.deferReply(msg, { |
94 |
|
|
content: (await fetchEmoji('error'))?.toString() + ' The channel with ID ' + (channels[0] as GuildChannel).id + ' is not a text channel or category.', |
95 |
|
|
embeds: [] |
96 |
|
|
}, true); |
97 |
|
|
|
98 |
|
|
return; |
99 |
|
|
} |
100 |
rakin |
64 |
else if (channels[0].type === 'GUILD_CATEGORY') { |
101 |
|
|
const ch = channels[0]; |
102 |
|
|
channels = []; |
103 |
rakin |
62 |
|
104 |
rakin |
64 |
const matching = <Collection<string, TextChannel>> await msg.guild!.channels.cache.filter(c => c.parent?.id === ch.id && c.type === 'GUILD_TEXT'); |
105 |
|
|
channels = matching.toJSON(); |
106 |
|
|
} |
107 |
|
|
|
108 |
rakin |
62 |
permKey = <PermissionString> options.options.getString('permission'); |
109 |
rakin |
63 |
|
110 |
|
|
if (FLAGS[permKey] === undefined) { |
111 |
|
|
await this.deferReply(msg, { |
112 |
|
|
content: (await fetchEmoji('error'))?.toString() + ' Invalid permission key given.', |
113 |
|
|
embeds: [] |
114 |
|
|
}, true); |
115 |
|
|
|
116 |
|
|
return; |
117 |
|
|
} |
118 |
|
|
|
119 |
rakin |
62 |
const permValueInput = <string> options.options.getString('value'); |
120 |
|
|
|
121 |
|
|
if (permValueInput === 'true') |
122 |
|
|
permValue = true; |
123 |
|
|
else if (permValueInput === 'false') |
124 |
|
|
permValue = false; |
125 |
|
|
else |
126 |
|
|
permValue = null; |
127 |
|
|
|
128 |
|
|
role = <Role> options.options.getRole('role'); |
129 |
|
|
} |
130 |
|
|
else { |
131 |
|
|
const permValueInput = options.args.pop(); |
132 |
|
|
|
133 |
|
|
if (permValueInput === 'true') |
134 |
|
|
permValue = true; |
135 |
|
|
else if (permValueInput === 'false') |
136 |
|
|
permValue = false; |
137 |
|
|
else if (permValueInput === 'null') |
138 |
|
|
permValue = null; |
139 |
|
|
else { |
140 |
|
|
await this.deferReply(msg, { |
141 |
|
|
content: (await fetchEmoji('error'))?.toString() + ' Invalid permission value given, permission values must be one of these: `null`, `true`, `false`.', |
142 |
|
|
embeds: [] |
143 |
|
|
}, true); |
144 |
|
|
|
145 |
|
|
return; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
permKey = <PermissionString> options.args.pop(); |
149 |
|
|
|
150 |
|
|
if (FLAGS[permKey] === undefined) { |
151 |
|
|
await this.deferReply(msg, { |
152 |
|
|
content: (await fetchEmoji('error'))?.toString() + ' Invalid permission key given.', |
153 |
|
|
embeds: [] |
154 |
|
|
}, true); |
155 |
|
|
|
156 |
|
|
return; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
if (options.args[options.args.length - 1] === 'everyone') { |
160 |
|
|
role = msg.guild!.roles.everyone; |
161 |
|
|
} |
162 |
|
|
else { |
163 |
|
|
try { |
164 |
|
|
role = <Role> await getRoleRaw(options.args[options.args.length - 1], msg.guild!); |
165 |
|
|
|
166 |
|
|
if (!role) |
167 |
|
|
throw new Error(); |
168 |
|
|
} |
169 |
|
|
catch (e) { |
170 |
|
|
console.log(e); |
171 |
|
|
|
172 |
|
|
await this.deferReply(msg, { |
173 |
|
|
content: (await fetchEmoji('error'))?.toString() + ' Invalid role given.', |
174 |
|
|
embeds: [] |
175 |
|
|
}, true); |
176 |
|
|
|
177 |
|
|
return; |
178 |
|
|
} |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
options.args.pop(); |
182 |
|
|
|
183 |
|
|
for await (let chID of options.args) { |
184 |
|
|
if (/^\d+$/g.test(chID)) { |
185 |
|
|
let channel: CategoryChannel | TextChannel; |
186 |
|
|
|
187 |
|
|
try { |
188 |
|
|
channel = <typeof channel> (await msg.guild!.channels.fetch(chID))!; |
189 |
|
|
|
190 |
|
|
if (channel.type !== 'GUILD_CATEGORY' && channel.type !== 'GUILD_TEXT') { |
191 |
|
|
await this.deferReply(msg, { |
192 |
|
|
content: (await fetchEmoji('error'))?.toString() + ' The channel with ID ' + chID + ' is not a text channel or category.', |
193 |
|
|
embeds: [] |
194 |
|
|
}, true); |
195 |
|
|
|
196 |
|
|
return; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
if (channel.type === 'GUILD_CATEGORY') { |
200 |
|
|
channels = [...channel.children.filter(c => c.type === 'GUILD_TEXT').toJSON() as TextChannel[], ...channels]; |
201 |
|
|
continue; |
202 |
|
|
} |
203 |
|
|
} |
204 |
|
|
catch (e) { |
205 |
|
|
console.log(e); |
206 |
|
|
|
207 |
|
|
await this.deferReply(msg, { |
208 |
|
|
content: (await fetchEmoji('error'))?.toString() + ' The channel with ID ' + chID + ' could not be fetched.', |
209 |
|
|
embeds: [] |
210 |
|
|
}, true); |
211 |
|
|
|
212 |
|
|
return; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
channels.push(channel); |
216 |
|
|
} |
217 |
|
|
} |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
if (FLAGS[permKey] === undefined) { |
221 |
|
|
await this.deferReply(msg, { |
222 |
|
|
content: (await fetchEmoji('error'))?.toString() + ' Invalid permission key given.', |
223 |
|
|
embeds: [] |
224 |
|
|
}, true); |
225 |
|
|
|
226 |
|
|
return; |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
let affected = ''; |
230 |
|
|
|
231 |
|
|
for await (const channel of channels) { |
232 |
|
|
try { |
233 |
|
|
await channel.permissionOverwrites.edit(role, { |
234 |
|
|
[permKey]: permValue |
235 |
|
|
}); |
236 |
|
|
|
237 |
|
|
affected += `${channelMention(channel.id)} (${channel.id})\n`; |
238 |
|
|
} |
239 |
|
|
catch (e) { |
240 |
|
|
console.log(e); |
241 |
|
|
|
242 |
|
|
await this.deferReply(msg, { |
243 |
|
|
content: (await fetchEmoji('error'))?.toString() + ' Failed to set permissions for channel ' + channel.id, |
244 |
|
|
embeds: [] |
245 |
|
|
}, true); |
246 |
|
|
|
247 |
|
|
return; |
248 |
|
|
} |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
await this.deferReply(msg, { |
252 |
|
|
embeds: [ |
253 |
|
|
new MessageEmbed() |
254 |
|
|
.setColor('GREEN') |
255 |
|
|
.setDescription(`${(await fetchEmoji('check'))?.toString()} Permissions updated!\nThese channels were affected:\n\n${affected}`) |
256 |
|
|
] |
257 |
|
|
}, true); |
258 |
|
|
} |
259 |
|
|
} |