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

Annotation of /branches/2.x/src/commands/moderation/SetChPermsCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26