mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-02-24 15:30:48 +03:00
Create Channel and Group types
This commit is contained in:
parent
da011e4b1d
commit
5081910c08
11
client/src/telethon/_impl/client/types/chat/__init__.py
Normal file
11
client/src/telethon/_impl/client/types/chat/__init__.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from ....session.chat.packed import PackedChat
|
||||||
|
from .channel import Channel
|
||||||
|
from .group import Group
|
||||||
|
from .user import RestrictionReason, User
|
||||||
|
|
||||||
|
Chat = Union[Channel, Group, User]
|
||||||
|
ChatLike = Union[Chat, PackedChat, int, str]
|
||||||
|
|
||||||
|
__all__ = ["Chat", "Channel", "Group", "RestrictionReason", "User"]
|
50
client/src/telethon/_impl/client/types/chat/channel.py
Normal file
50
client/src/telethon/_impl/client/types/chat/channel.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
from typing import Optional, Self, Union
|
||||||
|
|
||||||
|
from ....session.chat.packed import PackedChat, PackedType
|
||||||
|
from ....tl import abcs, types
|
||||||
|
from ..meta import NoPublicConstructor
|
||||||
|
|
||||||
|
|
||||||
|
class Channel(metaclass=NoPublicConstructor):
|
||||||
|
__slots__ = ("_raw",)
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
raw: Union[types.Channel, types.ChannelForbidden],
|
||||||
|
) -> None:
|
||||||
|
self._raw = raw
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_raw(cls, chat: abcs.Chat) -> Self:
|
||||||
|
if isinstance(chat, (types.ChatEmpty, types.Chat, types.ChatForbidden)):
|
||||||
|
raise RuntimeError("cannot create channel from group chat")
|
||||||
|
elif isinstance(chat, (types.Channel, types.ChannelForbidden)):
|
||||||
|
if not chat.broadcast:
|
||||||
|
raise RuntimeError("cannot create group from broadcast channel")
|
||||||
|
return cls._create(chat)
|
||||||
|
else:
|
||||||
|
raise RuntimeError("unexpected case")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def id(self) -> int:
|
||||||
|
return self._raw.id
|
||||||
|
|
||||||
|
def pack(self) -> Optional[PackedChat]:
|
||||||
|
if self._raw.access_hash is None:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return PackedChat(
|
||||||
|
ty=PackedType.GIGAGROUP
|
||||||
|
if getattr(self._raw, "gigagroup", False)
|
||||||
|
else PackedType.BROADCAST,
|
||||||
|
id=self._raw.id,
|
||||||
|
access_hash=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def title(self) -> str:
|
||||||
|
return getattr(self._raw, "title", None) or ""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def username(self) -> Optional[str]:
|
||||||
|
return getattr(self._raw, "username", None)
|
58
client/src/telethon/_impl/client/types/chat/group.py
Normal file
58
client/src/telethon/_impl/client/types/chat/group.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
from typing import Optional, Self, Union
|
||||||
|
|
||||||
|
from ....session.chat.packed import PackedChat, PackedType
|
||||||
|
from ....tl import abcs, types
|
||||||
|
from ..meta import NoPublicConstructor
|
||||||
|
|
||||||
|
|
||||||
|
class Group(metaclass=NoPublicConstructor):
|
||||||
|
__slots__ = ("_raw",)
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
raw: Union[
|
||||||
|
types.ChatEmpty,
|
||||||
|
types.Chat,
|
||||||
|
types.ChatForbidden,
|
||||||
|
types.Channel,
|
||||||
|
types.ChannelForbidden,
|
||||||
|
],
|
||||||
|
) -> None:
|
||||||
|
self._raw = raw
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _from_raw(cls, chat: abcs.Chat) -> Self:
|
||||||
|
if isinstance(chat, (types.ChatEmpty, types.Chat, types.ChatForbidden)):
|
||||||
|
return cls._create(chat)
|
||||||
|
elif isinstance(chat, (types.Channel, types.ChannelForbidden)):
|
||||||
|
if chat.broadcast:
|
||||||
|
raise RuntimeError("cannot create group from broadcast channel")
|
||||||
|
return cls._create(chat)
|
||||||
|
else:
|
||||||
|
raise RuntimeError("unexpected case")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def id(self) -> int:
|
||||||
|
return self._raw.id
|
||||||
|
|
||||||
|
def pack(self) -> Optional[PackedChat]:
|
||||||
|
if isinstance(self._raw, (types.ChatEmpty, types.Chat, types.ChatForbidden)):
|
||||||
|
return PackedChat(ty=PackedType.CHAT, id=self._raw.id, access_hash=None)
|
||||||
|
elif self._raw.access_hash is None:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return PackedChat(
|
||||||
|
ty=PackedType.MEGAGROUP, id=self._raw.id, access_hash=None
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def title(self) -> str:
|
||||||
|
return getattr(self._raw, "title", None) or ""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def username(self) -> Optional[str]:
|
||||||
|
return getattr(self._raw, "username", None)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_megagroup(self) -> bool:
|
||||||
|
return isinstance(self._raw, (types.Channel, types.ChannelForbidden))
|
Loading…
Reference in New Issue
Block a user