mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-20 20:16:06 -04:00
feat: handle more than one permission when checking
This commit is contained in:
+1
-1
@@ -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",
|
||||
|
||||
@@ -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
@@ -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
@@ -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]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user