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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 107 - (show annotations)
Mon Jul 29 17:28:37 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6410 byte(s)
Updated help information
1 import { Collection, CommandInteraction, GuildBasedChannel, GuildChannel, Message, Permissions, Role, TextChannel } from 'discord.js';
2 import BaseCommand from '../../utils/structures/BaseCommand';
3 import CommandOptions from '../../types/CommandOptions';
4 import InteractionOptions from '../../types/InteractionOptions';
5 import DiscordClient from '../../client/Client';
6 import MessageEmbed from '../../client/MessageEmbed';
7
8 export async function lockAll(client: DiscordClient, role: Role, channels: Collection <string, TextChannel>, send: boolean = true) {
9 if (role) {
10 // const gen = await channels.first()!.guild.roles.fetch(client.config.props[channels.first()!.guild.id].gen_role);
11
12 await channels.forEach(async channel => {
13 try {
14 if (send) {
15 await channel.send({
16 embeds: [
17 new MessageEmbed()
18 .setDescription(':lock: This channel has been locked.')
19 ]
20 });
21 }
22
23 let dbPerms;
24
25 let overWrites = await channel.permissionOverwrites.cache.get(role.id);
26 let allowperms = await overWrites?.allow?.has(Permissions.FLAGS.SEND_MESSAGES);
27 let denyperms = await overWrites?.deny?.has(Permissions.FLAGS.SEND_MESSAGES);
28
29 if (allowperms && !denyperms) {
30 await (dbPerms = 'ALLOW');
31 }
32 else if (!allowperms && denyperms) {
33 await (dbPerms = 'DENY');
34 }
35 else if (!allowperms && !denyperms) {
36 await (dbPerms = 'NULL');
37 }
38
39 console.log(dbPerms);
40
41
42 await client.db.get('INSERT INTO locks(channel_id, perms, date) VALUES(?, ?, ?)', [channel.id, dbPerms, new Date().toISOString()], async (err: any) => {
43 if (err)
44 console.log(err);
45
46 try {
47 await channel.permissionOverwrites.edit(role, {
48 SEND_MESSAGES: false,
49 });
50 }
51 catch (e) {
52 console.log(e);
53 }
54 })
55 }
56 catch (e) {
57 console.log(e);
58 }
59 });
60 }
61 }
62
63 export default class LockallCommand extends BaseCommand {
64 supportsInteractions: boolean = true;
65
66 constructor() {
67 super('lockall', 'moderation', []);
68 }
69
70 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
71 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
72 await msg.reply({
73 embeds: [
74 new MessageEmbed()
75 .setColor('#f14a60')
76 .setDescription(`This command requires at least one argument.`)
77 ]
78 });
79
80 return;
81 }
82
83 const raid = options.isInteraction ? options.options.getBoolean('raid') === true : (options.options.indexOf('--raid') !== -1);
84
85 let role: Role = <Role> msg.guild!.roles.everyone;
86 let lockall: string[] = [], lockallChannels: Collection<string, TextChannel> = new Collection();
87 // const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
88
89 if (options.isInteraction) {
90 lockall = options.options.getString('channels')!.split(' ');
91
92 if (options.options.getChannel('role')) {
93 role = await <Role> options.options.getRole('role');
94 }
95 }
96 else {
97 if ((msg as Message).mentions.roles.first()) {
98 role = await <Role> (msg as Message).mentions.roles.first();
99 }
100 else if (options.options.includes('-r') && options.normalArgs[options.options.indexOf('-r') + 1]) {
101 role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[options.options.indexOf('-r') + 1]);
102 }
103
104 if (!role) {
105 await msg.reply({
106 embeds: [
107 new MessageEmbed()
108 .setColor('#f14a60')
109 .setDescription(`Invalid role given.`)
110 ],
111 ephemeral: true
112 });
113
114 return;
115 }
116
117 if (!raid) {
118 for (const a of options.args) {
119 if (/^\d+$/g.test(a)) {
120 lockall.push(a);
121 }
122 }
123
124 if ((msg as Message).mentions.channels.first()) {
125 (msg as Message).mentions.channels.forEach(c => {
126 if (c instanceof TextChannel)
127 lockallChannels.set(c.id, c);
128 });
129 }
130 }
131 }
132
133 let channels = raid ? await msg.guild!.channels.cache.filter(c => (
134 (raid && (
135 (client.config.props[msg.guild!.id].raid.exclude && (client.config.props[msg.guild!.id].raid.channels.indexOf(c.id) === -1 && client.config.props[msg.guild!.id].raid.channels.indexOf(c.parent?.id) === -1)) ||
136 (!client.config.props[msg.guild!.id].raid.exclude && (client.config.props[msg.guild!.id].raid.channels.indexOf(c.id) !== -1 || client.config.props[msg.guild!.id].raid.channels.indexOf(c.parent?.id) !== -1))
137 ))) && c.type === 'GUILD_TEXT'
138 ) : null;
139
140 if (channels === null && !raid) {
141 channels = msg.guild!.channels.cache.filter(c2 => (lockall.includes(c2.id) || lockall.includes(c2.parent?.id!)) && c2.type === 'GUILD_TEXT')!;
142 channels = channels.merge(lockallChannels, c => ({ keep: true, value: c }), c => ({ keep: true, value: c }), (c1, c2) => ({ keep: true, value: c2 }));
143 }
144
145 await lockAll(client, role, channels as Collection <string, TextChannel>, true);
146
147 if (options.isInteraction) {
148 await msg.reply({
149 content: "The channels are locked.",
150 ephemeral: true
151 });
152 }
153 else {
154 await (msg as Message).react('🔒');
155 }
156 }
157 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26