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

Contents of /branches/2.x/src/services/ChannelLockManager.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26