/[sudobot]/trunk/src/commands/moderation/SetChPermsCommand.ts
ViewVC logotype

Annotation of /trunk/src/commands/moderation/SetChPermsCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (hide annotations)
Mon Jul 29 17:28:26 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 7823 byte(s)
Fixed slash command issue
1 rakin 62 import { CategoryChannel, CommandInteraction, Message, TextChannel, Permissions, PermissionFlags, PermissionString, GuildChannel, Role } 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    
13     export default class BanCommand extends BaseCommand {
14     supportsInteractions: boolean = true;
15    
16     constructor() {
17     super('setchperms', 'moderation', []);
18     }
19    
20     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
21     if (!options.isInteraction && typeof options.args[3] === 'undefined') {
22     await msg.reply({
23     embeds: [
24     new MessageEmbed()
25     .setColor('#f14a60')
26     .setDescription(`This command requires at least four arguments.`)
27     ]
28     });
29    
30     return;
31     }
32    
33     if (msg instanceof CommandInteraction)
34     await msg.deferReply();
35     else {
36     msg = await msg.reply({
37     embeds: [
38     new MessageEmbed()
39     .setColor('GOLD')
40     .setDescription((await fetchEmoji('loading'))?.toString() + ' Working...')
41     ]
42     });
43     }
44    
45     const { FLAGS } = Permissions;
46    
47     let channels: (TextChannel | CategoryChannel)[] = [];
48     let permKey: PermissionString;
49     let permValue: null | boolean = null;
50     let role: Role;
51    
52     if (options.isInteraction) {
53     channels.push(<TextChannel | CategoryChannel> options.options.getChannel('channel'));
54    
55     if (channels[0].type !== 'GUILD_CATEGORY' && channels[0].type !== 'GUILD_TEXT') {
56     await this.deferReply(msg, {
57     content: (await fetchEmoji('error'))?.toString() + ' The channel with ID ' + (channels[0] as GuildChannel).id + ' is not a text channel or category.',
58     embeds: []
59     }, true);
60    
61     return;
62     }
63    
64     permKey = <PermissionString> options.options.getString('permission');
65 rakin 63
66     if (FLAGS[permKey] === undefined) {
67     await this.deferReply(msg, {
68     content: (await fetchEmoji('error'))?.toString() + ' Invalid permission key given.',
69     embeds: []
70     }, true);
71    
72     return;
73     }
74    
75 rakin 62 const permValueInput = <string> options.options.getString('value');
76    
77     if (permValueInput === 'true')
78     permValue = true;
79     else if (permValueInput === 'false')
80     permValue = false;
81     else
82     permValue = null;
83    
84     role = <Role> options.options.getRole('role');
85     }
86     else {
87     const permValueInput = options.args.pop();
88    
89     if (permValueInput === 'true')
90     permValue = true;
91     else if (permValueInput === 'false')
92     permValue = false;
93     else if (permValueInput === 'null')
94     permValue = null;
95     else {
96     await this.deferReply(msg, {
97     content: (await fetchEmoji('error'))?.toString() + ' Invalid permission value given, permission values must be one of these: `null`, `true`, `false`.',
98     embeds: []
99     }, true);
100    
101     return;
102     }
103    
104     permKey = <PermissionString> options.args.pop();
105    
106     if (FLAGS[permKey] === undefined) {
107     await this.deferReply(msg, {
108     content: (await fetchEmoji('error'))?.toString() + ' Invalid permission key given.',
109     embeds: []
110     }, true);
111    
112     return;
113     }
114    
115     if (options.args[options.args.length - 1] === 'everyone') {
116     role = msg.guild!.roles.everyone;
117     }
118     else {
119     try {
120     role = <Role> await getRoleRaw(options.args[options.args.length - 1], msg.guild!);
121    
122     if (!role)
123     throw new Error();
124     }
125     catch (e) {
126     console.log(e);
127    
128     await this.deferReply(msg, {
129     content: (await fetchEmoji('error'))?.toString() + ' Invalid role given.',
130     embeds: []
131     }, true);
132    
133     return;
134     }
135     }
136    
137     options.args.pop();
138    
139     for await (let chID of options.args) {
140     if (/^\d+$/g.test(chID)) {
141     let channel: CategoryChannel | TextChannel;
142    
143     try {
144     channel = <typeof channel> (await msg.guild!.channels.fetch(chID))!;
145    
146     if (channel.type !== 'GUILD_CATEGORY' && channel.type !== 'GUILD_TEXT') {
147     await this.deferReply(msg, {
148     content: (await fetchEmoji('error'))?.toString() + ' The channel with ID ' + chID + ' is not a text channel or category.',
149     embeds: []
150     }, true);
151    
152     return;
153     }
154    
155     if (channel.type === 'GUILD_CATEGORY') {
156     channels = [...channel.children.filter(c => c.type === 'GUILD_TEXT').toJSON() as TextChannel[], ...channels];
157     continue;
158     }
159     }
160     catch (e) {
161     console.log(e);
162    
163     await this.deferReply(msg, {
164     content: (await fetchEmoji('error'))?.toString() + ' The channel with ID ' + chID + ' could not be fetched.',
165     embeds: []
166     }, true);
167    
168     return;
169     }
170    
171     channels.push(channel);
172     }
173     }
174     }
175    
176     if (FLAGS[permKey] === undefined) {
177     await this.deferReply(msg, {
178     content: (await fetchEmoji('error'))?.toString() + ' Invalid permission key given.',
179     embeds: []
180     }, true);
181    
182     return;
183     }
184    
185     let affected = '';
186    
187     for await (const channel of channels) {
188     try {
189     await channel.permissionOverwrites.edit(role, {
190     [permKey]: permValue
191     });
192    
193     affected += `${channelMention(channel.id)} (${channel.id})\n`;
194     }
195     catch (e) {
196     console.log(e);
197    
198     await this.deferReply(msg, {
199     content: (await fetchEmoji('error'))?.toString() + ' Failed to set permissions for channel ' + channel.id,
200     embeds: []
201     }, true);
202    
203     return;
204     }
205     }
206    
207     await this.deferReply(msg, {
208     embeds: [
209     new MessageEmbed()
210     .setColor('GREEN')
211     .setDescription(`${(await fetchEmoji('check'))?.toString()} Permissions updated!\nThese channels were affected:\n\n${affected}`)
212     ]
213     }, true);
214     }
215     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26