Improve generic duck type programming with PartialMessageable

This adds jump_url, permissions_for, and created_at. Luckily, most
cases of this type being constructed already have the guild_id at
creation time.
This commit is contained in:
Rapptz
2022-05-03 10:49:52 -04:00
parent e9c7c09ebf
commit 8699d2139a
8 changed files with 59 additions and 11 deletions

View File

@@ -2611,13 +2611,16 @@ class PartialMessageable(discord.abc.Messageable, Hashable):
-----------
id: :class:`int`
The channel ID associated with this partial messageable.
guild_id: Optional[:class:`int`]
The guild ID associated with this partial messageable.
type: Optional[:class:`ChannelType`]
The channel type associated with this partial messageable, if given.
"""
def __init__(self, state: ConnectionState, id: int, type: Optional[ChannelType] = None):
def __init__(self, state: ConnectionState, id: int, guild_id: Optional[int] = None, type: Optional[ChannelType] = None):
self._state: ConnectionState = state
self.id: int = id
self.guild_id: Optional[int] = guild_id
self.type: Optional[ChannelType] = type
def __repr__(self) -> str:
@@ -2626,6 +2629,40 @@ class PartialMessageable(discord.abc.Messageable, Hashable):
async def _get_channel(self) -> PartialMessageable:
return self
@property
def jump_url(self) -> str:
""":class:`str`: Returns a URL that allows the client to jump to the channel."""
if self.guild_id is None:
return f'https://discord.com/channels/@me/{self.id}'
return f'https://discord.com/channels/{self.guild_id}/{self.id}'
@property
def created_at(self) -> datetime.datetime:
""":class:`datetime.datetime`: Returns the direct message channel's creation time in UTC."""
return utils.snowflake_time(self.id)
def permissions_for(self, obj: Any = None, /) -> Permissions:
"""Handles permission resolution for a :class:`User`.
This function is there for compatibility with other channel types.
Since partial messageables cannot reasonably have the concept of
permissions, this will always return :meth:`Permissions.none`.
Parameters
-----------
obj: :class:`User`
The user to check permissions for. This parameter is ignored
but kept for compatibility with other ``permissions_for`` methods.
Returns
--------
:class:`Permissions`
The resolved permissions.
"""
return Permissions.none()
def get_partial_message(self, message_id: int, /) -> PartialMessage:
"""Creates a :class:`PartialMessage` from the message ID.