From d26785b0983d8ccfbc73d90661964dc87e4349cf Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 12 Aug 2021 00:27:05 +0200 Subject: :sparkles: added .overwriteChannel function to permissions --- src/util/Permissions.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/util') diff --git a/src/util/Permissions.ts b/src/util/Permissions.ts index 64ffed35..445e901f 100644 --- a/src/util/Permissions.ts +++ b/src/util/Permissions.ts @@ -117,6 +117,16 @@ export class Permissions extends BitField { throw new HTTPError(`You are missing the following permissions ${permission}`, 403); } + overwriteChannel(overwrites: ChannelPermissionOverwrite[]) { + if (!this.cache) throw new Error("permission chache not available"); + overwrites = overwrites.filter((x) => { + if (x.type === 0 && this.cache.roles?.some((r) => r.id === x.id)) return true; + if (x.type === 1 && x.id == this.cache.user_id) return true; + return false; + }); + return new Permissions(Permissions.channelPermission(overwrites, this.bitfield)); + } + static channelPermission(overwrites: ChannelPermissionOverwrite[], init?: bigint) { // TODO: do not deny any permissions if admin return overwrites.reduce((permission, overwrite) => { @@ -186,7 +196,7 @@ export class Permissions extends BitField { return new Permissions(); } - return permission; + return new Permissions(permission); } } @@ -195,6 +205,7 @@ export type PermissionCache = { member?: MemberDocument | null; guild?: GuildDocument | null; roles?: RoleDocument[] | null; + user_id?: string; }; export async function getPermission( @@ -245,7 +256,7 @@ export async function getPermission( const obj = new Permissions(permission); // pass cache to permission for possible future getPermission calls - obj.cache = { guild, member, channel, roles }; + obj.cache = { guild, member, channel, roles, user_id }; return obj; } -- cgit 1.4.1