|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2021-present Pycord Development |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +copy of this software and associated documentation files (the "Software"), |
| 8 | +to deal in the Software without restriction, including without limitation |
| 9 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +Software is furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +DEALINGS IN THE SOFTWARE. |
| 23 | +""" |
| 24 | + |
| 25 | +import datetime |
| 26 | +import logging |
| 27 | +from abc import ABC, abstractmethod |
| 28 | +from typing import TYPE_CHECKING, Generic |
| 29 | + |
| 30 | +from typing_extensions import Self, TypeVar, override |
| 31 | + |
| 32 | +from ..abc import Snowflake |
| 33 | +from ..enums import ChannelType, try_enum |
| 34 | +from ..permissions import Permissions |
| 35 | +from ..types.channel import Channel as ChannelPayload |
| 36 | +from ..utils import snowflake_time |
| 37 | + |
| 38 | +_log = logging.getLogger(__name__) |
| 39 | + |
| 40 | +if TYPE_CHECKING: |
| 41 | + from ..app.state import ConnectionState |
| 42 | + |
| 43 | + |
| 44 | +P = TypeVar("P", bound="ChannelPayload") |
| 45 | + |
| 46 | + |
| 47 | +class BaseChannel(ABC, Generic[P]): |
| 48 | + __slots__: tuple[str, ...] = ("id", "_type", "_state", "_data") # pyright: ignore [reportIncompatibleUnannotatedOverride] |
| 49 | + |
| 50 | + def __init__(self, id: int, state: "ConnectionState"): |
| 51 | + self.id: int = id |
| 52 | + self._state: ConnectionState = state |
| 53 | + self._data: P = {} # type: ignore |
| 54 | + |
| 55 | + async def _update(self, data: P) -> None: |
| 56 | + self._type: int = data["type"] |
| 57 | + self._data = self._data | data # type: ignore |
| 58 | + |
| 59 | + @classmethod |
| 60 | + async def _from_data(cls, *, data: P, state: "ConnectionState", **kwargs) -> Self: |
| 61 | + if kwargs: |
| 62 | + _log.warning("Unexpected keyword arguments passed to %s._from_data: %r", cls.__name__, kwargs) |
| 63 | + self = cls(int(data["id"]), state) |
| 64 | + await self._update(data) |
| 65 | + return self |
| 66 | + |
| 67 | + @property |
| 68 | + def type(self) -> ChannelType: |
| 69 | + """The channel's Discord channel type.""" |
| 70 | + return try_enum(ChannelType, self._type) |
| 71 | + |
| 72 | + async def _get_channel(self) -> Self: |
| 73 | + return self |
| 74 | + |
| 75 | + @property |
| 76 | + def created_at(self) -> datetime.datetime: |
| 77 | + """The channel's creation time in UTC.""" |
| 78 | + return snowflake_time(self.id) |
| 79 | + |
| 80 | + @abstractmethod |
| 81 | + @override |
| 82 | + def __repr__(self) -> str: ... |
| 83 | + |
| 84 | + @property |
| 85 | + @abstractmethod |
| 86 | + def jump_url(self) -> str: ... |
| 87 | + |
| 88 | + @abstractmethod |
| 89 | + def permissions_for(self, obj: Snowflake, /) -> Permissions: ... |
0 commit comments