/[sudobot]/branches/4.x/src/services/ChannelLockManager.ts
ViewVC logotype

Annotation of /branches/4.x/src/services/ChannelLockManager.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 6807 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
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";
21     import MessageEmbed from "../client/MessageEmbed";
22     import ChannelLock from "../models/ChannelLock";
23     import Service from "../utils/structures/Service";
24    
25     export interface ChannelLockOptions {
26     reason?: string;
27     sendConfirmation?: boolean;
28     force?: boolean;
29     role?: RoleResolvable;
30     }
31    
32     export default class ChannelLockManager extends Service {
33     async lock(channel: GuildChannel, user: User, { reason, sendConfirmation, role }: ChannelLockOptions = {}) {
34     const lockRole = role ? (role instanceof Role ? role : (await channel.guild.roles.fetch(role))!) : channel.guild.roles.everyone;
35    
36     const channelLock = await ChannelLock.findOne({
37     where: {
38     channel_id: channel.id,
39     guild_id: channel.guild.id,
40     role_id: lockRole.id
41     }
42     });
43    
44     if (channelLock) {
45     console.log('exists');
46     return false;
47     }
48    
49     console.log(lockRole?.name);
50    
51     let permissions = channel.permissionOverwrites.cache.get(lockRole.id);
52    
53     const permJson = {
54     allow: permissions?.allow?.toArray() ?? null,
55     deny: permissions?.deny?.toArray() ?? null,
56     };
57    
58     console.log(permJson);
59    
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, {
71     SEND_MESSAGES: false,
72     SEND_MESSAGES_IN_THREADS: false,
73     REQUEST_TO_SPEAK: false,
74     SPEAK: false,
75     }, { reason });
76    
77     console.log('success');
78    
79     if (sendConfirmation && channel.isText()) {
80     await channel.send({
81     embeds: [
82     new MessageEmbed({
83     color: 0x007bff,
84     description: `:lock: This channel has been locked.`,
85     fields: reason ? [
86     {
87     name: 'Reason',
88     value: reason + ''
89     }
90     ] : [],
91     footer: { text: 'Locked' }
92     })
93     .setTimestamp()
94     ]
95     });
96     }
97    
98     return true;
99     }
100    
101     async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) {
102     const lockRole = role ? (role instanceof Role ? role : (await channel.guild.roles.fetch(role))!) : channel.guild.roles.everyone;
103    
104     const channelLock = await ChannelLock.findOne({
105     channel_id: channel.id,
106     guild_id: channel.guild.id,
107     role_id: lockRole.id
108     });
109    
110     if (!channelLock) {
111     console.log('Channel not locked');
112     return false;
113     }
114    
115     const permissions = channelLock?.previous_perms; // as { allow: PermissionString[] | null, deny: PermissionString[] | null }
116    
117     if (!permissions && !force) {
118     console.log('Permission error');
119     return false;
120     }
121    
122     const transform = (key: PermissionString) => {
123     if (!permissions?.allow || !permissions?.deny) {
124     return undefined;
125     }
126    
127     if (!permissions) {
128     return force ? true : undefined;
129     }
130    
131     if (permissions.allow.includes(key) && !permissions.deny.includes(key)) {
132     return true;
133     }
134     else if (!permissions.allow.includes(key) && permissions.deny.includes(key)) {
135     return false;
136     }
137     else {
138     return null;
139     }
140     };
141    
142     if (!permissions?.allow && !permissions?.deny) {
143     await channel.permissionOverwrites.delete(lockRole);
144     }
145     else {
146     await channel.permissionOverwrites.edit(lockRole, {
147     SEND_MESSAGES: transform('SEND_MESSAGES'),
148     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?.delete();
155    
156     if (sendConfirmation && channel.isText()) {
157     await channel.send({
158     embeds: [
159     new MessageEmbed({
160     color: 0x007bff,
161     description: `:closed_lock_with_key: This channel has been unlocked.`,
162     fields: reason ? [
163     {
164     name: 'Reason',
165     value: reason + ''
166     }
167     ] : [],
168     footer: { text: 'Unlocked' }
169     })
170     .setTimestamp()
171     ]
172     });
173     }
174    
175     return true;
176     }
177    
178     async lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) {
179     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     async unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) {
196     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     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26