Remove Channel#_rawPermissionOverwrites

This commit is contained in:
ayyanm
2020-10-29 12:36:19 -07:00
parent 4e39f327a5
commit 3060bb5b33
2 changed files with 19 additions and 14 deletions

View File

@@ -46,8 +46,6 @@ export async function createChannel(
nsfw: data.nsfw || false,
/** The mention of the channel */
mention: `<#${data.id}>`,
/** Raw permissions */
_rawPermissionOverwrites: permission_overwrites,
};
cacheHandlers.set("channels", data.id, channel);

View File

@@ -2,7 +2,7 @@ import { cacheHandlers } from "../controllers/cache.ts";
import { botID } from "../module/client.ts";
import { Guild } from "../structures/guild.ts";
import { Role } from "../structures/role.ts";
import { RawOverwrite } from "../types/guild.ts";
import { PermissionOverwrite } from "../types/guild.ts";
import { Permission, Permissions } from "../types/permission.ts";
/** Checks if the member has this permission. If the member is an owner or has admin perms it will always be true. */
@@ -104,11 +104,11 @@ export async function hasChannelPermissions(
const member = guild.members.get(memberID);
if (!member) return false;
let memberOverwrite: RawOverwrite | undefined;
let everyoneOverwrite: RawOverwrite | undefined;
let rolesOverwrites: RawOverwrite[] = [];
let memberOverwrite: PermissionOverwrite | undefined;
let everyoneOverwrite: PermissionOverwrite | undefined;
let rolesOverwrites: PermissionOverwrite[] = [];
for (const overwrite of channel._rawPermissionOverwrites || []) {
for (const overwrite of channel.permissionOverwrites || []) {
// If the overwrite on this channel is specific to this member
if (overwrite.id === memberID) memberOverwrite = overwrite;
// If it is the everyone role overwrite
@@ -121,13 +121,16 @@ export async function hasChannelPermissions(
// Member perms override everything so we must check them first
if (memberOverwrite) {
const allowBits = calculateBits(memberOverwrite.allow);
const denyBits = calculateBits(memberOverwrite.deny);
for (const perm of permissions) {
// One of the necessary permissions is denied. Since this is main permission we can cancel if its denied.
if (BigInt(memberOverwrite.deny) & BigInt(perm)) return false;
if (BigInt(denyBits) & BigInt(perm)) return false;
// Already allowed perm
if (allowedPermissions.has(perm)) continue;
// This perm is allowed so we save it
if (BigInt(memberOverwrite.allow) & BigInt(perm)) {
if (BigInt(allowBits) & BigInt(perm)) {
allowedPermissions.add(perm);
}
}
@@ -139,17 +142,19 @@ export async function hasChannelPermissions(
if (allowedPermissions.has(perm)) continue;
for (const overwrite of rolesOverwrites) {
const allowBits = calculateBits(overwrite.allow);
// This perm is allowed so we save it
if (BigInt(overwrite.allow) & BigInt(perm)) {
if (BigInt(allowBits) & BigInt(perm)) {
allowedPermissions.add(perm);
break;
}
const denyBits = calculateBits(overwrite.deny);
// If this role denies it we need to save and check if another role allows it, allows > deny
if (BigInt(overwrite.deny) & BigInt(perm)) {
if (BigInt(denyBits) & BigInt(perm)) {
// This role denies his perm, but before denying we need to check all other roles if any allow as allow > deny
const isAllowed = rolesOverwrites.some((o) =>
BigInt(o.allow) & BigInt(perm)
BigInt(calculateBits(o.allow)) & BigInt(perm)
);
if (isAllowed) continue;
// This permission is in fact denied. Since Roles overrule everything below here we can cancel ou here
@@ -159,13 +164,15 @@ export async function hasChannelPermissions(
}
if (everyoneOverwrite) {
const allowBits = calculateBits(everyoneOverwrite.allow);
const denyBits = calculateBits(everyoneOverwrite.deny);
for (const perm of permissions) {
// Already allowed perm
if (allowedPermissions.has(perm)) continue;
// One of the necessary permissions is denied. Since everyone overwrite overrides role perms we can cancel here
if (BigInt(everyoneOverwrite.deny) & BigInt(perm)) return false;
if (BigInt(denyBits) & BigInt(perm)) return false;
// This perm is allowed so we save it
if (BigInt(everyoneOverwrite.allow) & BigInt(perm)) {
if (BigInt(allowBits) & BigInt(perm)) {
allowedPermissions.add(perm);
}
}