/[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 51 - (show annotations)
Mon Jul 29 17:28:23 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6184 byte(s)
Release version 2.0
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 let role1 = await channels.first()!.guild.roles.fetch(client.config.props[channels.first()!.guild.id].gen_role);
11 const gen = await channels.first()!.guild.roles.fetch(client.config.props[channels.first()!.guild.id].gen_role);
12
13 await channels.forEach(async channel => {
14 try {
15 if (send) {
16 await channel.send({
17 embeds: [
18 new MessageEmbed()
19 .setDescription(':lock: This channel has been locked.')
20 ]
21 });
22 }
23
24 let dbPerms;
25 let dbPerms1;
26
27 let overWrites = await channel.permissionOverwrites.cache.get(role.id);
28 let allowperms = await overWrites?.allow?.has(Permissions.FLAGS.SEND_MESSAGES);
29 let denyperms = await overWrites?.deny?.has(Permissions.FLAGS.SEND_MESSAGES);
30
31 let overWrites1 = await channel.permissionOverwrites.cache.get(role1!.id);
32 let allowperms1 = await overWrites1?.allow?.has(Permissions.FLAGS.SEND_MESSAGES);
33 let denyperms1 = await overWrites1?.deny?.has(Permissions.FLAGS.SEND_MESSAGES);
34
35 if (allowperms && !denyperms) {
36 await (dbPerms = 'ALLOW');
37 }
38 else if (!allowperms && denyperms) {
39 await (dbPerms = 'DENY');
40 }
41 else if (!allowperms && !denyperms) {
42 await (dbPerms = 'NULL');
43 }
44
45 if (allowperms1 && !denyperms1) {
46 await (dbPerms1 = 'ALLOW');
47 }
48 else if (!allowperms1 && denyperms1) {
49 await (dbPerms1 = 'DENY');
50 }
51 else if (!allowperms1 && !denyperms1) {
52 await (dbPerms1 = 'NULL');
53 }
54
55 await client.db.get('INSERT INTO locks(channel_id, perms, date) VALUES(?, ?, ?)', [channel.id, dbPerms + ',' + dbPerms1, new Date().toISOString()], async (err: any) => {
56 if (err)
57 console.log(err);
58
59 try {
60 await channel.permissionOverwrites.edit(role, {
61 SEND_MESSAGES: false,
62 });
63 }
64 catch (e) {
65 console.log(e);
66 }
67
68 try {
69 await channel.permissionOverwrites.edit(gen!, {
70 SEND_MESSAGES: false,
71 });
72 }
73 catch (e) {
74 console.log(e);
75 }
76 })
77 }
78 catch (e) {
79 console.log(e);
80 }
81 });
82 }
83 }
84
85 export default class LockallCommand extends BaseCommand {
86 supportsInteractions: boolean = true;
87
88 constructor() {
89 super('lockall', 'moderation', []);
90 }
91
92 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
93 const raid = options.isInteraction ? options.options.getBoolean('raid') === true : (options.options.indexOf('--raid') !== -1);
94
95 let role: Role = <Role> msg.guild!.roles.everyone;
96 // const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
97
98 if (options.isInteraction) {
99 if (options.options.getChannel('role')) {
100 role = await <Role> options.options.getRole('role');
101 }
102 }
103 else {
104 if ((msg as Message).mentions.roles.first()) {
105 role = await <Role> (msg as Message).mentions.roles.first();
106 }
107 else if (options.normalArgs[0] && options.normalArgs[0] !== 'everyone') {
108 role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[0]);
109 }
110
111 if (!role) {
112 await msg.reply({
113 embeds: [
114 new MessageEmbed()
115 .setColor('#f14a60')
116 .setDescription(`Invalid role given.`)
117 ],
118 ephemeral: true
119 });
120
121 return;
122 }
123 }
124
125 const channels: Collection <string, GuildBasedChannel> = await msg.guild!.channels.cache.filter(c => (
126 (!raid && (client.config.props[msg.guild!.id].lockall.indexOf(c.id) !== -1 || client.config.props[msg.guild!.id].lockall.indexOf(c.parent?.id) !== -1)) ||
127 (raid && (
128 (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)) ||
129 (!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))
130 ))) && c.type === 'GUILD_TEXT'
131 );
132 await lockAll(client, role, channels as Collection <string, TextChannel>, true);
133
134 if (options.isInteraction) {
135 await msg.reply({
136 content: "The channels are locked.",
137 ephemeral: true
138 });
139 }
140 else {
141 await (msg as Message).react('🔒');
142 }
143 }
144 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26