/[sudobot]/branches/3.x/src/commands/moderation/SetChPermsCommand.ts
ViewVC logotype

Contents of /branches/3.x/src/commands/moderation/SetChPermsCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26