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

Contents of /trunk/src/commands/moderation/LockCommand.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: 5966 byte(s)
Release version 2.0
1 import { BanOptions, CommandInteraction, EmojiIdentifierResolvable, GuildMember, Interaction, Message, Permissions, Role, TextChannel, User } 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 getMember from '../../utils/getMember';
9 import History from '../../automod/History';
10 import { fetchEmoji } from '../../utils/Emoji';
11
12 export default class LockCommand extends BaseCommand {
13 supportsInteractions: boolean = true;
14
15 constructor() {
16 super('lock', 'moderation', []);
17 }
18
19 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
20 let channel: TextChannel = <TextChannel> msg.channel;
21 let role: Role = <Role> msg.guild!.roles.everyone;
22
23 if (options.isInteraction) {
24 if (options.options.getChannel('channel')) {
25 channel = await <TextChannel> options.options.getChannel('channel');
26 }
27
28 if (options.options.getChannel('role')) {
29 role = await <Role> options.options.getRole('role');
30 }
31 }
32 else {
33 if ((msg as Message).mentions.roles.first()) {
34 role = await <Role> (msg as Message).mentions.roles.first();
35 }
36 else if (options.normalArgs[0] && options.normalArgs[0] !== 'everyone') {
37 role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[0]);
38 }
39
40 if ((msg as Message).mentions.channels.first()) {
41 channel = await <TextChannel> (msg as Message).mentions.channels.first();
42 }
43 else if (options.normalArgs[1]) {
44 channel = <TextChannel> await (msg as Message).guild?.channels.fetch(options.normalArgs[1]);
45 }
46
47 if (!role) {
48 await msg.reply({
49 embeds: [
50 new MessageEmbed()
51 .setColor('#f14a60')
52 .setDescription(`Invalid role given.`)
53 ],
54 ephemeral: true
55 });
56
57 return;
58 }
59
60 if (!channel || channel.type !== 'GUILD_TEXT') {
61 await msg.reply({
62 embeds: [
63 new MessageEmbed()
64 .setColor('#f14a60')
65 .setDescription(`Invalid text channel given.`)
66 ],
67 ephemeral: true
68 });
69
70 return;
71 }
72 }
73
74 try {
75 let dbPerms;
76 let dbPerms1;
77
78 let overWrites = await channel.permissionOverwrites.cache.get(role.id);
79 let allowperms = await overWrites?.allow?.has(Permissions.FLAGS.SEND_MESSAGES);
80 let denyperms = await overWrites?.deny?.has(Permissions.FLAGS.SEND_MESSAGES);
81
82 let role1 = await channel.guild.roles.fetch(client.config.props[channel.guild.id].gen_role);
83
84 let overWrites1 = await channel.permissionOverwrites.cache.get(role1!.id);
85 let allowperms1 = await overWrites1?.allow?.has(Permissions.FLAGS.SEND_MESSAGES);
86 let denyperms1 = await overWrites1?.deny?.has(Permissions.FLAGS.SEND_MESSAGES);
87
88 if (allowperms && !denyperms) {
89 await (dbPerms = 'ALLOW');
90 }
91 else if (!allowperms && denyperms) {
92 await (dbPerms = 'DENY');
93 }
94 else if (!allowperms && !denyperms) {
95 await (dbPerms = 'NULL');
96 }
97
98 if (allowperms1 && !denyperms1) {
99 await (dbPerms1 = 'ALLOW');
100 }
101 else if (!allowperms1 && denyperms1) {
102 await (dbPerms1 = 'DENY');
103 }
104 else if (!allowperms1 && !denyperms1) {
105 await (dbPerms1 = 'NULL');
106 }
107
108 await client.db.get('INSERT INTO locks(channel_id, perms, date) VALUES(?, ?, ?)', [channel.id, dbPerms + ',' + dbPerms1, new Date().toISOString()], async (err: any) => {
109 if (err)
110 console.log(err);
111
112 try {
113 await channel.permissionOverwrites.edit(role, {
114 SEND_MESSAGES: false,
115 });
116 }
117 catch (e) {
118 console.log(e);
119 }
120
121 try {
122 await channel.permissionOverwrites.edit(role1!, {
123 SEND_MESSAGES: false,
124 });
125 }
126 catch (e) {
127 console.log(e);
128 }
129 })
130
131 await channel.send({
132 embeds: [
133 new MessageEmbed()
134 .setColor('#007bff')
135 .setDescription(`:lock: This channel has been locked.`)
136 ]
137 });
138
139 if (options.isInteraction) {
140 await msg.reply({
141 content: "Channel locked.",
142 ephemeral: true
143 });
144 }
145 else {
146 await (msg as Message).react('🔒');
147 }
148 }
149 catch (e) {
150 console.log(e);
151
152 await msg.reply({
153 embeds: [
154 new MessageEmbed()
155 .setColor('#f14a60')
156 .setDescription(`Failed to lock channel. Maybe missing permissions?`)
157 ],
158 ephemeral: true
159 });
160
161 return;
162 }
163 }
164 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26