mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-23 09:35:02 +00:00
More work on permission utils and eager permission checks
This commit is contained in:
parent
5a3be1ab03
commit
8e1d39f26b
11 changed files with 189 additions and 79 deletions
29
backend/src/utils/getPermissionNames.ts
Normal file
29
backend/src/utils/getPermissionNames.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { Constants } from "eris";
|
||||
|
||||
const camelCaseToTitleCase = str =>
|
||||
str
|
||||
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
||||
.split(" ")
|
||||
.map(w => w[0].toUpperCase() + w.slice(1))
|
||||
.join(" ");
|
||||
|
||||
const permissionNumberToName: Map<bigint, string> = new Map();
|
||||
const ignoredPermissionConstants = ["all", "allGuild", "allText", "allVoice"];
|
||||
|
||||
for (const key in Constants.Permissions) {
|
||||
if (ignoredPermissionConstants.includes(key)) continue;
|
||||
permissionNumberToName.set(BigInt(Constants.Permissions[key]), camelCaseToTitleCase(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param permissions Bitmask of permissions to get the names for
|
||||
*/
|
||||
export function getPermissionNames(permissions: number | bigint): string[] {
|
||||
const permissionNames = [];
|
||||
for (const [permissionNumber, permissionName] of permissionNumberToName.entries()) {
|
||||
if (BigInt(permissions) & permissionNumber) {
|
||||
permissionNames.push(permissionName);
|
||||
}
|
||||
}
|
||||
return permissionNames;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue