/[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 331 by rakin, Mon Jul 29 17:29:35 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                createdAt: new Date()
49            });
50    
51          await channel.permissionOverwrites.edit(lockRole, {          await channel.permissionOverwrites.edit(lockRole, {
52              SEND_MESSAGES: false,              SEND_MESSAGES: false,
53              SEND_MESSAGES_IN_THREADS: false,              SEND_MESSAGES_IN_THREADS: false,
             ADD_REACTIONS: false,  
54              REQUEST_TO_SPEAK: false,              REQUEST_TO_SPEAK: false,
55              SPEAK: false,              SPEAK: false,
56          }, { reason });          }, { reason });
57    
58            console.log('success');
59    
60          if (sendConfirmation && channel.isText()) {          if (sendConfirmation && channel.isText()) {
61              await channel.send({              await channel.send({
62                  embeds: [                  embeds: [
# Line 77  export default class ChannelLockManager Line 80  export default class ChannelLockManager
80      }      }
81    
82      async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) {      async unlock(channel: GuildChannel, { reason, sendConfirmation, force, role }: ChannelLockOptions = {}) {
83          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;
84    
85          const channelLock = await ChannelLock.findOne({          const channelLock = await ChannelLock.findOne({
86              where: {              channel_id: channel.id,
87                  channel_id: channel.id,              guild_id: channel.guild.id,
88                  guild_id: channel.guild.id,              role_id: lockRole.id
                 role_id: lockRole  
             }  
89          });          });
90    
91          if (!channelLock) {          if (!channelLock) {
92                console.log('Channel not locked');            
93              return false;              return false;
94          }          }
95    
96          const permissions = channelLock?.get('previous_perms') as { allow: PermissionString[], deny: PermissionString[] };          const permissions = channelLock?.previous_perms; //  as { allow: PermissionString[] | null, deny: PermissionString[] | null }
97    
98          if (!permissions && !force) {          if (!permissions && !force) {
99                console.log('Permission error');
100              return false;              return false;
101          }          }
102    
103          const transform = (key: PermissionString) => {          const transform = (key: PermissionString) => {
104                if (!permissions?.allow || !permissions?.deny) {
105                    return undefined;
106                }
107    
108              if (!permissions) {              if (!permissions) {
109                  return force ? true : undefined;                  return force ? true : undefined;
110              }              }
# Line 113  export default class ChannelLockManager Line 120  export default class ChannelLockManager
120              }              }
121          };          };
122    
123          await channel.permissionOverwrites.edit(lockRole, {          if (!permissions?.allow && !permissions?.deny) {
124              SEND_MESSAGES: transform('SEND_MESSAGES'),              await channel.permissionOverwrites.delete(lockRole);
125              SEND_MESSAGES_IN_THREADS: transform('SEND_MESSAGES_IN_THREADS'),          }
126              ADD_REACTIONS: transform('ADD_REACTIONS'),          else {
127              REQUEST_TO_SPEAK: transform('REQUEST_TO_SPEAK'),              await channel.permissionOverwrites.edit(lockRole, {
128              SPEAK: transform('SPEAK'),                  SEND_MESSAGES: transform('SEND_MESSAGES'),
129          }, { reason });                  SEND_MESSAGES_IN_THREADS: transform('SEND_MESSAGES_IN_THREADS'),
130                    REQUEST_TO_SPEAK: transform('REQUEST_TO_SPEAK'),
131                    SPEAK: transform('SPEAK'),
132                }, { reason });
133            }        
134    
135          await channelLock?.destroy();          await channelLock?.delete();
136    
137          if (sendConfirmation && channel.isText()) {          if (sendConfirmation && channel.isText()) {
138              await channel.send({              await channel.send({
# Line 145  export default class ChannelLockManager Line 156  export default class ChannelLockManager
156          return true;          return true;
157      }      }
158    
159      lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) {      async lockAll(channels: GuildChannel[], user: User, options: ChannelLockOptions = {}) {
160          return Promise.all(channels.map(c => this.lock(c, user, options)));          let success = 0, failure = 0;
161    
162            for await (const channel of channels) {
163                console.log('Locking', channel.name);
164                
165                if (await this.lock(channel, user, options)) {
166                    success++;
167                }
168                else {
169                    failure++;
170                }
171            }
172    
173            return [success, failure];
174      }      }
175    
176      unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) {      async unlockAll(channels: GuildChannel[], options: ChannelLockOptions = {}) {
177          return Promise.all(channels.map(c => this.unlock(c, options)));          let success = 0, failure = 0;
178    
179            for await (const channel of channels) {
180                console.log('Unlocking', channel.name);
181    
182                if (await this.unlock(channel, options)) {
183                    success++;
184                }
185                else {
186                    failure++;
187                }
188            }
189    
190            return [success, failure];
191      }      }
192  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26