3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-18 15:00:00 +00:00
zeppelin/backend/src/utils/canAssignRole.ts

29 lines
1,016 B
TypeScript
Raw Normal View History

2021-06-30 04:56:56 +02:00
import { Guild, GuildMember, Permissions, Role, Snowflake } from "discord.js";
import { getMissingPermissions } from "./getMissingPermissions";
import { hasDiscordPermissions } from "./hasDiscordPermissions";
2021-05-31 21:12:24 +02:00
export function canAssignRole(guild: Guild, member: GuildMember, roleId: string) {
if (getMissingPermissions(member.permissions, Permissions.FLAGS.MANAGE_ROLES)) {
return false;
}
if (roleId === guild.id) {
return false;
}
2021-06-30 04:56:56 +02:00
const targetRole = guild.roles.cache.get(roleId as Snowflake);
if (!targetRole) {
return false;
}
2021-05-31 21:12:24 +02:00
const memberRoles = member.roles.cache;
const highestRoleWithManageRoles = memberRoles.reduce<Role | null>((highest, role) => {
2021-05-31 21:12:24 +02:00
if (!hasDiscordPermissions(role.permissions, Permissions.FLAGS.MANAGE_ROLES)) return highest;
if (highest == null) return role;
if (role.position > highest.position) return role;
return highest;
}, null);
return highestRoleWithManageRoles && highestRoleWithManageRoles.position > targetRole.position;
}