3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-06-08 08:05:03 +00:00

More work on permission utils and eager permission checks

This commit is contained in:
Dragory 2020-08-07 01:21:31 +03:00
parent 8af64a6944
commit 6d4a7cdafd
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
11 changed files with 189 additions and 79 deletions

View 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;
}