From 295a50838499901d7fa69f7ad73ef5e2de2e8a5a Mon Sep 17 00:00:00 2001 From: wulan17 Date: Tue, 26 Dec 2023 14:50:33 +0700 Subject: [PATCH] Pyrofork: Add GiveawayResult and refactor Giveaway Signed-off-by: wulan17 --- compiler/docs/compiler.py | 1 + pyrogram/enums/message_media_type.py | 3 + pyrogram/types/messages_and_media/__init__.py | 3 +- pyrogram/types/messages_and_media/giveaway.py | 28 ++--- .../messages_and_media/giveaway_result.py | 111 ++++++++++++++++++ pyrogram/types/messages_and_media/message.py | 10 ++ 6 files changed, 141 insertions(+), 15 deletions(-) create mode 100644 pyrogram/types/messages_and_media/giveaway_result.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index b8c144f5e..b94fc69c3 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -464,6 +464,7 @@ def get_title_list(s: str) -> list: StickerSet Game Giveaway + GiveawayResult MessageStory WebPage WebPageEmpty diff --git a/pyrogram/enums/message_media_type.py b/pyrogram/enums/message_media_type.py index 22d45be57..722d34642 100644 --- a/pyrogram/enums/message_media_type.py +++ b/pyrogram/enums/message_media_type.py @@ -72,5 +72,8 @@ class MessageMediaType(AutoName): GIVEAWAY = auto() "Giveaway media" + GIVEAWAY_RESULT = auto() + "Giveaway result media" + STORY = auto() "Forwarded story media" diff --git a/pyrogram/types/messages_and_media/__init__.py b/pyrogram/types/messages_and_media/__init__.py index 47ab06a2f..6e48724ea 100644 --- a/pyrogram/types/messages_and_media/__init__.py +++ b/pyrogram/types/messages_and_media/__init__.py @@ -23,6 +23,7 @@ from .document import Document from .game import Game from .giveaway import Giveaway +from .giveaway_result import GiveawayResult from .location import Location from .media_area import MediaArea from .media_area_channel_post import MediaAreaChannelPost @@ -56,7 +57,7 @@ from .exported_story_link import ExportedStoryLink __all__ = [ - "Animation", "Audio", "Contact", "Document", "Game", "Giveaway", "Location", "MediaArea", "MediaAreaChannelPost", "MediaAreaCoordinates", "Message", "MessageEntity", "Photo", "Thumbnail", + "Animation", "Audio", "Contact", "Document", "Game", "Giveaway", "GiveawayResult", "Location", "MediaArea", "MediaAreaChannelPost", "MediaAreaCoordinates", "Message", "MessageEntity", "Photo", "Thumbnail", "StrippedThumbnail", "Poll", "PollOption", "Sticker", "StickerSet", "Venue", "Video", "VideoNote", "Voice", "WebPage", "WebPageEmpty", "WebPagePreview", "Dice", "Reaction", "WebAppData", "MessageReactions", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoryForwardHeader", "StoriesPrivacyRules", "ExportedStoryLink" ] diff --git a/pyrogram/types/messages_and_media/giveaway.py b/pyrogram/types/messages_and_media/giveaway.py index 10561362b..217da0b10 100644 --- a/pyrogram/types/messages_and_media/giveaway.py +++ b/pyrogram/types/messages_and_media/giveaway.py @@ -30,7 +30,7 @@ class Giveaway(Object): """A giveaway. Parameters: - channels (List of :obj:`~pyrogram.types.Chat`): + chats (List of :obj:`~pyrogram.types.Chat`): List of channel(s) which host the giveaway. quantity (``int``): @@ -45,7 +45,7 @@ class Giveaway(Object): new_subscribers (``bool``): True, if the giveaway only for new subscribers. - countries_iso2 (List of ``str``, *optional*): + allowed_countries (List of ``str``, *optional*): List of ISO country codes which eligible to join the giveaway. private_channel_ids (List of ``int``, *optional*): @@ -56,51 +56,51 @@ def __init__( self, *, client: "pyrogram.Client" = None, - channels: List["types.Chat"], + chats: List["types.Chat"], quantity: int, months: int, expire_date: datetime, new_subscribers : bool, - countries_iso2: List[str] = None, + allowed_countries: List[str] = None, private_channel_ids: List[int] = None ): super().__init__(client) - self.channels = channels + self.chats = chats self.quantity = quantity self.months = months self.expire_date = expire_date self.new_subscribers = new_subscribers - self.countries_iso2 = countries_iso2 + self.allowed_countries = allowed_countries self.private_channel_ids = private_channel_ids @staticmethod async def _parse(client, message: "raw.types.Message") -> "Giveaway": giveaway: "raw.types.MessageMediaGiveaway" = message.media - channels = [] + chats = [] private_ids = [] - for raw_channel_id in giveaway.channels: - channel_id = utils.get_channel_id(raw_channel_id) + for raw_chat_id in giveaway.channels: + chat_id = utils.get_channel_id(raw_chat_id) try: chat = await client.invoke( raw.functions.channels.GetChannels( - id=[await client.resolve_peer(channel_id)] + id=[await client.resolve_peer(chat_id)] ) ) except FloodWait as e: await asyncio.sleep(e.value) except Exception: - private_ids.append(channel_id) + private_ids.append(chat_id) else: - channels.append(types.Chat._parse_chat(client, chat.chats[0])) + chats.append(types.Chat._parse_chat(client, chat.chats[0])) return Giveaway( - channels=channels, + chats=chats, 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, + allowed_countries=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/giveaway_result.py b/pyrogram/types/messages_and_media/giveaway_result.py new file mode 100644 index 000000000..5b1aeb8ab --- /dev/null +++ b/pyrogram/types/messages_and_media/giveaway_result.py @@ -0,0 +1,111 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2022-present Mayuri-Chan +# +# This file is part of Pyrofork. +# +# Pyrofork is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrofork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrofork. If not, see . + +import pyrogram + +from datetime import datetime +from pyrogram import raw, types, utils +from ..object import Object +from typing import List + + +class GiveawayResult(Object): + """A giveaway result. + + Parameters: + chat (:obj:`~pyrogram.types.Chat`): + Channel which host the giveaway. + + giveaway_message (:obj:`~pyrogram.types.Message`): + The original giveaway message. + + quantity (``int``): + Quantity of the giveaway prize. + + unclaimed_quantity (``int``): + Quantity of unclaimed giveaway prize. + + winners (List of :obj:`~pyrogram.types.User`): + The giveaway winners. + + months (``int``): + How long the telegram premium last (in month). + + expire_date (:py:obj:`~datetime.datetime`): + Date the giveaway winner(s) choosen. + + new_subscribers (``bool``, *optional*): + True, if the giveaway only for new subscribers. + + is_refunded (``bool``, *optional*): + True, if the giveaway was refunded. + """ + + def __init__( + self, + *, + client: "pyrogram.Client" = None, + chat: "types.Chat", + giveaway_message: "types.Message", + quantity: int, + unclaimed_quantity: int, + winners: List["types.User"], + months: int, + expire_date: datetime, + new_subscribers : bool, + is_refunded: bool = None + ): + super().__init__(client) + + self.chat = chat + self.giveaway_message = giveaway_message + self.quantity = quantity + self.unclaimed_quantity = unclaimed_quantity + self.winners = winners + self.months = months + self.expire_date = expire_date + self.new_subscribers = new_subscribers + self.is_refunded = is_refunded + + @staticmethod + async def _parse(client, message: "raw.types.Message") -> "GiveawayResult": + giveaway_result: "raw.types.MessageMediaGiveawayResults" = message.media + chat_id = utils.get_channel_id(giveaway_result.channel_id) + chat = await client.invoke( + raw.functions.channels.GetChannels( + id=[await client.resolve_peer(chat_id)] + ) + ) + chat = types.Chat._parse_chat(client, chat.chats[0]) + giveaway_message = await client.get_messages(chat_id, giveaway_result.launch_msg_id) + winners = [] + for winner in giveaway_result.winners: + winners.append(await client.get_users(winner)) + + return GiveawayResult( + chat=chat, + giveaway_message=giveaway_message, + quantity=giveaway_result.winners_count, + unclaimed_quantity=giveaway_result.unclaimed_count, + winners=winners, + months=giveaway_result.months, + expire_date=utils.timestamp_to_datetime(giveaway_result.until_date), + new_subscribers=giveaway_result.only_new_subscribers, + is_refunded=giveaway_result.refunded, + client=client + ) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 314b0deb3..a0b4344e7 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -198,6 +198,9 @@ class Message(Object, Update): giveaway (:obj:`~pyrogram.types.Giveaway`, *optional*): Message is a giveaway, information about the giveaway. + giveaway_result (:obj:`~pyrogram.types.GiveawayResult`, *optional*): + Message is a giveaway result, information about the giveaway result. + story (:obj:`~pyrogram.types.MessageStory` | :obj:`~pyrogram.types.Story`, *optional*): Message is a forwarded story, information about the forwarded story. @@ -411,6 +414,7 @@ def __init__( animation: "types.Animation" = None, game: "types.Game" = None, giveaway: "types.Giveaway" = None, + giveaway_result: "types.GiveawayResult" = None, story: Union["types.MessageStory", "types.Story"] = None, video: "types.Video" = None, voice: "types.Voice" = None, @@ -506,6 +510,7 @@ def __init__( self.animation = animation self.game = game self.giveaway = giveaway + self.giveaway_result = giveaway_result self.story = story self.video = video self.voice = voice @@ -849,6 +854,7 @@ async def _parse( venue = None game = None giveaway = None + giveaway_result = None story = None audio = None voice = None @@ -885,6 +891,9 @@ async def _parse( elif isinstance(media, raw.types.MessageMediaGiveaway): giveaway = await types.Giveaway._parse(client, message) media_type = enums.MessageMediaType.GIVEAWAY + elif isinstance(media, raw.types.MessageMediaGiveawayResults): + giveaway_result = await types.GiveawayResult._parse(client, message) + media_type = enums.MessageMediaType.GIVEAWAY_RESULT elif isinstance(media, raw.types.MessageMediaStory): story = await types.MessageStory._parse(client, media) media_type = enums.MessageMediaType.STORY @@ -1018,6 +1027,7 @@ async def _parse( animation=animation, game=game, giveaway=giveaway, + giveaway_result=giveaway_result, story=story, video=video, video_note=video_note,