diff --git a/discord/channel.py b/discord/channel.py index 6d0fe0813..a22827318 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'' + return f'' + + @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 4a09a9cc0..7acf64b2f 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)