mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-06 03:47:29 +00:00
Add support for specifying the type of a generic discord.Object
Co-authored-by: Danny <1695103+Rapptz@users.noreply.github.com>
This commit is contained in:
parent
842d6b4fbb
commit
1787867320
@ -34,12 +34,9 @@ class EqualityComparable:
|
||||
id: int
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
return isinstance(other, self.__class__) and other.id == self.id
|
||||
|
||||
def __ne__(self, other: object) -> bool:
|
||||
if isinstance(other, self.__class__):
|
||||
return other.id != self.id
|
||||
return True
|
||||
return other.id == self.id
|
||||
return NotImplemented
|
||||
|
||||
|
||||
class Hashable(EqualityComparable):
|
||||
|
@ -25,16 +25,18 @@ DEALINGS IN THE SOFTWARE.
|
||||
from __future__ import annotations
|
||||
|
||||
from .mixins import Hashable
|
||||
from .utils import snowflake_time
|
||||
from .utils import snowflake_time, MISSING
|
||||
|
||||
from typing import (
|
||||
SupportsInt,
|
||||
TYPE_CHECKING,
|
||||
Type,
|
||||
Union,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import datetime
|
||||
from . import abc
|
||||
|
||||
SupportsIntCast = Union[SupportsInt, str, bytes, bytearray]
|
||||
|
||||
@ -77,18 +79,34 @@ class Object(Hashable):
|
||||
-----------
|
||||
id: :class:`int`
|
||||
The ID of the object.
|
||||
type: Type[:class:`abc.Snowflake`]
|
||||
The discord.py model type of the object, if not specified, defaults to this class.
|
||||
|
||||
.. note::
|
||||
|
||||
In instances where there are multiple applicable types, use a shared base class.
|
||||
for example, both :class:`Member` and :class:`User` are subclasses of :class:`abc.User`.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
|
||||
def __init__(self, id: SupportsIntCast):
|
||||
def __init__(self, id: SupportsIntCast, *, type: Type[abc.Snowflake] = MISSING):
|
||||
try:
|
||||
id = int(id)
|
||||
except ValueError:
|
||||
raise TypeError(f'id parameter must be convertible to int not {id.__class__!r}') from None
|
||||
else:
|
||||
self.id = id
|
||||
self.id: int = id
|
||||
self.type: Type[abc.Snowflake] = type or self.__class__
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f'<Object id={self.id!r}>'
|
||||
return f'<Object id={self.id!r} type={self.type!r}>'
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if isinstance(other, self.type):
|
||||
return self.id == other.id
|
||||
return NotImplemented
|
||||
|
||||
__hash__ = Hashable.__hash__
|
||||
|
||||
@property
|
||||
def created_at(self) -> datetime.datetime:
|
||||
|
Loading…
x
Reference in New Issue
Block a user