diff --git a/discord/channel.py b/discord/channel.py index 983b4fb95..5f53b837f 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -28,11 +28,11 @@ from . import utils from .permissions import Permissions from .enums import ChannelType from collections import namedtuple -from .mixins import EqualityComparable +from .mixins import Hashable Overwrites = namedtuple('Overwrites', 'id allow deny type') -class Channel(EqualityComparable): +class Channel(Hashable): """Represents a Discord server channel. Supported Operations: @@ -44,6 +44,8 @@ class Channel(EqualityComparable): +-----------+---------------------------------------+ | x != y | Checks if two channels are not equal. | +-----------+---------------------------------------+ + | hash(x) | Returns the channel's hash. | + +-----------+---------------------------------------+ | str(x) | Returns the channel's name. | +-----------+---------------------------------------+ @@ -196,7 +198,7 @@ class Channel(EqualityComparable): return base -class PrivateChannel(EqualityComparable): +class PrivateChannel(Hashable): """Represents a Discord private channel. Supported Operations: @@ -208,6 +210,8 @@ class PrivateChannel(EqualityComparable): +-----------+-------------------------------------------------+ | x != y | Checks if two channels are not equal. | +-----------+-------------------------------------------------+ + | hash(x) | Returns the channel's hash. | + +-----------+-------------------------------------------------+ | str(x) | Returns the string "Direct Message with " | +-----------+-------------------------------------------------+ diff --git a/discord/colour.py b/discord/colour.py index 0b63a6089..67137cb4d 100644 --- a/discord/colour.py +++ b/discord/colour.py @@ -39,6 +39,8 @@ class Colour(object): +-----------+----------------------------------------+ | x != y | Checks if two colours are not equal. | +-----------+----------------------------------------+ + | hash(x) | Return the colour's hash. | + +-----------+----------------------------------------+ | str(x) | Returns the hex format for the colour. | +-----------+----------------------------------------+ @@ -63,6 +65,9 @@ class Colour(object): def __str__(self): return '#' + format(self.value, 'x') + def __hash__(self): + return hash(self.value) + @property def r(self): """Returns the red component of the colour.""" diff --git a/discord/invite.py b/discord/invite.py index bc1ae3fb3..ad6b2673b 100644 --- a/discord/invite.py +++ b/discord/invite.py @@ -26,9 +26,9 @@ DEALINGS IN THE SOFTWARE. from .user import User from .utils import parse_time -from .mixins import EqualityComparable +from .mixins import Hashable -class Invite(EqualityComparable): +class Invite(Hashable): """Represents a Discord :class:`Server` or :class:`Channel` invite. Depending on the way this object was created, some of the attributes can @@ -43,6 +43,8 @@ class Invite(EqualityComparable): +-----------+--------------------------------------+ | x != y | Checks if two invites are not equal. | +-----------+--------------------------------------+ + | hash(x) | Return the invite's hash. | + +-----------+--------------------------------------+ | str(x) | Returns the invite's URL. | +-----------+--------------------------------------+ diff --git a/discord/mixins.py b/discord/mixins.py index 8a11a7c17..cdbd67d76 100644 --- a/discord/mixins.py +++ b/discord/mixins.py @@ -32,3 +32,7 @@ class EqualityComparable: if isinstance(other, self.__class__): return other.id != self.id return True + +class Hashable(EqualityComparable): + def __hash__(self): + return hash(self.id) diff --git a/discord/permissions.py b/discord/permissions.py index d41d22d9c..ee0ad5553 100644 --- a/discord/permissions.py +++ b/discord/permissions.py @@ -36,6 +36,8 @@ class Permissions(object): +-----------+------------------------------------------+ | x != y | Checks if two permissions are not equal. | +-----------+------------------------------------------+ + | hash(x) | Return the permission's hash. | + +-----------+------------------------------------------+ Attributes ----------- @@ -57,6 +59,9 @@ class Permissions(object): def __ne__(self, other): return not self.__eq__(other) + def __hash__(self): + return hash(self.value) + @classmethod def none(cls): """A factory method that creates a :class:`Permission` with all diff --git a/discord/role.py b/discord/role.py index eb44795a0..a78a4af74 100644 --- a/discord/role.py +++ b/discord/role.py @@ -26,9 +26,9 @@ DEALINGS IN THE SOFTWARE. from .permissions import Permissions from .colour import Colour -from .mixins import EqualityComparable +from .mixins import Hashable -class Role(EqualityComparable): +class Role(Hashable): """Represents a Discord role in a :class:`Server`. Supported Operations: @@ -40,6 +40,8 @@ class Role(EqualityComparable): +-----------+------------------------------------+ | x != y | Checks if two roles are not equal. | +-----------+------------------------------------+ + | hash(x) | Return the role's hash. | + +-----------+------------------------------------+ | str(x) | Returns the role's name. | +-----------+------------------------------------+ diff --git a/discord/server.py b/discord/server.py index f9f1333ce..0a6b7091f 100644 --- a/discord/server.py +++ b/discord/server.py @@ -29,9 +29,9 @@ from .role import Role from .member import Member from .channel import Channel from .enums import ServerRegion, Status -from .mixins import EqualityComparable +from .mixins import Hashable -class Server(EqualityComparable): +class Server(Hashable): """Represents a Discord server. Supported Operations: @@ -43,6 +43,8 @@ class Server(EqualityComparable): +-----------+--------------------------------------+ | x != y | Checks if two servers are not equal. | +-----------+--------------------------------------+ + | hash(x) | Returns the server's hash. | + +-----------+--------------------------------------+ | str(x) | Returns the server's name. | +-----------+--------------------------------------+ diff --git a/discord/user.py b/discord/user.py index a5ef2edb2..d2f149c34 100644 --- a/discord/user.py +++ b/discord/user.py @@ -36,6 +36,8 @@ class User: +-----------+------------------------------------+ | x != y | Checks if two users are not equal. | +-----------+------------------------------------+ + | hash(x) | Return the user's hash. | + +-----------+------------------------------------+ | str(x) | Returns the user's name. | +-----------+------------------------------------+ @@ -66,6 +68,9 @@ class User: def __ne__(self, other): return not self.__eq__(other) + def __hash__(self): + return hash(self.id) + @property def avatar_url(self): """Returns a friendly URL version of the avatar variable the user has. An empty string if