|
26 | 26 |
|
27 | 27 | import time
|
28 | 28 | import asyncio
|
29 |
| - |
| 29 | +from typing import ( |
| 30 | + Any, |
| 31 | + Callable, |
| 32 | + Dict, |
| 33 | + Iterable, |
| 34 | + List, |
| 35 | + Mapping, |
| 36 | + Optional, |
| 37 | + TYPE_CHECKING, |
| 38 | + Tuple, |
| 39 | + Type, |
| 40 | + TypeVar, |
| 41 | + Union, |
| 42 | + overload, |
| 43 | +) |
30 | 44 | import discord.abc
|
31 | 45 | from .permissions import Permissions
|
32 | 46 | from .enums import ChannelType, try_enum, VoiceRegion
|
33 | 47 | from .mixins import Hashable
|
34 | 48 | from . import utils
|
| 49 | +from .object import Object |
35 | 50 | from .asset import Asset
|
36 | 51 | from .errors import ClientException, NoMoreItems, InvalidArgument
|
37 | 52 |
|
| 53 | +if TYPE_CHECKING: |
| 54 | + from .state import ConnectionState |
| 55 | + from .message import Message, PartialMessage |
| 56 | + |
| 57 | + |
38 | 58 | __all__ = (
|
39 | 59 | 'TextChannel',
|
40 | 60 | 'VoiceChannel',
|
@@ -1224,6 +1244,16 @@ def __str__(self):
|
1224 | 1244 | def __repr__(self):
|
1225 | 1245 | return '<DMChannel id={0.id} recipient={0.recipient!r}>'.format(self)
|
1226 | 1246 |
|
| 1247 | + @classmethod |
| 1248 | + def _from_message(cls, state, channel_id: int): |
| 1249 | + self = cls.__new__(cls) |
| 1250 | + self._state = state |
| 1251 | + self.id = channel_id |
| 1252 | + self.recipient = None |
| 1253 | + # state.user won't be None here |
| 1254 | + self.me = state.user # type: ignore |
| 1255 | + return self |
| 1256 | + |
1227 | 1257 | @property
|
1228 | 1258 | def type(self):
|
1229 | 1259 | """:class:`ChannelType`: The channel's Discord type."""
|
@@ -1545,6 +1575,57 @@ async def leave(self):
|
1545 | 1575 |
|
1546 | 1576 | await self._state.http.leave_group(self.id)
|
1547 | 1577 |
|
| 1578 | + |
| 1579 | +class PartialMessageable(discord.abc.Messageable, Hashable): |
| 1580 | + """Represents a partial messageable to aid with working messageable channels when |
| 1581 | + only a channel ID are present. |
| 1582 | + The only way to construct this class is through :meth:`Client.get_partial_messageable`. |
| 1583 | + Note that this class is trimmed down and has no rich attributes. |
| 1584 | + .. versionadded:: 2.0 |
| 1585 | + .. container:: operations |
| 1586 | + .. describe:: x == y |
| 1587 | + Checks if two partial messageables are equal. |
| 1588 | + .. describe:: x != y |
| 1589 | + Checks if two partial messageables are not equal. |
| 1590 | + .. describe:: hash(x) |
| 1591 | + Returns the partial messageable's hash. |
| 1592 | + Attributes |
| 1593 | + ----------- |
| 1594 | + id: :class:`int` |
| 1595 | + The channel ID associated with this partial messageable. |
| 1596 | + type: Optional[:class:`ChannelType`] |
| 1597 | + The channel type associated with this partial messageable, if given. |
| 1598 | + """ |
| 1599 | + |
| 1600 | + def __init__(self, state: 'ConnectionState', id: int, type: Optional[ChannelType] = None): |
| 1601 | + self._state: ConnectionState = state |
| 1602 | + self._channel: Object = Object(id=id) |
| 1603 | + self.id: int = id |
| 1604 | + self.type: Optional[ChannelType] = type |
| 1605 | + |
| 1606 | + async def _get_channel(self) -> Object: |
| 1607 | + return self._channel |
| 1608 | + |
| 1609 | + def get_partial_message(self, message_id: int, /): |
| 1610 | + """Creates a :class:`PartialMessage` from the message ID. |
| 1611 | + This is useful if you want to work with a message and only have its ID without |
| 1612 | + doing an unnecessary API call. |
| 1613 | + Parameters |
| 1614 | + ------------ |
| 1615 | + message_id: :class:`int` |
| 1616 | + The message ID to create a partial message for. |
| 1617 | + Returns |
| 1618 | + --------- |
| 1619 | + :class:`PartialMessage` |
| 1620 | + The partial message. |
| 1621 | + """ |
| 1622 | + |
| 1623 | + from .message import PartialMessage |
| 1624 | + |
| 1625 | + return PartialMessage(channel=self, id=message_id) |
| 1626 | + |
| 1627 | + |
| 1628 | + |
1548 | 1629 | def _channel_factory(channel_type):
|
1549 | 1630 | value = try_enum(ChannelType, channel_type)
|
1550 | 1631 | if value is ChannelType.text:
|
|
0 commit comments