aboutsummaryrefslogtreecommitdiff
path: root/discord
diff options
context:
space:
mode:
authorRapptz <[email protected]>2021-08-10 22:10:45 -0400
committerRapptz <[email protected]>2021-08-10 22:10:45 -0400
commitdc9c224b54bf87731ca7d07417aad8af116f8ab7 (patch)
tree322668a8bda187165e7ebf2bba72269f4aea4e1d /discord
parentRemove unused log lines in HTTPClient (diff)
downloaddiscord.py-dc9c224b54bf87731ca7d07417aad8af116f8ab7.tar.xz
discord.py-dc9c224b54bf87731ca7d07417aad8af116f8ab7.zip
Undo coercion of partial DMChannel to PartialMessageable
Diffstat (limited to 'discord')
-rw-r--r--discord/channel.py24
-rw-r--r--discord/state.py2
2 files changed, 21 insertions, 5 deletions
diff --git a/discord/channel.py b/discord/channel.py
index 6d0fe081..a2282731 100644
--- a/discord/channel.py
+++ b/discord/channel.py
@@ -1669,6 +1669,9 @@ class StoreChannel(discord.abc.GuildChannel, Hashable):
await self._edit(options, reason=reason)
+DMC = TypeVar('DMC', bound='DMChannel')
+
+
class DMChannel(discord.abc.Messageable, Hashable):
"""Represents a Discord direct message channel.
@@ -1692,8 +1695,10 @@ class DMChannel(discord.abc.Messageable, Hashable):
Attributes
----------
- recipient: :class:`User`
+ recipient: Optional[:class:`User`]
The user you are participating with in the direct message channel.
+ If this channel is received through the gateway, the recipient information
+ may not be always available.
me: :class:`ClientUser`
The user presenting yourself.
id: :class:`int`
@@ -1704,7 +1709,7 @@ class DMChannel(discord.abc.Messageable, Hashable):
def __init__(self, *, me: ClientUser, state: ConnectionState, data: DMChannelPayload):
self._state: ConnectionState = state
- self.recipient: User = state.store_user(data['recipients'][0])
+ self.recipient: Optional[User] = state.store_user(data['recipients'][0])
self.me: ClientUser = me
self.id: int = int(data['id'])
@@ -1712,10 +1717,21 @@ class DMChannel(discord.abc.Messageable, Hashable):
return self
def __str__(self) -> str:
- return f'Direct Message with {self.recipient}'
+ if self.recipient:
+ return f'Direct Message with {self.recipient}'
+ return 'Direct Message with Unknown User'
def __repr__(self) -> str:
- return f'<DMChannel id={self.id} recipient={self.recipient!r}>'
+ return f'<DMChannel id={self.id} recipient={self.recipient!r}>'
+
+ @classmethod
+ def _from_message(cls: Type[DMC], state: ConnectionState, channel_id: int) -> DMC:
+ self: DMC = cls.__new__(cls)
+ self._state = state
+ self.id = channel_id
+ self.recipient = None
+ self.me = state.user # type: ignore
+ return self
@property
def type(self) -> ChannelType:
diff --git a/discord/state.py b/discord/state.py
index 4a09a9cc..7acf64b2 100644
--- a/discord/state.py
+++ b/discord/state.py
@@ -405,7 +405,7 @@ class ConnectionState:
try:
guild = self._get_guild(int(data['guild_id']))
except KeyError:
- channel = PartialMessageable(state=self, id=channel_id, type=ChannelType.private)
+ channel = DMChannel._from_message(self, channel_id)
guild = None
else:
channel = guild and guild._resolve_channel(channel_id)