Browse Source

Allow PartialMessage to work with DM channels as well

pull/6153/head
Rapptz 4 years ago
parent
commit
52d587d286
  1. 22
      discord/channel.py
  2. 18
      discord/message.py

22
discord/channel.py

@ -1096,6 +1096,28 @@ class DMChannel(discord.abc.Messageable, Hashable):
base.manage_messages = False base.manage_messages = False
return base return base
def get_partial_message(self, message_id):
"""Creates a :class:`PartialMessage` from the message ID.
This is useful if you want to work with a message and only have its ID without
doing an unnecessary API call.
.. versionadded:: 1.6
Parameters
------------
message_id: :class:`int`
The message ID to create a partial message for.
Returns
---------
:class:`PartialMessage`
The partial message.
"""
from .message import PartialMessage
return PartialMessage(channel=self, id=message_id)
class GroupChannel(discord.abc.Messageable, Hashable): class GroupChannel(discord.abc.Messageable, Hashable):
"""Represents a Discord group channel. """Represents a Discord group channel.

18
discord/message.py

@ -1318,7 +1318,7 @@ class PartialMessage(Hashable):
There are two ways to construct this class. The first one is through There are two ways to construct this class. The first one is through
the constructor itself, and the second is via the constructor itself, and the second is via
:meth:`TextChannel.get_partial_message`. :meth:`TextChannel.get_partial_message` or :meth:`DMChannel.get_partial_message`.
Note that this class is trimmed down and has no rich attributes. Note that this class is trimmed down and has no rich attributes.
@ -1340,13 +1340,13 @@ class PartialMessage(Hashable):
Attributes Attributes
----------- -----------
channel: :class:`TextChannel` channel: Union[:class:`TextChannel`, :class:`DMChannel`]
The text channel associated with this partial message. The channel associated with this partial message.
id: :class:`int` id: :class:`int`
The message ID. The message ID.
""" """
__slots__ = ('channel', 'id', '_state') __slots__ = ('channel', 'id', '_cs_guild', '_state')
_exported_names = ( _exported_names = (
'jump_url', 'jump_url',
@ -1365,8 +1365,8 @@ class PartialMessage(Hashable):
) )
def __init__(self, *, channel, id): def __init__(self, *, channel, id):
if channel.type not in (ChannelType.text, ChannelType.news): if channel.type not in (ChannelType.text, ChannelType.news, ChannelType.private):
raise TypeError('Expected TextChannel not %r' % type(channel)) raise TypeError('Expected TextChannel or DMChannel not %r' % type(channel))
self.channel = channel self.channel = channel
self._state = channel._state self._state = channel._state
@ -1389,10 +1389,10 @@ class PartialMessage(Hashable):
""":class:`datetime.datetime`: The partial message's creation time in UTC.""" """:class:`datetime.datetime`: The partial message's creation time in UTC."""
return utils.snowflake_time(self.id) return utils.snowflake_time(self.id)
@property @utils.cached_slot_property('_cs_guild')
def guild(self): def guild(self):
""":class:`Guild`: The guild that the partial message belongs to.""" """Optional[:class:`Guild`]: The guild that the partial message belongs to, if applicable."""
return self.channel.guild return getattr(self.channel, 'guild', None)
async def fetch(self): async def fetch(self):
"""|coro| """|coro|

Loading…
Cancel
Save