/[sudobot]/branches/6.x/src/utils/LayerBasedPermissionManager.ts
ViewVC logotype

Annotation of /branches/6.x/src/utils/LayerBasedPermissionManager.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: 2878 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 import { PermissionOverwrite } from "@prisma/client";
2     import { GuildMember, PermissionsBitField, PermissionsString, Role, Snowflake } from "discord.js";
3     import { GetMemberPermissionInGuildResult } from "../services/PermissionManager";
4     import AbstractPermissionManager from "./AbstractPermissionManager";
5     import { logInfo } from "./logger";
6    
7     export default class LayerBasedPermissionManager extends AbstractPermissionManager {
8     protected cache: Record<`${Snowflake}_${"r" | "u"}_${Snowflake}`, PermissionOverwrite> = {};
9    
10     async sync() {
11     const overwrites = await this.client.prisma.permissionOverwrite.findMany();
12    
13     this.cache = {};
14    
15     for (const overwrite of overwrites) {
16     for (const roleId of overwrite.roles) {
17     this.cache[`${overwrite.guildId}_r_${roleId}`] = overwrite;
18     }
19    
20     for (const userId of overwrite.users) {
21     this.cache[`${overwrite.guildId}_u_${userId}`] = overwrite;
22     }
23     }
24    
25     logInfo(`[${this.constructor.name}] Synchronized permission overwrites`);
26     }
27    
28     isImmuneToAutoMod(member: GuildMember) {
29     const { permissions } = this.getMemberPermissions(member);
30     return permissions.has("ManageGuild", true);
31     }
32    
33     // FIXME: This is not implemented yet
34     shouldModerate(member: GuildMember, moderator: GuildMember) {
35     return true;
36     }
37    
38     getMemberPermissions(member: GuildMember, mergeWithDiscordPermissions = true): GetMemberPermissionInGuildResult {
39     const overwriteIds = [];
40     const baseUserOverwrite = this.cache[`${member.guild.id}_u_${member.user.id}`];
41     const permissions = new PermissionsBitField([
42     ...(mergeWithDiscordPermissions ? member.permissions.toArray() : []),
43     ...((baseUserOverwrite?.grantedPermissions as PermissionsString[]) ?? [])
44     ]);
45     let highestRoleHavingOverwrite: Role | undefined;
46     let highestOverwrite: PermissionOverwrite | undefined;
47    
48     if (baseUserOverwrite) {
49     overwriteIds.push(baseUserOverwrite.id);
50     }
51    
52     for (const [roleId, role] of member.roles.cache) {
53     const overwrite = this.cache[`${member.guild.id}_r_${roleId}`];
54    
55     if (overwrite) {
56     overwriteIds.push(overwrite.id);
57    
58     for (const permission of overwrite.grantedPermissions) {
59     permissions.add(permission as PermissionsString);
60     }
61    
62     if (role.position > (highestRoleHavingOverwrite?.position ?? 0)) {
63     highestRoleHavingOverwrite = role;
64     highestOverwrite = overwrite;
65     }
66     }
67     }
68    
69     return {
70     type: "layered",
71     permissions,
72     highestRoleHavingOverwrite,
73     highestOverwrite,
74     overwriteIds
75     };
76     }
77     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26