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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 103 - (hide annotations)
Mon Jul 29 17:28:36 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5884 byte(s)
Improved channel locking
1 rakin 51 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 UnlockCommand extends BaseCommand {
13     supportsInteractions: boolean = true;
14    
15     constructor() {
16     super('unlock', '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     const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1);
23    
24     if (options.isInteraction) {
25     if (options.options.getChannel('channel')) {
26     channel = await <TextChannel> options.options.getChannel('channel');
27     }
28    
29     if (options.options.getChannel('role')) {
30     role = await <Role> options.options.getRole('role');
31     }
32     }
33     else {
34     if ((msg as Message).mentions.roles.first()) {
35     role = await <Role> (msg as Message).mentions.roles.first();
36     }
37     else if (options.normalArgs[0] && options.normalArgs[0] !== 'everyone') {
38     role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[0]);
39     }
40    
41     if ((msg as Message).mentions.channels.first()) {
42     channel = await <TextChannel> (msg as Message).mentions.channels.first();
43     }
44     else if (options.normalArgs[1]) {
45     channel = <TextChannel> await (msg as Message).guild?.channels.fetch(options.normalArgs[1]);
46     }
47    
48     if (!role) {
49     await msg.reply({
50     embeds: [
51     new MessageEmbed()
52     .setColor('#f14a60')
53     .setDescription(`Invalid role given.`)
54     ],
55     ephemeral: true
56     });
57    
58     return;
59     }
60    
61     if (!channel || channel.type !== 'GUILD_TEXT') {
62     await msg.reply({
63     embeds: [
64     new MessageEmbed()
65     .setColor('#f14a60')
66     .setDescription(`Invalid text channel given.`)
67     ],
68     ephemeral: true
69     });
70    
71     return;
72     }
73     }
74    
75     try {
76 rakin 103 await client.db.get('SELECT * FROM locks WHERE channel_id = ?', [channel.id], async (err: any, data: any) => {
77 rakin 51 if (data || force) {
78     let perm;
79 rakin 103 const data1 = data?.perms;
80 rakin 51
81     if (data1) {
82 rakin 103 if (data1 === 'DENY') {
83 rakin 51 await (perm = false);
84     }
85 rakin 103 else if (data1 === 'NULL') {
86 rakin 51 await (perm = null);
87     }
88 rakin 103 else if (data1 === 'ALLOW') {
89 rakin 51 await (perm = true);
90     }
91 rakin 103 }
92 rakin 51
93 rakin 103 console.log(data1, perm);
94 rakin 51
95     if (force) {
96     await (perm = true);
97     }
98    
99 rakin 103 await console.log(channel.name, role.name);
100 rakin 51
101     try {
102     await channel.permissionOverwrites.edit(role, {
103     SEND_MESSAGES: perm,
104     });
105     }
106     catch (e) {
107     console.log(e);
108     }
109    
110     if (data) {
111     await client.db.get('DELETE FROM locks WHERE id = ?', [data?.id], async (err: any) => {});
112     }
113     }
114 rakin 103 else {
115     await msg.reply({
116     embeds: [
117     new MessageEmbed()
118     .setColor('#007bff')
119     .setDescription(`:closed_lock_with_key: This channel wasn't locked.`)
120     ],
121     ephemeral: true
122     });
123    
124     return;
125     }
126    
127     if (options.isInteraction) {
128     await msg.reply({
129     content: "Channel unlocked.",
130     ephemeral: true
131     });
132     }
133     else {
134     await (msg as Message).react('🔓');
135     }
136    
137     await channel.send({
138     embeds: [
139     new MessageEmbed()
140     .setColor('#007bff')
141     .setDescription(`:closed_lock_with_key: This channel has been unlocked.`)
142     ]
143 rakin 51 });
144     });
145     }
146     catch (e) {
147     console.log(e);
148    
149     await msg.reply({
150     embeds: [
151     new MessageEmbed()
152     .setColor('#f14a60')
153     .setDescription(`Failed to unlock channel. Maybe missing permissions?`)
154     ],
155     ephemeral: true
156     });
157    
158     return;
159     }
160     }
161     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26