diff options
| author | Zeyla Hellyer <[email protected]> | 2018-01-06 14:37:31 -0800 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2018-01-06 14:37:31 -0800 |
| commit | b7542f44306fedb7f79f7b8cd5c8d6afd6ccb7ad (patch) | |
| tree | b5b1d3c6ffef2ce5f2bc6de3d11ac66747d477f7 /src/model | |
| parent | Add latency command to example 05 (diff) | |
| download | serenity-b7542f44306fedb7f79f7b8cd5c8d6afd6ccb7ad.tar.xz serenity-b7542f44306fedb7f79f7b8cd5c8d6afd6ccb7ad.zip | |
Add Member::highest_role_info
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/guild/member.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index c8b0a3b..3622489 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -233,6 +233,46 @@ impl Member { http::edit_member(self.guild_id.0, self.user.read().id.0, &map) } + /// Retrieves the ID and position of the member's highest role in the + /// hierarchy, if they have one. + /// + /// This _may_ return `None` if the user has roles, but they are not present + /// in the cache for cache inconsistency reasons. + /// + /// The "highest role in hierarchy" is defined as the role with the highest + /// position. If two or more roles have the same highest position, then the + /// role with the lowest ID is the highest. + /// + /// # Deadlocking + /// + /// This function will deadlock if you have a write lock to the member's + /// guild. + #[cfg(feature = "cache")] + pub fn highest_role_info(&self) -> Option<(RoleId, i64)> { + let guild = self.guild_id.find()?; + let reader = guild.read(); + + let mut highest = None; + + for role_id in &self.roles { + if let Some(role) = reader.roles.get(&role_id) { + // Skip this role if this role in iteration has: + // + // - a position less than the recorded highest + // - a position equal to the recorded, but a higher ID + if let Some((id, pos)) = highest { + if role.position < pos || (role.position == pos && role.id > id) { + continue; + } + } + + highest = Some((role.id, role.position)); + } + } + + highest + } + /// Kick the member from the guild. /// /// **Note**: Requires the [Kick Members] permission. |