/[sudobot]/trunk/src/services/ChannelLockManager.ts
ViewVC logotype

Annotation of /trunk/src/services/ChannelLockManager.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 393 - (hide annotations)
Mon Jul 29 17:29:59 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6807 byte(s)
style: add license comments (#77)

* style: add license commits

* fix: shebang errors
1 rakin 393 /**
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 rakin 242 import { GuildChannel, PermissionString, Role, RoleResolvable, User } from "discord.js";
21 rakin 241 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 rakin 242 role?: RoleResolvable;
30 rakin 241 }
31    
32     export default class ChannelLockManager extends Service {
33 rakin 242 async lock(channel: GuildChannel, user: User, { reason, sendConfirmation, role }: ChannelLockOptions = {}) {
34 rakin 244 const lockRole = role ? (role instanceof Role ? role : (await channel.guild.roles.fetch(role))!) : channel.guild.roles.everyone;
35 rakin 242
36 rakin 241 const channelLock = await ChannelLock.findOne({
37     where: {
38     channel_id: channel.id,
39 rakin 242 guild_id: channel.guild.id,
40 rakin 244 role_id: lockRole.id
41 rakin 241 }
42     });
43    
44     if (channelLock) {
45 rakin 244 console.log('exists');
46 rakin 241 return false;
47     }
48    
49 rakin 244 console.log(lockRole?.name);
50 rakin 241
51 rakin 244 let permissions = channel.permissionOverwrites.cache.get(lockRole.id);
52 rakin 241
53 rakin 244 const permJson = {
54     allow: permissions?.allow?.toArray() ?? null,
55     deny: permissions?.deny?.toArray() ?? null,
56     };
57 rakin 241
58 rakin 244 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 rakin 331 role_id: lockRole.id,
67     createdAt: new Date()
68 rakin 244 });
69    
70 rakin 242 await channel.permissionOverwrites.edit(lockRole, {
71 rakin 241 SEND_MESSAGES: false,
72     SEND_MESSAGES_IN_THREADS: false,
73     REQUEST_TO_SPEAK: false,
74     SPEAK: false,
75     }, { reason });
76    
77 rakin 244 console.log('success');
78    
79 rakin 241 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 rakin 242 async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) {
102 rakin 244 const lockRole = role ? (role instanceof Role ? role : (await channel.guild.roles.fetch(role))!) : channel.guild.roles.everyone;
103 rakin 242
104 rakin 241 const channelLock = await ChannelLock.findOne({
105 rakin 331 channel_id: channel.id,
106     guild_id: channel.guild.id,
107     role_id: lockRole.id
108 rakin 241 });
109    
110 rakin 242 if (!channelLock) {
111 rakin 244 console.log('Channel not locked');
112 rakin 242 return false;
113     }
114    
115 rakin 331 const permissions = channelLock?.previous_perms; // as { allow: PermissionString[] | null, deny: PermissionString[] | null }
116 rakin 241
117     if (!permissions && !force) {
118 rakin 244 console.log('Permission error');
119 rakin 241 return false;
120     }
121    
122     const transform = (key: PermissionString) => {
123 rakin 331 if (!permissions?.allow || !permissions?.deny) {
124 rakin 244 return undefined;
125     }
126    
127 rakin 241 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 rakin 331 if (!permissions?.allow && !permissions?.deny) {
143 rakin 244 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 rakin 241
154 rakin 331 await channelLock?.delete();
155 rakin 241
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 rakin 244 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 rakin 241 }
194    
195 rakin 244 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 rakin 241 }
211     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26