Skip to content

Commit 0d5af89

Browse files
committed
Add methods to check if a role is above another, and if a user has a role above the roles of another user
1 parent d8788f5 commit 0d5af89

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

ackCord/src/main/scala/net/katsstuff/ackcord/data/guild.scala

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,41 @@ case class GuildMember(
344344
*/
345345
def channelPermissions(channelId: ChannelId)(implicit c: CacheSnapshot): Permission =
346346
permissionsWithOverrides(permissions, channelId)
347+
348+
/**
349+
* Check if this user has any roles above the passed in roles.
350+
*/
351+
def hasRoleAbove(others: Seq[RoleId])(implicit c: CacheSnapshot): Boolean = {
352+
guild.exists { guild =>
353+
val ownerId = guild.ownerId
354+
if (this.userId == ownerId) true
355+
else {
356+
def maxRolesPosition(roles: Seq[RoleId]): Int = {
357+
val poses = roles.flatMap(_.resolve(this.guildId)).map(_.position)
358+
if (poses.isEmpty) 0
359+
else poses.max
360+
}
361+
362+
maxRolesPosition(this.roleIds) > maxRolesPosition(others)
363+
}
364+
}
365+
}
366+
367+
/**
368+
* Check if this user has any roles above the passed in roles.
369+
*/
370+
def hasRoleAbove(other: GuildMember)(implicit c: CacheSnapshot): Boolean = {
371+
guild.exists { guild =>
372+
val ownerId = guild.ownerId
373+
if (this.userId == ownerId) true
374+
else if (other.userId == ownerId) false
375+
else {
376+
hasRoleAbove(other.roleIds)
377+
}
378+
}
379+
}
347380
}
381+
348382
/**
349383
* An emoji in a guild.
350384
* @param id The id of the emoji.
@@ -375,7 +409,7 @@ sealed trait PresenceContent {
375409
/**
376410
* The presence of someone playing a game
377411
*/
378-
case class PresenceGame(name: String) extends PresenceContent
412+
case class PresenceGame(name: String) extends PresenceContent
379413

380414
/**
381415
* The presence of someone streaming

ackCord/src/main/scala/net/katsstuff/ackcord/data/permission.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,14 @@ case class Role(
177177
* Mention this role.
178178
*/
179179
def mention: String = s"<@&$id>"
180+
181+
/**
182+
* Check if this role is above another role.
183+
*/
184+
def isAbove(other: Role): Boolean = this.position > other.position
185+
186+
/**
187+
* Check if this role is below another role.
188+
*/
189+
def isBelow(other: Role): Boolean = this.position < other.position
180190
}

0 commit comments

Comments
 (0)