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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (show annotations)
Mon Jul 29 17:28:26 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 7515 byte(s)
Add -setchperms command
1 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 const permValueInput = <string> options.options.getString('value');
66
67 if (permValueInput === 'true')
68 permValue = true;
69 else if (permValueInput === 'false')
70 permValue = false;
71 else
72 permValue = null;
73
74 role = <Role> options.options.getRole('role');
75 }
76 else {
77 const permValueInput = options.args.pop();
78
79 if (permValueInput === 'true')
80 permValue = true;
81 else if (permValueInput === 'false')
82 permValue = false;
83 else if (permValueInput === 'null')
84 permValue = null;
85 else {
86 await this.deferReply(msg, {
87 content: (await fetchEmoji('error'))?.toString() + ' Invalid permission value given, permission values must be one of these: `null`, `true`, `false`.',
88 embeds: []
89 }, true);
90
91 return;
92 }
93
94 permKey = <PermissionString> options.args.pop();
95
96 if (FLAGS[permKey] === undefined) {
97 await this.deferReply(msg, {
98 content: (await fetchEmoji('error'))?.toString() + ' Invalid permission key given.',
99 embeds: []
100 }, true);
101
102 return;
103 }
104
105 if (options.args[options.args.length - 1] === 'everyone') {
106 role = msg.guild!.roles.everyone;
107 }
108 else {
109 try {
110 role = <Role> await getRoleRaw(options.args[options.args.length - 1], msg.guild!);
111
112 if (!role)
113 throw new Error();
114 }
115 catch (e) {
116 console.log(e);
117
118 await this.deferReply(msg, {
119 content: (await fetchEmoji('error'))?.toString() + ' Invalid role given.',
120 embeds: []
121 }, true);
122
123 return;
124 }
125 }
126
127 options.args.pop();
128
129 for await (let chID of options.args) {
130 if (/^\d+$/g.test(chID)) {
131 let channel: CategoryChannel | TextChannel;
132
133 try {
134 channel = <typeof channel> (await msg.guild!.channels.fetch(chID))!;
135
136 if (channel.type !== 'GUILD_CATEGORY' && channel.type !== 'GUILD_TEXT') {
137 await this.deferReply(msg, {
138 content: (await fetchEmoji('error'))?.toString() + ' The channel with ID ' + chID + ' is not a text channel or category.',
139 embeds: []
140 }, true);
141
142 return;
143 }
144
145 if (channel.type === 'GUILD_CATEGORY') {
146 channels = [...channel.children.filter(c => c.type === 'GUILD_TEXT').toJSON() as TextChannel[], ...channels];
147 continue;
148 }
149 }
150 catch (e) {
151 console.log(e);
152
153 await this.deferReply(msg, {
154 content: (await fetchEmoji('error'))?.toString() + ' The channel with ID ' + chID + ' could not be fetched.',
155 embeds: []
156 }, true);
157
158 return;
159 }
160
161 channels.push(channel);
162 }
163 }
164 }
165
166 if (FLAGS[permKey] === undefined) {
167 await this.deferReply(msg, {
168 content: (await fetchEmoji('error'))?.toString() + ' Invalid permission key given.',
169 embeds: []
170 }, true);
171
172 return;
173 }
174
175 let affected = '';
176
177 for await (const channel of channels) {
178 try {
179 await channel.permissionOverwrites.edit(role, {
180 [permKey]: permValue
181 });
182
183 affected += `${channelMention(channel.id)} (${channel.id})\n`;
184 }
185 catch (e) {
186 console.log(e);
187
188 await this.deferReply(msg, {
189 content: (await fetchEmoji('error'))?.toString() + ' Failed to set permissions for channel ' + channel.id,
190 embeds: []
191 }, true);
192
193 return;
194 }
195 }
196
197 await this.deferReply(msg, {
198 embeds: [
199 new MessageEmbed()
200 .setColor('GREEN')
201 .setDescription(`${(await fetchEmoji('check'))?.toString()} Permissions updated!\nThese channels were affected:\n\n${affected}`)
202 ]
203 }, true);
204 }
205 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26