3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-07 19:17:19 +00:00
zeppelin/backend/src/utils/canAssignRole.ts
Dragory 45e3fe2ef0
chore: esm imports
This will make merging this into 'next' much easier.
2024-08-11 21:58:52 +03:00

28 lines
1 KiB
TypeScript

import { Guild, GuildMember, PermissionsBitField, Role, Snowflake } from "discord.js";
import { getMissingPermissions } from "./getMissingPermissions.js";
import { hasDiscordPermissions } from "./hasDiscordPermissions.js";
export function canAssignRole(guild: Guild, member: GuildMember, roleId: string) {
if (getMissingPermissions(member.permissions, PermissionsBitField.Flags.ManageRoles)) {
return false;
}
if (roleId === guild.id) {
return false;
}
const targetRole = guild.roles.cache.get(roleId as Snowflake);
if (!targetRole) {
return false;
}
const memberRoles = member.roles.cache;
const highestRoleWithManageRoles = memberRoles.reduce<Role | null>((highest, role) => {
if (!hasDiscordPermissions(role.permissions, PermissionsBitField.Flags.ManageRoles)) return highest;
if (highest == null) return role;
if (role.position > highest.position) return role;
return highest;
}, null);
return highestRoleWithManageRoles && highestRoleWithManageRoles.position > targetRole.position;
}