aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2016-09-21 03:53:24 -0400
committerRapptz <[email protected]>2016-09-21 04:22:12 -0400
commit13f85b3292909a08e62ec9c2c1b3be18ce37b741 (patch)
treea7132669a5eb617ca217f3a65107930ac0f45a57
parentUpgrade requirements to stable aiohttp. (diff)
downloaddiscord.py-13f85b3292909a08e62ec9c2c1b3be18ce37b741.tar.xz
discord.py-13f85b3292909a08e62ec9c2c1b3be18ce37b741.zip
Make Roles totally ordered.
This also fixes a bug with Member.top_role that chose the wrong role should they have the same position.
-rw-r--r--discord/member.py2
-rw-r--r--discord/role.py60
2 files changed, 50 insertions, 12 deletions
diff --git a/discord/member.py b/discord/member.py
index 5130a241..eaf70db8 100644
--- a/discord/member.py
+++ b/discord/member.py
@@ -197,6 +197,6 @@ class Member(User):
"""
if self.roles:
- roles = sorted(self.roles, key=lambda r: r.position, reverse=True)
+ roles = sorted(self.roles, reverse=True)
return roles[0]
return None
diff --git a/discord/role.py b/discord/role.py
index 460eb74f..9e3d0f3c 100644
--- a/discord/role.py
+++ b/discord/role.py
@@ -34,17 +34,25 @@ class Role(Hashable):
Supported Operations:
- +-----------+------------------------------------+
- | Operation | Description |
- +===========+====================================+
- | x == y | Checks if two roles are equal. |
- +-----------+------------------------------------+
- | x != y | Checks if two roles are not equal. |
- +-----------+------------------------------------+
- | hash(x) | Return the role's hash. |
- +-----------+------------------------------------+
- | str(x) | Returns the role's name. |
- +-----------+------------------------------------+
+ +-----------+------------------------------------------------------------------+
+ | Operation | Description |
+ +===========+==================================================================+
+ | x == y | Checks if two roles are equal. |
+ +-----------+------------------------------------------------------------------+
+ | x != y | Checks if two roles are not equal. |
+ +-----------+------------------------------------------------------------------+
+ | x > y | Checks if a role is higher than another in the hierarchy. |
+ +-----------+------------------------------------------------------------------+
+ | x < y | Checks if a role is lower than another in the hierarchy. |
+ +-----------+------------------------------------------------------------------+
+ | x >= y | Checks if a role is higher or equal to another in the hierarchy. |
+ +-----------+------------------------------------------------------------------+
+ | x <= y | Checks if a role is lower or equal to another in the hierarchy. |
+ +-----------+------------------------------------------------------------------+
+ | hash(x) | Return the role's hash. |
+ +-----------+------------------------------------------------------------------+
+ | str(x) | Returns the role's name. |
+ +-----------+------------------------------------------------------------------+
Attributes
----------
@@ -80,6 +88,36 @@ class Role(Hashable):
def __str__(self):
return self.name
+ def __lt__(self, other):
+ if not isinstance(other, Role) or not isinstance(self, Role):
+ return NotImplemented
+
+ if self.server != other.server:
+ raise RuntimeError('cannot compare roles from two different servers.')
+
+ if self.position < other.position:
+ return True
+
+ if self.position == other.position:
+ return self.id > other.id
+
+ return False
+
+ def __le__(self, other):
+ r = Role.__lt__(other, self)
+ if r is NotImplemented:
+ return NotImplemented
+ return not r
+
+ def __gt__(self, other):
+ return Role.__lt__(other, self)
+
+ def __ge__(self, other):
+ r = Role.__lt__(self, other)
+ if r is NotImplemented:
+ return NotImplemented
+ return not r
+
def _update(self, **kwargs):
self.id = kwargs.get('id')
self.name = kwargs.get('name')