1 |
rakin |
242 |
import { GuildChannel, PermissionString, Role, RoleResolvable, User } from "discord.js"; |
2 |
rakin |
241 |
import MessageEmbed from "../client/MessageEmbed"; |
3 |
|
|
import ChannelLock from "../models/ChannelLock"; |
4 |
|
|
import Service from "../utils/structures/Service"; |
5 |
|
|
|
6 |
|
|
export interface ChannelLockOptions { |
7 |
|
|
reason?: string; |
8 |
|
|
sendConfirmation?: boolean; |
9 |
|
|
force?: boolean; |
10 |
rakin |
242 |
role?: RoleResolvable; |
11 |
rakin |
241 |
} |
12 |
|
|
|
13 |
|
|
export default class ChannelLockManager extends Service { |
14 |
rakin |
242 |
async lock(channel: GuildChannel, user: User, { reason, sendConfirmation, role }: ChannelLockOptions = {}) { |
15 |
|
|
const lockRole = role ? (role instanceof Role ? role.id : role) : channel.guild.id; |
16 |
|
|
|
17 |
rakin |
241 |
const channelLock = await ChannelLock.findOne({ |
18 |
|
|
where: { |
19 |
|
|
channel_id: channel.id, |
20 |
rakin |
242 |
guild_id: channel.guild.id, |
21 |
|
|
role_id: lockRole |
22 |
rakin |
241 |
} |
23 |
|
|
}); |
24 |
|
|
|
25 |
|
|
if (channelLock) { |
26 |
|
|
return false; |
27 |
|
|
} |
28 |
|
|
|
29 |
rakin |
242 |
const permissions = channel.permissionOverwrites.cache.get(lockRole); |
30 |
rakin |
241 |
|
31 |
|
|
if (permissions) { |
32 |
|
|
const permJson = { |
33 |
|
|
allow: permissions.allow.toArray(), |
34 |
|
|
deny: permissions.deny.toArray(), |
35 |
|
|
}; |
36 |
|
|
|
37 |
|
|
console.log(permJson); |
38 |
|
|
|
39 |
|
|
await ChannelLock.create({ |
40 |
|
|
user_id: user.id, |
41 |
|
|
guild_id: channel.guild.id, |
42 |
|
|
channel_id: channel.id, |
43 |
|
|
reason, |
44 |
rakin |
242 |
previous_perms: permJson, |
45 |
|
|
role_id: lockRole |
46 |
rakin |
241 |
}); |
47 |
|
|
} |
48 |
|
|
|
49 |
rakin |
242 |
await channel.permissionOverwrites.edit(lockRole, { |
50 |
rakin |
241 |
SEND_MESSAGES: false, |
51 |
|
|
SEND_MESSAGES_IN_THREADS: false, |
52 |
|
|
ADD_REACTIONS: false, |
53 |
|
|
REQUEST_TO_SPEAK: false, |
54 |
|
|
SPEAK: false, |
55 |
|
|
}, { reason }); |
56 |
|
|
|
57 |
|
|
if (sendConfirmation && channel.isText()) { |
58 |
|
|
await channel.send({ |
59 |
|
|
embeds: [ |
60 |
|
|
new MessageEmbed({ |
61 |
|
|
color: 0x007bff, |
62 |
|
|
description: `:lock: This channel has been locked.`, |
63 |
|
|
fields: reason ? [ |
64 |
|
|
{ |
65 |
|
|
name: 'Reason', |
66 |
|
|
value: reason + '' |
67 |
|
|
} |
68 |
|
|
] : [], |
69 |
|
|
footer: { text: 'Locked' } |
70 |
|
|
}) |
71 |
|
|
.setTimestamp() |
72 |
|
|
] |
73 |
|
|
}); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
return true; |
77 |
|
|
} |
78 |
|
|
|
79 |
rakin |
242 |
async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) { |
80 |
|
|
const lockRole = role ? (role instanceof Role ? role.id : role) : channel.guild.id; |
81 |
|
|
|
82 |
rakin |
241 |
const channelLock = await ChannelLock.findOne({ |
83 |
|
|
where: { |
84 |
|
|
channel_id: channel.id, |
85 |
rakin |
242 |
guild_id: channel.guild.id, |
86 |
|
|
role_id: lockRole |
87 |
rakin |
241 |
} |
88 |
|
|
}); |
89 |
|
|
|
90 |
rakin |
242 |
if (!channelLock) { |
91 |
|
|
return false; |
92 |
|
|
} |
93 |
|
|
|
94 |
rakin |
241 |
const permissions = channelLock?.get('previous_perms') as { allow: PermissionString[], deny: PermissionString[] }; |
95 |
|
|
|
96 |
|
|
if (!permissions && !force) { |
97 |
|
|
return false; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
const transform = (key: PermissionString) => { |
101 |
|
|
if (!permissions) { |
102 |
|
|
return force ? true : undefined; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
if (permissions.allow.includes(key) && !permissions.deny.includes(key)) { |
106 |
|
|
return true; |
107 |
|
|
} |
108 |
|
|
else if (!permissions.allow.includes(key) && permissions.deny.includes(key)) { |
109 |
|
|
return false; |
110 |
|
|
} |
111 |
|
|
else { |
112 |
|
|
return null; |
113 |
|
|
} |
114 |
|
|
}; |
115 |
|
|
|
116 |
rakin |
242 |
await channel.permissionOverwrites.edit(lockRole, { |
117 |
rakin |
241 |
SEND_MESSAGES: transform('SEND_MESSAGES'), |
118 |
|
|
SEND_MESSAGES_IN_THREADS: transform('SEND_MESSAGES_IN_THREADS'), |
119 |
|
|
ADD_REACTIONS: transform('ADD_REACTIONS'), |
120 |
|
|
REQUEST_TO_SPEAK: transform('REQUEST_TO_SPEAK'), |
121 |
|
|
SPEAK: transform('SPEAK'), |
122 |
|
|
}, { reason }); |
123 |
|
|
|
124 |
|
|
await channelLock?.destroy(); |
125 |
|
|
|
126 |
|
|
if (sendConfirmation && channel.isText()) { |
127 |
|
|
await channel.send({ |
128 |
|
|
embeds: [ |
129 |
|
|
new MessageEmbed({ |
130 |
|
|
color: 0x007bff, |
131 |
|
|
description: `:closed_lock_with_key: This channel has been unlocked.`, |
132 |
|
|
fields: reason ? [ |
133 |
|
|
{ |
134 |
|
|
name: 'Reason', |
135 |
|
|
value: reason + '' |
136 |
|
|
} |
137 |
|
|
] : [], |
138 |
|
|
footer: { text: 'Unlocked' } |
139 |
|
|
}) |
140 |
|
|
.setTimestamp() |
141 |
|
|
] |
142 |
|
|
}); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
return true; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) { |
149 |
|
|
return Promise.all(channels.map(c => this.lock(c, user, options))); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) { |
153 |
|
|
return Promise.all(channels.map(c => this.unlock(c, options))); |
154 |
|
|
} |
155 |
|
|
} |