feat: handle more than one permission when checking

This commit is contained in:
Paul Makles
2022-04-28 15:00:28 +01:00
parent bceb106d59
commit 54157d5cd6
5 changed files with 20 additions and 13 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "revolt.js",
"version": "6.0.0-rc.19",
"version": "6.0.0-rc.20",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"module": "esm/index.js",
+6 -3
View File
@@ -653,11 +653,14 @@ export class Channel {
/**
* Check whether we have a given permission in a channel
* @param permission Permission Name
* @param permission Permission Names
* @returns Whether we have this permission
*/
@computed havePermission(permission: keyof typeof Permission) {
return bitwiseAndEq(this.permission, Permission[permission]);
@computed havePermission(...permission: (keyof typeof Permission)[]) {
return bitwiseAndEq(
this.permission,
...permission.map((x) => Permission[x]),
);
}
}
+3 -3
View File
@@ -143,16 +143,16 @@ export class Member {
/**
* Check whether a member has a certain permission against a certain object
* @param target Target object to check permissions against
* @param permission Permission string to check for
* @param permission Permission names to check for
* @returns Whether the member has this permission
*/
@computed hasPermission(
target: Server | Channel,
permission: keyof typeof Permission,
...permission: (keyof typeof Permission)[]
) {
return bitwiseAndEq(
this.getPermissions(target),
Permission[permission],
...permission.map((x) => Permission[x]),
);
}
}
+6 -3
View File
@@ -389,11 +389,14 @@ export class Server {
/**
* Check whether we have a given permission in a server
* @param permission Permission Name
* @param permission Permission Names
* @returns Whether we have this permission
*/
@computed havePermission(permission: keyof typeof Permission) {
return bitwiseAndEq(this.permission, Permission[permission]);
@computed havePermission(...permission: (keyof typeof Permission)[]) {
return bitwiseAndEq(
this.permission,
...permission.map((x) => Permission[x]),
);
}
}
+4 -3
View File
@@ -10,10 +10,11 @@ import {
/**
* Check whether `b` is present in `a`
* @param a Input A
* @param b Input B
* @param b Inputs (OR'd together)
*/
export function bitwiseAndEq(a: number, b: number) {
return Long.fromNumber(a).and(b).eq(b);
export function bitwiseAndEq(a: number, ...b: number[]) {
const value = b.reduce((prev, cur) => prev.or(cur), Long.fromNumber(0));
return value.and(a).eq(value);
}
/**