added .overwriteChannel function to permissions

This commit is contained in:
Flam3rboy 2021-08-12 00:27:05 +02:00
parent 1889798b98
commit d26785b098

View File

@ -117,6 +117,16 @@ export class Permissions extends BitField {
throw new HTTPError(`You are missing the following permissions ${permission}`, 403); 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) { static channelPermission(overwrites: ChannelPermissionOverwrite[], init?: bigint) {
// TODO: do not deny any permissions if admin // TODO: do not deny any permissions if admin
return overwrites.reduce((permission, overwrite) => { return overwrites.reduce((permission, overwrite) => {
@ -186,7 +196,7 @@ export class Permissions extends BitField {
return new Permissions(); return new Permissions();
} }
return permission; return new Permissions(permission);
} }
} }
@ -195,6 +205,7 @@ export type PermissionCache = {
member?: MemberDocument | null; member?: MemberDocument | null;
guild?: GuildDocument | null; guild?: GuildDocument | null;
roles?: RoleDocument[] | null; roles?: RoleDocument[] | null;
user_id?: string;
}; };
export async function getPermission( export async function getPermission(
@ -245,7 +256,7 @@ export async function getPermission(
const obj = new Permissions(permission); const obj = new Permissions(permission);
// pass cache to permission for possible future getPermission calls // 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; return obj;
} }