From 9137d92f67a6d50782e199ca6d6558c1ea6015e2 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sun, 13 Dec 2015 22:53:48 -0500 Subject: [PATCH] All data classes now support !=, == and str(obj). --- discord/channel.py | 35 +++++++++++++++++++++++++++++++++-- discord/colour.py | 23 ++++++++++++++--------- discord/invite.py | 18 +++++++++++++++++- discord/mixins.py | 34 ++++++++++++++++++++++++++++++++++ discord/permissions.py | 16 ++++++++++++++++ discord/role.py | 18 +++++++++++++++++- discord/server.py | 18 +++++++++++++++++- discord/user.py | 4 +--- 8 files changed, 149 insertions(+), 17 deletions(-) create mode 100644 discord/mixins.py diff --git a/discord/channel.py b/discord/channel.py index edb45817e..41445a12c 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -28,12 +28,25 @@ from . import utils from .permissions import Permissions from .enums import ChannelType from collections import namedtuple +from .mixins import EqualityComparable Overwrites = namedtuple('Overwrites', 'id allow deny type') -class Channel: +class Channel(EqualityComparable): """Represents a Discord server channel. + Supported Operations: + + +-----------+---------------------------------------+ + | Operation | Description | + +===========+=======================================+ + | x == y | Checks if two channels are equal. | + +-----------+---------------------------------------+ + | x != y | Checks if two channels are not equal. | + +-----------+---------------------------------------+ + | str(x) | Returns the channel's name. | + +-----------+---------------------------------------+ + Attributes ----------- name : str @@ -63,6 +76,9 @@ class Channel: self.update(**kwargs) self.voice_members = [] + def __str__(self): + return self.name + def update(self, **kwargs): self.name = kwargs.get('name') self.server = kwargs.get('server') @@ -179,9 +195,21 @@ class Channel: return base -class PrivateChannel: +class PrivateChannel(EqualityComparable): """Represents a Discord private channel. + Supported Operations: + + +-----------+-------------------------------------------------+ + | Operation | Description | + +===========+=================================================+ + | x == y | Checks if two channels are equal. | + +-----------+-------------------------------------------------+ + | x != y | Checks if two channels are not equal. | + +-----------+-------------------------------------------------+ + | str(x) | Returns the string "Direct Message with " | + +-----------+-------------------------------------------------+ + Attributes ---------- user : :class:`User` @@ -197,6 +225,9 @@ class PrivateChannel: self.id = id self.is_private = True + def __str__(self): + return 'Direct Message with {0.name}'.format(self.user) + def permissions_for(user): """Handles permission resolution for a :class:`User`. diff --git a/discord/colour.py b/discord/colour.py index 169e6a04a..0b63a6089 100644 --- a/discord/colour.py +++ b/discord/colour.py @@ -32,13 +32,15 @@ class Colour(object): Supported operations: - +-----------+--------------------------------------+ - | Operation | Description | - +===========+======================================+ - | x == y | Checks if two colours are equal. | - +-----------+--------------------------------------+ - | x != y | Checks if two colours are not equal. | - +-----------+--------------------------------------+ + +-----------+----------------------------------------+ + | Operation | Description | + +===========+========================================+ + | x == y | Checks if two colours are equal. | + +-----------+----------------------------------------+ + | x != y | Checks if two colours are not equal. | + +-----------+----------------------------------------+ + | str(x) | Returns the hex format for the colour. | + +-----------+----------------------------------------+ Attributes ------------ @@ -53,10 +55,13 @@ class Colour(object): return (self.value >> (8 * byte)) & 0xff def __eq__(self, other): - return self.value == getattr(other, 'value', None) + return isinstance(other, Colour) and self.value == other.value def __ne__(self, other): - return isinstance(other, Colour) and self.value != other.value + return not self.__eq__(other) + + def __str__(self): + return '#' + format(self.value, 'x') @property def r(self): diff --git a/discord/invite.py b/discord/invite.py index 5b59e20a3..bc1ae3fb3 100644 --- a/discord/invite.py +++ b/discord/invite.py @@ -26,13 +26,26 @@ DEALINGS IN THE SOFTWARE. from .user import User from .utils import parse_time +from .mixins import EqualityComparable -class Invite(object): +class Invite(EqualityComparable): """Represents a Discord :class:`Server` or :class:`Channel` invite. Depending on the way this object was created, some of the attributes can have a value of ``None``. + Supported Operations: + + +-----------+--------------------------------------+ + | Operation | Description | + +===========+======================================+ + | x == y | Checks if two invites are equal. | + +-----------+--------------------------------------+ + | x != y | Checks if two invites are not equal. | + +-----------+--------------------------------------+ + | str(x) | Returns the invite's URL. | + +-----------+--------------------------------------+ + Attributes ----------- max_age : int @@ -75,6 +88,9 @@ class Invite(object): self.inviter = None if inviter_data is None else User(**inviter_data) self.channel = kwargs.get('channel') + def __str__(self): + return self.url + @property def id(self): """Returns the proper code portion of the invite.""" diff --git a/discord/mixins.py b/discord/mixins.py new file mode 100644 index 000000000..8a11a7c17 --- /dev/null +++ b/discord/mixins.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +""" +The MIT License (MIT) + +Copyright (c) 2015 Rapptz + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +""" + +class EqualityComparable: + def __eq__(self, other): + return isinstance(other, self.__class__) and other.id == self.id + + def __ne__(self, other): + if isinstance(other, self.__class__): + return other.id != self.id + return True diff --git a/discord/permissions.py b/discord/permissions.py index 5f826a1fd..079bb4643 100644 --- a/discord/permissions.py +++ b/discord/permissions.py @@ -37,6 +37,16 @@ def create_permission_masks(cls): class Permissions(object): """Wraps up the Discord permission value. + Supported operations: + + +-----------+------------------------------------------+ + | Operation | Description | + +===========+==========================================+ + | x == y | Checks if two permissions are equal. | + +-----------+------------------------------------------+ + | x != y | Checks if two permissions are not equal. | + +-----------+------------------------------------------+ + Class attributes: .. attribute:: NONE @@ -80,6 +90,12 @@ class Permissions(object): def __init__(self, permissions=0, **kwargs): self.value = permissions + def __eq__(self, other): + return isinstance(other, Permissions) and self.value == other.value + + def __ne__(self, other): + return not self.__eq__(other) + @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 760a0bced..93d8e3b50 100644 --- a/discord/role.py +++ b/discord/role.py @@ -26,10 +26,23 @@ DEALINGS IN THE SOFTWARE. from .permissions import Permissions from .colour import Colour +from .mixins import EqualityComparable -class Role(object): +class Role(EqualityComparable): """Represents a Discord role in a :class:`Server`. + Supported Operations: + + +-----------+------------------------------------+ + | Operation | Description | + +===========+====================================+ + | x == y | Checks if two roles are equal. | + +-----------+------------------------------------+ + | x != y | Checks if two roles are not equal. | + +-----------+------------------------------------+ + | str(x) | Returns the role's name. | + +-----------+------------------------------------+ + Attributes ---------- id : str @@ -53,6 +66,9 @@ class Role(object): self._is_everyone = kwargs.get('everyone', False) self.update(**kwargs) + def __str__(self): + return self.name + def update(self, **kwargs): self.id = kwargs.get('id') self.name = kwargs.get('name') diff --git a/discord/server.py b/discord/server.py index 456f4ca90..bc0a7ba8f 100644 --- a/discord/server.py +++ b/discord/server.py @@ -29,10 +29,23 @@ from .role import Role from .member import Member from .channel import Channel from .enums import ServerRegion, Status +from .mixins import EqualityComparable -class Server: +class Server(EqualityComparable): """Represents a Discord server. + Supported Operations: + + +-----------+--------------------------------------+ + | Operation | Description | + +===========+======================================+ + | x == y | Checks if two servers are equal. | + +-----------+--------------------------------------+ + | x != y | Checks if two servers are not equal. | + +-----------+--------------------------------------+ + | str(x) | Returns the server's name. | + +-----------+--------------------------------------+ + Attributes ---------- name : str @@ -70,6 +83,9 @@ class Server: self.members = [] self._from_data(kwargs) + def __str__(self): + return self.name + def _update_voice_state(self, data): user_id = data.get('user_id') member = utils.find(lambda m: m.id == user_id, self.members) diff --git a/discord/user.py b/discord/user.py index 973a9ae2d..455fca14f 100644 --- a/discord/user.py +++ b/discord/user.py @@ -64,9 +64,7 @@ class User(object): return isinstance(other, User) and other.id == self.id def __ne__(self, other): - if isinstance(other, User): - return other.id != self.id - return False + return not self.__eq__(other) @property def avatar_url(self):