Pyrofork: Refactor giveaway

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2023-11-26 01:37:51 +07:00
parent af8c490c29
commit 7ef785e5aa
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
2 changed files with 26 additions and 10 deletions

View file

@ -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
)

View file

@ -869,7 +869,7 @@ class Message(Object, Update):
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)