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 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 |
await client.db.get('SELECT * FROM locks WHERE channel_id = ?', [channel.id], async (err: any, data: any) => { |
77 |
if (data || force) { |
78 |
let perm; |
79 |
const data1 = data?.perms; |
80 |
|
81 |
if (data1) { |
82 |
if (data1 === 'DENY') { |
83 |
await (perm = false); |
84 |
} |
85 |
else if (data1 === 'NULL') { |
86 |
await (perm = null); |
87 |
} |
88 |
else if (data1 === 'ALLOW') { |
89 |
await (perm = true); |
90 |
} |
91 |
} |
92 |
|
93 |
console.log(data1, perm); |
94 |
|
95 |
if (force) { |
96 |
await (perm = true); |
97 |
} |
98 |
|
99 |
await console.log(channel.name, role.name); |
100 |
|
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 |
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 |
}); |
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 |
} |