1 |
|
/** |
2 |
|
* This file is part of SudoBot. |
3 |
|
* |
4 |
|
* Copyright (C) 2021-2022 OSN Inc. |
5 |
|
* |
6 |
|
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
|
* under the terms of the GNU Affero General Public License as published by |
8 |
|
* the Free Software Foundation, either version 3 of the License, or |
9 |
|
* (at your option) any later version. |
10 |
|
* |
11 |
|
* SudoBot is distributed in the hope that it will be useful, but |
12 |
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
* GNU Affero General Public License for more details. |
15 |
|
* |
16 |
|
* You should have received a copy of the GNU Affero General Public License |
17 |
|
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
|
*/ |
19 |
|
|
20 |
import { GuildChannel, PermissionString, Role, RoleResolvable, User } from "discord.js"; |
import { GuildChannel, PermissionString, Role, RoleResolvable, User } from "discord.js"; |
21 |
import MessageEmbed from "../client/MessageEmbed"; |
import MessageEmbed from "../client/MessageEmbed"; |
22 |
import ChannelLock from "../models/ChannelLock"; |
import ChannelLock from "../models/ChannelLock"; |
31 |
|
|
32 |
export default class ChannelLockManager extends Service { |
export default class ChannelLockManager extends Service { |
33 |
async lock(channel: GuildChannel, user: User, { reason, sendConfirmation, role }: ChannelLockOptions = {}) { |
async lock(channel: GuildChannel, user: User, { reason, sendConfirmation, role }: ChannelLockOptions = {}) { |
34 |
const lockRole = role ? (role instanceof Role ? role.id : role) : channel.guild.id; |
const lockRole = role ? (role instanceof Role ? role : (await channel.guild.roles.fetch(role))!) : channel.guild.roles.everyone; |
35 |
|
|
36 |
const channelLock = await ChannelLock.findOne({ |
const channelLock = await ChannelLock.findOne({ |
37 |
where: { |
where: { |
38 |
channel_id: channel.id, |
channel_id: channel.id, |
39 |
guild_id: channel.guild.id, |
guild_id: channel.guild.id, |
40 |
role_id: lockRole |
role_id: lockRole.id |
41 |
} |
} |
42 |
}); |
}); |
43 |
|
|
44 |
if (channelLock) { |
if (channelLock) { |
45 |
|
console.log('exists'); |
46 |
return false; |
return false; |
47 |
} |
} |
48 |
|
|
49 |
const permissions = channel.permissionOverwrites.cache.get(lockRole); |
console.log(lockRole?.name); |
|
|
|
|
if (permissions) { |
|
|
const permJson = { |
|
|
allow: permissions.allow.toArray(), |
|
|
deny: permissions.deny.toArray(), |
|
|
}; |
|
50 |
|
|
51 |
console.log(permJson); |
let permissions = channel.permissionOverwrites.cache.get(lockRole.id); |
52 |
|
|
53 |
await ChannelLock.create({ |
const permJson = { |
54 |
user_id: user.id, |
allow: permissions?.allow?.toArray() ?? null, |
55 |
guild_id: channel.guild.id, |
deny: permissions?.deny?.toArray() ?? null, |
56 |
channel_id: channel.id, |
}; |
57 |
reason, |
|
58 |
previous_perms: permJson, |
console.log(permJson); |
59 |
role_id: lockRole |
|
60 |
}); |
await ChannelLock.create({ |
61 |
} |
user_id: user.id, |
62 |
|
guild_id: channel.guild.id, |
63 |
|
channel_id: channel.id, |
64 |
|
reason, |
65 |
|
previous_perms: permJson, |
66 |
|
role_id: lockRole.id, |
67 |
|
createdAt: new Date() |
68 |
|
}); |
69 |
|
|
70 |
await channel.permissionOverwrites.edit(lockRole, { |
await channel.permissionOverwrites.edit(lockRole, { |
71 |
SEND_MESSAGES: false, |
SEND_MESSAGES: false, |
72 |
SEND_MESSAGES_IN_THREADS: false, |
SEND_MESSAGES_IN_THREADS: false, |
|
ADD_REACTIONS: false, |
|
73 |
REQUEST_TO_SPEAK: false, |
REQUEST_TO_SPEAK: false, |
74 |
SPEAK: false, |
SPEAK: false, |
75 |
}, { reason }); |
}, { reason }); |
76 |
|
|
77 |
|
console.log('success'); |
78 |
|
|
79 |
if (sendConfirmation && channel.isText()) { |
if (sendConfirmation && channel.isText()) { |
80 |
await channel.send({ |
await channel.send({ |
81 |
embeds: [ |
embeds: [ |
99 |
} |
} |
100 |
|
|
101 |
async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) { |
async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) { |
102 |
const lockRole = role ? (role instanceof Role ? role.id : role) : channel.guild.id; |
const lockRole = role ? (role instanceof Role ? role : (await channel.guild.roles.fetch(role))!) : channel.guild.roles.everyone; |
103 |
|
|
104 |
const channelLock = await ChannelLock.findOne({ |
const channelLock = await ChannelLock.findOne({ |
105 |
where: { |
channel_id: channel.id, |
106 |
channel_id: channel.id, |
guild_id: channel.guild.id, |
107 |
guild_id: channel.guild.id, |
role_id: lockRole.id |
|
role_id: lockRole |
|
|
} |
|
108 |
}); |
}); |
109 |
|
|
110 |
if (!channelLock) { |
if (!channelLock) { |
111 |
|
console.log('Channel not locked'); |
112 |
return false; |
return false; |
113 |
} |
} |
114 |
|
|
115 |
const permissions = channelLock?.get('previous_perms') as { allow: PermissionString[], deny: PermissionString[] }; |
const permissions = channelLock?.previous_perms; // as { allow: PermissionString[] | null, deny: PermissionString[] | null } |
116 |
|
|
117 |
if (!permissions && !force) { |
if (!permissions && !force) { |
118 |
|
console.log('Permission error'); |
119 |
return false; |
return false; |
120 |
} |
} |
121 |
|
|
122 |
const transform = (key: PermissionString) => { |
const transform = (key: PermissionString) => { |
123 |
|
if (!permissions?.allow || !permissions?.deny) { |
124 |
|
return undefined; |
125 |
|
} |
126 |
|
|
127 |
if (!permissions) { |
if (!permissions) { |
128 |
return force ? true : undefined; |
return force ? true : undefined; |
129 |
} |
} |
139 |
} |
} |
140 |
}; |
}; |
141 |
|
|
142 |
await channel.permissionOverwrites.edit(lockRole, { |
if (!permissions?.allow && !permissions?.deny) { |
143 |
SEND_MESSAGES: transform('SEND_MESSAGES'), |
await channel.permissionOverwrites.delete(lockRole); |
144 |
SEND_MESSAGES_IN_THREADS: transform('SEND_MESSAGES_IN_THREADS'), |
} |
145 |
ADD_REACTIONS: transform('ADD_REACTIONS'), |
else { |
146 |
REQUEST_TO_SPEAK: transform('REQUEST_TO_SPEAK'), |
await channel.permissionOverwrites.edit(lockRole, { |
147 |
SPEAK: transform('SPEAK'), |
SEND_MESSAGES: transform('SEND_MESSAGES'), |
148 |
}, { reason }); |
SEND_MESSAGES_IN_THREADS: transform('SEND_MESSAGES_IN_THREADS'), |
149 |
|
REQUEST_TO_SPEAK: transform('REQUEST_TO_SPEAK'), |
150 |
|
SPEAK: transform('SPEAK'), |
151 |
|
}, { reason }); |
152 |
|
} |
153 |
|
|
154 |
await channelLock?.destroy(); |
await channelLock?.delete(); |
155 |
|
|
156 |
if (sendConfirmation && channel.isText()) { |
if (sendConfirmation && channel.isText()) { |
157 |
await channel.send({ |
await channel.send({ |
175 |
return true; |
return true; |
176 |
} |
} |
177 |
|
|
178 |
lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) { |
async lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) { |
179 |
return Promise.all(channels.map(c => this.lock(c, user, options))); |
let success = 0, failure = 0; |
180 |
|
|
181 |
|
for await (const channel of channels) { |
182 |
|
console.log('Locking', channel.name); |
183 |
|
|
184 |
|
if (await this.lock(channel, user, options)) { |
185 |
|
success++; |
186 |
|
} |
187 |
|
else { |
188 |
|
failure++; |
189 |
|
} |
190 |
|
} |
191 |
|
|
192 |
|
return [success, failure]; |
193 |
} |
} |
194 |
|
|
195 |
unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) { |
async unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) { |
196 |
return Promise.all(channels.map(c => this.unlock(c, options))); |
let success = 0, failure = 0; |
197 |
|
|
198 |
|
for await (const channel of channels) { |
199 |
|
console.log('Unlocking', channel.name); |
200 |
|
|
201 |
|
if (await this.unlock(channel, options)) { |
202 |
|
success++; |
203 |
|
} |
204 |
|
else { |
205 |
|
failure++; |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
return [success, failure]; |
210 |
} |
} |
211 |
} |
} |