1 |
import { GuildChannel, PermissionString, User } from "discord.js"; |
import { GuildChannel, PermissionString, Role, RoleResolvable, User } from "discord.js"; |
2 |
import MessageEmbed from "../client/MessageEmbed"; |
import MessageEmbed from "../client/MessageEmbed"; |
3 |
import ChannelLock from "../models/ChannelLock"; |
import ChannelLock from "../models/ChannelLock"; |
4 |
import Service from "../utils/structures/Service"; |
import Service from "../utils/structures/Service"; |
7 |
reason?: string; |
reason?: string; |
8 |
sendConfirmation?: boolean; |
sendConfirmation?: boolean; |
9 |
force?: boolean; |
force?: boolean; |
10 |
|
role?: RoleResolvable; |
11 |
} |
} |
12 |
|
|
13 |
export default class ChannelLockManager extends Service { |
export default class ChannelLockManager extends Service { |
14 |
async lock(channel: GuildChannel, user: User, { reason, sendConfirmation }: ChannelLockOptions = {}) { |
async lock(channel: GuildChannel, user: User, { reason, sendConfirmation, role }: ChannelLockOptions = {}) { |
15 |
|
const lockRole = role ? (role instanceof Role ? role : (await channel.guild.roles.fetch(role))!) : channel.guild.roles.everyone; |
16 |
|
|
17 |
const channelLock = await ChannelLock.findOne({ |
const channelLock = await ChannelLock.findOne({ |
18 |
where: { |
where: { |
19 |
channel_id: channel.id, |
channel_id: channel.id, |
20 |
guild_id: channel.guild.id |
guild_id: channel.guild.id, |
21 |
|
role_id: lockRole.id |
22 |
} |
} |
23 |
}); |
}); |
24 |
|
|
25 |
if (channelLock) { |
if (channelLock) { |
26 |
|
console.log('exists'); |
27 |
return false; |
return false; |
28 |
} |
} |
29 |
|
|
30 |
const permissions = channel.permissionOverwrites.cache.get(channel.guild.id); |
console.log(lockRole?.name); |
|
|
|
|
if (permissions) { |
|
|
const permJson = { |
|
|
allow: permissions.allow.toArray(), |
|
|
deny: permissions.deny.toArray(), |
|
|
}; |
|
31 |
|
|
32 |
console.log(permJson); |
let permissions = channel.permissionOverwrites.cache.get(lockRole.id); |
33 |
|
|
34 |
await ChannelLock.create({ |
const permJson = { |
35 |
user_id: user.id, |
allow: permissions?.allow?.toArray() ?? null, |
36 |
guild_id: channel.guild.id, |
deny: permissions?.deny?.toArray() ?? null, |
37 |
channel_id: channel.id, |
}; |
38 |
reason, |
|
39 |
previous_perms: permJson |
console.log(permJson); |
40 |
}); |
|
41 |
} |
await ChannelLock.create({ |
42 |
|
user_id: user.id, |
43 |
|
guild_id: channel.guild.id, |
44 |
|
channel_id: channel.id, |
45 |
|
reason, |
46 |
|
previous_perms: permJson, |
47 |
|
role_id: lockRole.id, |
48 |
|
createdAt: new Date() |
49 |
|
}); |
50 |
|
|
51 |
await channel.permissionOverwrites.edit(channel.guild.id, { |
await channel.permissionOverwrites.edit(lockRole, { |
52 |
SEND_MESSAGES: false, |
SEND_MESSAGES: false, |
53 |
SEND_MESSAGES_IN_THREADS: false, |
SEND_MESSAGES_IN_THREADS: false, |
|
ADD_REACTIONS: false, |
|
54 |
REQUEST_TO_SPEAK: false, |
REQUEST_TO_SPEAK: false, |
55 |
SPEAK: false, |
SPEAK: false, |
56 |
}, { reason }); |
}, { reason }); |
57 |
|
|
58 |
|
console.log('success'); |
59 |
|
|
60 |
if (sendConfirmation && channel.isText()) { |
if (sendConfirmation && channel.isText()) { |
61 |
await channel.send({ |
await channel.send({ |
62 |
embeds: [ |
embeds: [ |
79 |
return true; |
return true; |
80 |
} |
} |
81 |
|
|
82 |
async unlock(channel: GuildChannel, { reason, sendConfirmation, force }: ChannelLockOptions = {}) { |
async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) { |
83 |
|
const lockRole = role ? (role instanceof Role ? role : (await channel.guild.roles.fetch(role))!) : channel.guild.roles.everyone; |
84 |
|
|
85 |
const channelLock = await ChannelLock.findOne({ |
const channelLock = await ChannelLock.findOne({ |
86 |
where: { |
channel_id: channel.id, |
87 |
channel_id: channel.id, |
guild_id: channel.guild.id, |
88 |
guild_id: channel.guild.id |
role_id: lockRole.id |
|
} |
|
89 |
}); |
}); |
90 |
|
|
91 |
const permissions = channelLock?.get('previous_perms') as { allow: PermissionString[], deny: PermissionString[] }; |
if (!channelLock) { |
92 |
|
console.log('Channel not locked'); |
93 |
|
return false; |
94 |
|
} |
95 |
|
|
96 |
|
const permissions = channelLock?.previous_perms; // as { allow: PermissionString[] | null, deny: PermissionString[] | null } |
97 |
|
|
98 |
if (!permissions && !force) { |
if (!permissions && !force) { |
99 |
|
console.log('Permission error'); |
100 |
return false; |
return false; |
101 |
} |
} |
102 |
|
|
103 |
const transform = (key: PermissionString) => { |
const transform = (key: PermissionString) => { |
104 |
|
if (!permissions?.allow || !permissions?.deny) { |
105 |
|
return undefined; |
106 |
|
} |
107 |
|
|
108 |
if (!permissions) { |
if (!permissions) { |
109 |
return force ? true : undefined; |
return force ? true : undefined; |
110 |
} |
} |
120 |
} |
} |
121 |
}; |
}; |
122 |
|
|
123 |
await channel.permissionOverwrites.edit(channel.guild.id, { |
if (!permissions?.allow && !permissions?.deny) { |
124 |
SEND_MESSAGES: transform('SEND_MESSAGES'), |
await channel.permissionOverwrites.delete(lockRole); |
125 |
SEND_MESSAGES_IN_THREADS: transform('SEND_MESSAGES_IN_THREADS'), |
} |
126 |
ADD_REACTIONS: transform('ADD_REACTIONS'), |
else { |
127 |
REQUEST_TO_SPEAK: transform('REQUEST_TO_SPEAK'), |
await channel.permissionOverwrites.edit(lockRole, { |
128 |
SPEAK: transform('SPEAK'), |
SEND_MESSAGES: transform('SEND_MESSAGES'), |
129 |
}, { reason }); |
SEND_MESSAGES_IN_THREADS: transform('SEND_MESSAGES_IN_THREADS'), |
130 |
|
REQUEST_TO_SPEAK: transform('REQUEST_TO_SPEAK'), |
131 |
|
SPEAK: transform('SPEAK'), |
132 |
|
}, { reason }); |
133 |
|
} |
134 |
|
|
135 |
await channelLock?.destroy(); |
await channelLock?.delete(); |
136 |
|
|
137 |
if (sendConfirmation && channel.isText()) { |
if (sendConfirmation && channel.isText()) { |
138 |
await channel.send({ |
await channel.send({ |
156 |
return true; |
return true; |
157 |
} |
} |
158 |
|
|
159 |
lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) { |
async lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) { |
160 |
return Promise.all(channels.map(c => this.lock(c, user, options))); |
let success = 0, failure = 0; |
161 |
|
|
162 |
|
for await (const channel of channels) { |
163 |
|
console.log('Locking', channel.name); |
164 |
|
|
165 |
|
if (await this.lock(channel, user, options)) { |
166 |
|
success++; |
167 |
|
} |
168 |
|
else { |
169 |
|
failure++; |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
return [success, failure]; |
174 |
} |
} |
175 |
|
|
176 |
unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) { |
async unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) { |
177 |
return Promise.all(channels.map(c => this.unlock(c, options))); |
let success = 0, failure = 0; |
178 |
|
|
179 |
|
for await (const channel of channels) { |
180 |
|
console.log('Unlocking', channel.name); |
181 |
|
|
182 |
|
if (await this.unlock(channel, options)) { |
183 |
|
success++; |
184 |
|
} |
185 |
|
else { |
186 |
|
failure++; |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
return [success, failure]; |
191 |
} |
} |
192 |
} |
} |