From 7ef785e5aa2bae662cf0e87da56dd725235a7e85 Mon Sep 17 00:00:00 2001 From: wulan17 Date: Sun, 26 Nov 2023 01:37:51 +0700 Subject: [PATCH] Pyrofork: Refactor giveaway Signed-off-by: wulan17 --- pyrogram/types/messages_and_media/giveaway.py | 34 ++++++++++++++----- pyrogram/types/messages_and_media/message.py | 2 +- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/pyrogram/types/messages_and_media/giveaway.py b/pyrogram/types/messages_and_media/giveaway.py index 8a1abce1d..8239e531c 100644 --- a/pyrogram/types/messages_and_media/giveaway.py +++ b/pyrogram/types/messages_and_media/giveaway.py @@ -28,8 +28,8 @@ class Giveaway(Object): """A giveaway. Parameters: - channel_ids (List of ``int``): - List Unique identifier of the channel(s) which host the giveaway. + channels (List of :obj:`~pyrogram.types.Chat`): + List of channel(s) which host the giveaway. quantity (``int``): Quantity of the giveaway prize. @@ -45,42 +45,58 @@ class Giveaway(Object): countries_iso2 (List of ``str``, *optional*): List of ISO country codes which eligible to join the giveaway. + + private_channel_ids (List of ``int``, *optional*): + List of Unique channel identifier of private channel which host the giveaway. """ def __init__( self, *, client: "pyrogram.Client" = None, - channel_ids: List[int], + channels: List["types.Chat"], quantity: int, months: int, expire_date: datetime, new_subscribers : bool, - countries_iso2: List[str] = None + countries_iso2: List[str] = None, + private_channel_ids: List[int] = None ): super().__init__(client) - self.channel_ids = channel_ids + self.channels = channels self.quantity = quantity self.months = months self.expire_date = expire_date self.new_subscribers = new_subscribers self.countries_iso2 = countries_iso2 + self.private_channel_ids = private_channel_ids @staticmethod - def _parse(client, message: "raw.types.Message") -> "Giveaway": + async def _parse(client, message: "raw.types.Message") -> "Giveaway": giveaway: "raw.types.MessageMediaGiveaway" = message.media - channel_ids = [] + channels = [] + private_ids = [] for raw_channel_id in giveaway.channels: channel_id = utils.get_channel_id(raw_channel_id) - channel_ids.append(channel_id) + try: + chat = await client.invoke( + raw.functions.channels.GetChannels( + id=[await client.resolve_peer(channel_id)] + ) + ) + except Exception: + private_ids.append(channel_id) + else: + channels.append(types.Chat._parse_chat(client, chat.chats[0])) return Giveaway( - channel_ids=channel_ids, + channels=channels, quantity=giveaway.quantity, months=giveaway.months, expire_date=utils.timestamp_to_datetime(giveaway.until_date), new_subscribers=giveaway.only_new_subscribers, countries_iso2=giveaway.countries_iso2 if len(giveaway.countries_iso2) > 0 else None, + private_channel_ids=private_ids if len(private_ids) > 0 else None, client=client ) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 994514106..1be3fab18 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -869,7 +869,7 @@ async def _parse( game = types.Game._parse(client, message) media_type = enums.MessageMediaType.GAME elif isinstance(media, raw.types.MessageMediaGiveaway): - giveaway = types.Giveaway._parse(client, message) + giveaway = await types.Giveaway._parse(client, message) media_type = enums.MessageMediaType.GIVEAWAY elif isinstance(media, raw.types.MessageMediaStory): story = types.MessageStory._parse(media)