refactor: resolve async promises, fixed typos, and used inline variable for return (#299)

* Added await in async function, fixed typos and used inline variable for return

* Added await in async function, fixed typos and used inline variable for return

* Revert "Added await in async function, fixed typos and used inline variable for return"

This reverts commit f31caf5d

* Added await in async function, fixed typos and used inline variable for return

* Fixes format

* Fixes format 2

* Fixes format 4475757

* Fixes format 878757854786312378657865785785785785

* Change return to await

* Fixing more issues

* Fixing even more issues

* Fixing even more issues +

* Fixes format
This commit is contained in:
TriForMine
2020-12-30 09:31:11 +01:00
committed by GitHub
parent 6d7aa35d9c
commit 484f86638f
29 changed files with 126 additions and 141 deletions
+15 -16
View File
@@ -36,15 +36,15 @@ export function memberHasPermission(
)
// Removes any edge case undefined
.filter((id) => id)
.reduce((bits, permissions) => {
bits |= BigInt(permissions);
.reduce((bits, perms) => {
bits |= BigInt(perms);
return bits;
}, BigInt(0));
if (permissionBits & BigInt(Permissions.ADMINISTRATOR)) return true;
if (permissionBits && BigInt(Permissions.ADMINISTRATOR)) return true;
return permissions.every((permission) =>
permissionBits & BigInt(Permissions[permission])
permissionBits && BigInt(Permissions[permission])
);
}
@@ -74,10 +74,10 @@ export async function botHasPermission(
return bits;
}, BigInt(0));
if (permissionBits & BigInt(Permissions.ADMINISTRATOR)) return true;
if (permissionBits && BigInt(Permissions.ADMINISTRATOR)) return true;
return permissions.every((permission) =>
permissionBits & BigInt(Permissions[permission])
permissionBits && BigInt(Permissions[permission])
);
}
@@ -134,12 +134,12 @@ export async function hasChannelPermissions(
const denyBits = 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(denyBits) & BigInt(Permissions[perm])) return false;
if (BigInt(denyBits) && BigInt(Permissions[perm])) return false;
// Already allowed perm
if (allowedPermissions.has(perm)) continue;
// This perm is allowed so we save it
if (BigInt(allowBits) & BigInt(Permissions[perm])) {
if (BigInt(allowBits) && BigInt(Permissions[perm])) {
allowedPermissions.add(perm);
}
}
@@ -153,17 +153,17 @@ export async function hasChannelPermissions(
for (const overwrite of rolesOverwrites) {
const allowBits = overwrite.allow;
// This perm is allowed so we save it
if (BigInt(allowBits) & BigInt(Permissions[perm])) {
if (BigInt(allowBits) && BigInt(Permissions[perm])) {
allowedPermissions.add(perm);
break;
}
const denyBits = overwrite.deny;
// If this role denies it we need to save and check if another role allows it, allows > deny
if (BigInt(denyBits) & BigInt(Permissions[perm])) {
if (BigInt(denyBits) && BigInt(Permissions[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(Permissions[perm])
BigInt(o.allow) && BigInt(Permissions[perm])
);
if (isAllowed) continue;
// This permission is in fact denied. Since Roles overrule everything below here we can cancel ou here
@@ -179,9 +179,9 @@ export async function hasChannelPermissions(
// 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(denyBits) & BigInt(Permissions[perm])) return false;
if (BigInt(denyBits) && BigInt(Permissions[perm])) return false;
// This perm is allowed so we save it
if (BigInt(allowBits) & BigInt(Permissions[perm])) {
if (BigInt(allowBits) && BigInt(Permissions[perm])) {
allowedPermissions.add(perm);
}
}
@@ -191,15 +191,14 @@ export async function hasChannelPermissions(
if (permissions.every((perm) => allowedPermissions.has(perm))) return true;
// Some permission was not explicitly allowed so we default to checking role perms directly
const hasPerms = await memberIDHasPermission(memberID, guild.id, permissions);
return hasPerms;
return await memberIDHasPermission(memberID, guild.id, permissions);
}
/** This function converts a bitwise string to permission strings */
export function calculatePermissions(permissionBits: bigint) {
return Object.keys(Permissions).filter((perm) => {
if (Number(perm)) return false;
return permissionBits & BigInt(Permissions[perm as Permission]);
return permissionBits && BigInt(Permissions[perm as Permission]);
}) as Permission[];
}