2021-06-30 04:56:56 +02:00
|
|
|
import { Guild, GuildMember, Permissions, Role, Snowflake } from "discord.js";
|
2020-08-07 01:21:31 +03:00
|
|
|
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)) {
|
2020-08-07 01:21:31 +03:00
|
|
|
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);
|
2020-08-07 01:21:31 +03:00
|
|
|
if (!targetRole) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-31 21:12:24 +02:00
|
|
|
const memberRoles = member.roles.cache;
|
2020-11-09 20:03:57 +02:00
|
|
|
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;
|
2020-08-07 01:21:31 +03:00
|
|
|
if (highest == null) return role;
|
|
|
|
if (role.position > highest.position) return role;
|
|
|
|
return highest;
|
|
|
|
}, null);
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
return highestRoleWithManageRoles && highestRoleWithManageRoles.position > targetRole.position;
|
2020-08-07 01:21:31 +03:00
|
|
|
}
|