Skip to content

Commit

Permalink
Pyrofork: Refactor giveaway
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <[email protected]>
  • Loading branch information
wulan17 committed Nov 25, 2023
1 parent 8259c0c commit 37c185e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
34 changes: 25 additions & 9 deletions pyrogram/types/messages_and_media/giveaway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
)
2 changes: 1 addition & 1 deletion pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 37c185e

Please sign in to comment.