mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-22 01:05: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
28
backend/src/utils/canAssignRole.ts
Normal file
28
backend/src/utils/canAssignRole.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { Constants, Guild, Member, Role } from "eris";
|
||||
import { getMissingPermissions } from "./getMissingPermissions";
|
||||
import { hasDiscordPermissions } from "./hasDiscordPermissions";
|
||||
|
||||
export function canAssignRole(guild: Guild, member: Member, roleId: string) {
|
||||
if (getMissingPermissions(member.permission, Constants.Permissions.manageRoles)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (roleId === guild.id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const targetRole = guild.roles.get(roleId);
|
||||
if (!targetRole) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const memberRoles = member.roles.map(_roleId => guild.roles.get(_roleId));
|
||||
const highestRoleWithManageRoles: Role = memberRoles.reduce((highest, role) => {
|
||||
if (!hasDiscordPermissions(role.permissions, Constants.Permissions.manageRoles)) return highest;
|
||||
if (highest == null) return role;
|
||||
if (role.position > highest.position) return role;
|
||||
return highest;
|
||||
}, null);
|
||||
|
||||
return highestRoleWithManageRoles.position > targetRole.position;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue