mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-05-09 15:29:57 +00:00
All data classes now support !=, == and str(obj).
This commit is contained in:
parent
ab46afee1d
commit
9137d92f67
@ -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 <User>" |
|
||||
+-----------+-------------------------------------------------+
|
||||
|
||||
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`.
|
||||
|
||||
|
@ -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):
|
||||
|
@ -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."""
|
||||
|
34
discord/mixins.py
Normal file
34
discord/mixins.py
Normal file
@ -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
|
@ -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
|
||||
|
@ -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')
|
||||
|
@ -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)
|
||||
|
@ -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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user