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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 242 by rakin, Mon Jul 29 17:29:10 2024 UTC revision 244 by rakin, Mon Jul 29 17:29:11 2024 UTC
# Line 12  export interface ChannelLockOptions { Line 12  export interface ChannelLockOptions {
12    
13  export default class ChannelLockManager extends Service {  export default class ChannelLockManager extends Service {
14      async lock(channel: GuildChannel, user: User, { reason, sendConfirmation, role }: ChannelLockOptions = {}) {      async lock(channel: GuildChannel, user: User, { reason, sendConfirmation, role }: ChannelLockOptions = {}) {
15          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;
16    
17          const channelLock = await ChannelLock.findOne({          const channelLock = await ChannelLock.findOne({
18              where: {              where: {
19                  channel_id: channel.id,                  channel_id: channel.id,
20                  guild_id: channel.guild.id,                  guild_id: channel.guild.id,
21                  role_id: lockRole                  role_id: lockRole.id
22              }              }
23          });          });
24    
25          if (channelLock) {          if (channelLock) {
26                console.log('exists');
27              return false;              return false;
28          }          }
29    
30          const permissions = channel.permissionOverwrites.cache.get(lockRole);          console.log(lockRole?.name);
           
         if (permissions) {  
             const permJson = {  
                 allow: permissions.allow.toArray(),  
                 deny: permissions.deny.toArray(),  
             };  
31    
32              console.log(permJson);          let permissions = channel.permissionOverwrites.cache.get(lockRole.id);
33    
34              await ChannelLock.create({          const permJson = {
35                  user_id: user.id,              allow: permissions?.allow?.toArray() ?? null,
36                  guild_id: channel.guild.id,              deny: permissions?.deny?.toArray() ?? null,
37                  channel_id: channel.id,          };
38                  reason,  
39                  previous_perms: permJson,          console.log(permJson);
40                  role_id: lockRole  
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, {          await channel.permissionOverwrites.edit(lockRole, {
51              SEND_MESSAGES: false,              SEND_MESSAGES: false,
52              SEND_MESSAGES_IN_THREADS: false,              SEND_MESSAGES_IN_THREADS: false,
             ADD_REACTIONS: false,  
53              REQUEST_TO_SPEAK: false,              REQUEST_TO_SPEAK: false,
54              SPEAK: false,              SPEAK: false,
55          }, { reason });          }, { reason });
56    
57            console.log('success');
58    
59          if (sendConfirmation && channel.isText()) {          if (sendConfirmation && channel.isText()) {
60              await channel.send({              await channel.send({
61                  embeds: [                  embeds: [
# Line 77  export default class ChannelLockManager Line 79  export default class ChannelLockManager
79      }      }
80    
81      async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) {      async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) {
82          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;
83    
84          const channelLock = await ChannelLock.findOne({          const channelLock = await ChannelLock.findOne({
85              where: {              where: {
86                  channel_id: channel.id,                  channel_id: channel.id,
87                  guild_id: channel.guild.id,                  guild_id: channel.guild.id,
88                  role_id: lockRole                  role_id: lockRole.id
89              }              }
90          });          });
91    
92          if (!channelLock) {          if (!channelLock) {
93                console.log('Channel not locked');            
94              return false;              return false;
95          }          }
96    
97          const permissions = channelLock?.get('previous_perms') as { allow: PermissionString[], deny: PermissionString[] };          const permissions = channelLock?.get('previous_perms') as { allow: PermissionString[] | null, deny: PermissionString[] | null };
98    
99          if (!permissions && !force) {          if (!permissions && !force) {
100                console.log('Permission error');
101              return false;              return false;
102          }          }
103    
104          const transform = (key: PermissionString) => {          const transform = (key: PermissionString) => {
105                if (!permissions.allow || !permissions.deny) {
106                    return undefined;
107                }
108    
109              if (!permissions) {              if (!permissions) {
110                  return force ? true : undefined;                  return force ? true : undefined;
111              }              }
# Line 113  export default class ChannelLockManager Line 121  export default class ChannelLockManager
121              }              }
122          };          };
123    
124          await channel.permissionOverwrites.edit(lockRole, {          if (!permissions.allow && !permissions.deny) {
125              SEND_MESSAGES: transform('SEND_MESSAGES'),              await channel.permissionOverwrites.delete(lockRole);
126              SEND_MESSAGES_IN_THREADS: transform('SEND_MESSAGES_IN_THREADS'),          }
127              ADD_REACTIONS: transform('ADD_REACTIONS'),          else {
128              REQUEST_TO_SPEAK: transform('REQUEST_TO_SPEAK'),              await channel.permissionOverwrites.edit(lockRole, {
129              SPEAK: transform('SPEAK'),                  SEND_MESSAGES: transform('SEND_MESSAGES'),
130          }, { reason });                  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();          await channelLock?.destroy();
137    
# Line 145  export default class ChannelLockManager Line 157  export default class ChannelLockManager
157          return true;          return true;
158      }      }
159    
160      lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) {      async lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) {
161          return Promise.all(channels.map(c => this.lock(c, user, options)));          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      unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) {      async unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) {
178          return Promise.all(channels.map(c => this.unlock(c, options)));          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  }  }

Legend:
Removed from v.242  
changed lines
  Added in v.244

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26