mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-05-11 08:19:54 +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 .permissions import Permissions
|
||||||
from .enums import ChannelType
|
from .enums import ChannelType
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
from .mixins import EqualityComparable
|
||||||
|
|
||||||
Overwrites = namedtuple('Overwrites', 'id allow deny type')
|
Overwrites = namedtuple('Overwrites', 'id allow deny type')
|
||||||
|
|
||||||
class Channel:
|
class Channel(EqualityComparable):
|
||||||
"""Represents a Discord server channel.
|
"""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
|
Attributes
|
||||||
-----------
|
-----------
|
||||||
name : str
|
name : str
|
||||||
@ -63,6 +76,9 @@ class Channel:
|
|||||||
self.update(**kwargs)
|
self.update(**kwargs)
|
||||||
self.voice_members = []
|
self.voice_members = []
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
def update(self, **kwargs):
|
def update(self, **kwargs):
|
||||||
self.name = kwargs.get('name')
|
self.name = kwargs.get('name')
|
||||||
self.server = kwargs.get('server')
|
self.server = kwargs.get('server')
|
||||||
@ -179,9 +195,21 @@ class Channel:
|
|||||||
|
|
||||||
return base
|
return base
|
||||||
|
|
||||||
class PrivateChannel:
|
class PrivateChannel(EqualityComparable):
|
||||||
"""Represents a Discord private channel.
|
"""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
|
Attributes
|
||||||
----------
|
----------
|
||||||
user : :class:`User`
|
user : :class:`User`
|
||||||
@ -197,6 +225,9 @@ class PrivateChannel:
|
|||||||
self.id = id
|
self.id = id
|
||||||
self.is_private = True
|
self.is_private = True
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return 'Direct Message with {0.name}'.format(self.user)
|
||||||
|
|
||||||
def permissions_for(user):
|
def permissions_for(user):
|
||||||
"""Handles permission resolution for a :class:`User`.
|
"""Handles permission resolution for a :class:`User`.
|
||||||
|
|
||||||
|
@ -32,13 +32,15 @@ class Colour(object):
|
|||||||
|
|
||||||
Supported operations:
|
Supported operations:
|
||||||
|
|
||||||
+-----------+--------------------------------------+
|
+-----------+----------------------------------------+
|
||||||
| Operation | Description |
|
| Operation | Description |
|
||||||
+===========+======================================+
|
+===========+========================================+
|
||||||
| x == y | Checks if two colours are equal. |
|
| x == y | Checks if two colours are equal. |
|
||||||
+-----------+--------------------------------------+
|
+-----------+----------------------------------------+
|
||||||
| x != y | Checks if two colours are not equal. |
|
| x != y | Checks if two colours are not equal. |
|
||||||
+-----------+--------------------------------------+
|
+-----------+----------------------------------------+
|
||||||
|
| str(x) | Returns the hex format for the colour. |
|
||||||
|
+-----------+----------------------------------------+
|
||||||
|
|
||||||
Attributes
|
Attributes
|
||||||
------------
|
------------
|
||||||
@ -53,10 +55,13 @@ class Colour(object):
|
|||||||
return (self.value >> (8 * byte)) & 0xff
|
return (self.value >> (8 * byte)) & 0xff
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return self.value == getattr(other, 'value', None)
|
return isinstance(other, Colour) and self.value == other.value
|
||||||
|
|
||||||
def __ne__(self, other):
|
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
|
@property
|
||||||
def r(self):
|
def r(self):
|
||||||
|
@ -26,13 +26,26 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
from .user import User
|
from .user import User
|
||||||
from .utils import parse_time
|
from .utils import parse_time
|
||||||
|
from .mixins import EqualityComparable
|
||||||
|
|
||||||
class Invite(object):
|
class Invite(EqualityComparable):
|
||||||
"""Represents a Discord :class:`Server` or :class:`Channel` invite.
|
"""Represents a Discord :class:`Server` or :class:`Channel` invite.
|
||||||
|
|
||||||
Depending on the way this object was created, some of the attributes can
|
Depending on the way this object was created, some of the attributes can
|
||||||
have a value of ``None``.
|
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
|
Attributes
|
||||||
-----------
|
-----------
|
||||||
max_age : int
|
max_age : int
|
||||||
@ -75,6 +88,9 @@ class Invite(object):
|
|||||||
self.inviter = None if inviter_data is None else User(**inviter_data)
|
self.inviter = None if inviter_data is None else User(**inviter_data)
|
||||||
self.channel = kwargs.get('channel')
|
self.channel = kwargs.get('channel')
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.url
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
"""Returns the proper code portion of the invite."""
|
"""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):
|
class Permissions(object):
|
||||||
"""Wraps up the Discord permission value.
|
"""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:
|
Class attributes:
|
||||||
|
|
||||||
.. attribute:: NONE
|
.. attribute:: NONE
|
||||||
@ -80,6 +90,12 @@ class Permissions(object):
|
|||||||
def __init__(self, permissions=0, **kwargs):
|
def __init__(self, permissions=0, **kwargs):
|
||||||
self.value = permissions
|
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
|
@classmethod
|
||||||
def none(cls):
|
def none(cls):
|
||||||
"""A factory method that creates a :class:`Permission` with all
|
"""A factory method that creates a :class:`Permission` with all
|
||||||
|
@ -26,10 +26,23 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
from .permissions import Permissions
|
from .permissions import Permissions
|
||||||
from .colour import Colour
|
from .colour import Colour
|
||||||
|
from .mixins import EqualityComparable
|
||||||
|
|
||||||
class Role(object):
|
class Role(EqualityComparable):
|
||||||
"""Represents a Discord role in a :class:`Server`.
|
"""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
|
Attributes
|
||||||
----------
|
----------
|
||||||
id : str
|
id : str
|
||||||
@ -53,6 +66,9 @@ class Role(object):
|
|||||||
self._is_everyone = kwargs.get('everyone', False)
|
self._is_everyone = kwargs.get('everyone', False)
|
||||||
self.update(**kwargs)
|
self.update(**kwargs)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
def update(self, **kwargs):
|
def update(self, **kwargs):
|
||||||
self.id = kwargs.get('id')
|
self.id = kwargs.get('id')
|
||||||
self.name = kwargs.get('name')
|
self.name = kwargs.get('name')
|
||||||
|
@ -29,10 +29,23 @@ from .role import Role
|
|||||||
from .member import Member
|
from .member import Member
|
||||||
from .channel import Channel
|
from .channel import Channel
|
||||||
from .enums import ServerRegion, Status
|
from .enums import ServerRegion, Status
|
||||||
|
from .mixins import EqualityComparable
|
||||||
|
|
||||||
class Server:
|
class Server(EqualityComparable):
|
||||||
"""Represents a Discord server.
|
"""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
|
Attributes
|
||||||
----------
|
----------
|
||||||
name : str
|
name : str
|
||||||
@ -70,6 +83,9 @@ class Server:
|
|||||||
self.members = []
|
self.members = []
|
||||||
self._from_data(kwargs)
|
self._from_data(kwargs)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
def _update_voice_state(self, data):
|
def _update_voice_state(self, data):
|
||||||
user_id = data.get('user_id')
|
user_id = data.get('user_id')
|
||||||
member = utils.find(lambda m: m.id == user_id, self.members)
|
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
|
return isinstance(other, User) and other.id == self.id
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
if isinstance(other, User):
|
return not self.__eq__(other)
|
||||||
return other.id != self.id
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def avatar_url(self):
|
def avatar_url(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user