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