Skip to content

Commit

Permalink
Add extension to get a member's highest role
Browse files Browse the repository at this point in the history
  • Loading branch information
abitofevrything committed Sep 23, 2024
1 parent 20443f1 commit 9e60d2e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/src/extensions/role.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ extension PartialRoleExtensions on PartialRole {
return roleMention(id);
}
}

/// Extensions on [List]s of [Role]s.
extension RoleList on List<Role> {
/// Compare two [Role]s by their positions.
static int compare(Role a, Role b) {
final position = a.position.compareTo(b.position);
if (position != 0) return position;

return a.id.compareTo(b.id);
}

/// The highest role in this list.
Role get highest => reduce((a, b) => compare(a, b) < 0 ? b : a);

/// The roles in this list, sorted from lowest to highest.
List<Role> get sorted => List.of(this)..sort(compare);
}

0 comments on commit 9e60d2e

Please sign in to comment.