From 19f05bc790a051754029f0528b9079eab2f0e5cf Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Thu, 28 Dec 2023 00:54:46 +0300 Subject: [PATCH] Add GiveawayResult type --- compiler/docs/compiler.py | 1 + pyrogram/enums/message_media_type.py | 3 + pyrogram/filters.py | 33 +++-- pyrogram/types/messages_and_media/__init__.py | 9 +- pyrogram/types/messages_and_media/giveaway.py | 5 +- .../messages_and_media/giveaway_result.py | 134 ++++++++++++++++++ pyrogram/types/messages_and_media/message.py | 7 + 7 files changed, 174 insertions(+), 18 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 fdf418e9..b20f6865 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -466,6 +466,7 @@ def pyrogram_api(): MyBoost BoostsStatus Giveaway + GiveawayResult GiftCode """, bot_keyboards=""" diff --git a/pyrogram/enums/message_media_type.py b/pyrogram/enums/message_media_type.py index be8397f0..c6844716 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() "Story media" diff --git a/pyrogram/filters.py b/pyrogram/filters.py index 3f6dec19..a61156cd 100644 --- a/pyrogram/filters.py +++ b/pyrogram/filters.py @@ -337,6 +337,17 @@ giveaway = create(giveaway_filter) """Filter messages that contain :obj:`~pyrogram.types.Giveaway` objects.""" +# endregion + +# region giveaway_result_filter +async def giveaway_result_filter(_, __, m: Message): + return bool(m.giveaway_result) + + +giveaway_result = create(giveaway_result_filter) +"""Filter messages that contain :obj:`~pyrogram.types.GiveawayResult` objects.""" + + # endregion # region gift_code_filter @@ -359,17 +370,6 @@ requested_chats = create(requested_chats_filter) """Filter service messages for request chats.""" -# endregion - -# region story_filter -async def story_filter(_, __, m: Message): - return bool(m.story) - - -story = create(story_filter) -"""Filter messages that contain :obj:`~pyrogram.types.Story` objects.""" - - # endregion # region video_filter @@ -546,6 +546,17 @@ forum = create(forum_filter) """Filter messages sent in forums.""" +# endregion + +# region story_filter +async def story_filter(_, __, m: Message): + return bool(m.story) + + +story = create(story_filter) +"""Filter messages that contain :obj:`~pyrogram.types.Story` objects.""" + + # endregion # region new_chat_members_filter diff --git a/pyrogram/types/messages_and_media/__init__.py b/pyrogram/types/messages_and_media/__init__.py index 68ba2c2b..73cd07dc 100644 --- a/pyrogram/types/messages_and_media/__init__.py +++ b/pyrogram/types/messages_and_media/__init__.py @@ -32,6 +32,7 @@ from .general_forum_topic_unhidden import GeneralTopicUnhidden from .game import Game from .gift_code import GiftCode from .giveaway import Giveaway +from .giveaway_result import GiveawayResult from .location import Location from .message import Message from .message_entity import MessageEntity @@ -55,8 +56,8 @@ from .my_boost import MyBoost __all__ = [ "Animation", "Audio", "BoostsStatus", "Contact", "Document", "ForumTopic", "ForumTopicCreated", "ForumTopicClosed", "ForumTopicReopened", "ForumTopicEdited", "GeneralTopicHidden", - "GeneralTopicUnhidden", "Game", "GiftCode", "Giveaway", "Location", "Message", "MessageEntity", - "Photo", "Thumbnail", "StrippedThumbnail", "Story", "Poll", "PollOption", "Sticker", "Venue", - "Video", "VideoNote", "Voice", "WebPage", "Dice", "Reaction", "WebAppData", "MessageReactions", - "MyBoost" + "GeneralTopicUnhidden", "Game", "GiftCode", "Giveaway", "GiveawayResult", "Location", + "Message", "MessageEntity", "Photo", "Thumbnail", "StrippedThumbnail", "Story", "Poll", + "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice", "WebPage", "Dice", "Reaction", + "WebAppData", "MessageReactions", "MyBoost" ] diff --git a/pyrogram/types/messages_and_media/giveaway.py b/pyrogram/types/messages_and_media/giveaway.py index b82a8dc0..60e807af 100644 --- a/pyrogram/types/messages_and_media/giveaway.py +++ b/pyrogram/types/messages_and_media/giveaway.py @@ -45,14 +45,13 @@ class Giveaway(Object): Prize description. only_new_subscribers (``bool``, *optional*): - True if the giveaway is for new subscribers only. + True, if this giveaway is for new subscribers only. only_for_countries (List of ``str`` , *optional*): Countries for which the giveaway is available in iso2 format. winners_are_visible (``bool``, *optional*): - True if the winners is visible. - + True, if this giveaway winners is visible. """ def __init__( 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 00000000..950f0448 --- /dev/null +++ b/pyrogram/types/messages_and_media/giveaway_result.py @@ -0,0 +1,134 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram 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. +# +# Pyrogram 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 Pyrogram. If not, see . + +from datetime import datetime +from typing import List + +import pyrogram +from pyrogram import errors, raw, utils +from pyrogram import types +from ..object import Object + + +class GiveawayResult(Object): + """An giveaway result. + + Parameters: + chat (List of :obj:`~pyrogram.types.Chat`): + Channel which host the giveaway. + + quantity (``int``): + Total number of subscriptions in this giveaway. + + winners_count (``int``): + Number of winners who claimed their gift. + + unclaimed_count (``int``): + Unclaimed giveaway subscriptions count. + + winners (List of :obj:`~pyrogram.types.User`): + A list of giveaway winners. + + months (``int``): + Number of months for which a subscription is given. + + until_date (:py:obj:`~datetime.datetime`): + Date when the giveaway will end. + + launch_message_id (``int``): + Identifier of the original message with the giveaway. + + launch_message (:obj:`~pyrogram.types.Message`, *optional*): + Returns the original giveaway start message. + If the channel is private, returns None + + description (``str``, *optional*): + Prize description. + + only_new_subscribers (``bool``, *optional*): + True, if this giveaway is for new subscribers only. + + is_refunded (``bool``, *optional*): + True, if this giveaway was refunded. + """ + + def __init__( + self, + *, + client: "pyrogram.Client" = None, + chat: "types.Chat", + quantity: int, + winners_count: int, + unclaimed_count: int, + winners: List["types.User"], + months: int, + until_date: datetime, + launch_message_id: int, + launch_message: "types.Message" = None, + description: str = None, + only_new_subscribers: bool = None, + is_refunded: bool = None + ): + super().__init__(client) + + self.chat = chat + self.quantity = quantity + self.winners_count = winners_count + self.unclaimed_count = unclaimed_count + self.winners = winners + self.months = months + self.until_date = until_date + self.launch_message_id = launch_message_id + self.launch_message = launch_message + self.description = description + self.only_new_subscribers = only_new_subscribers + self.is_refunded = is_refunded + + @staticmethod + async def _parse( + client, + giveaway_result: "raw.types.MessageMediaGiveawayResults", + users: dict, + chats: dict + ) -> "GiveawayResult": + launch_message = None + + try: + launch_message = await client.get_messages( + utils.get_channel_id(giveaway_result.channel_id), + giveaway_result.launch_msg_id, + replies=0 + ) + except (errors.ChannelPrivate, errors.ChannelInvalid): + pass + + return GiveawayResult( + chat=types.Chat._parse_channel_chat(client, chats[giveaway_result.channel_id]), + quantity=giveaway_result.winners_count + giveaway_result.unclaimed_count, + winners_count=giveaway_result.winners_count, + unclaimed_count=giveaway_result.unclaimed_count, + winners=types.List(types.User._parse(client, users.get(i)) for i in giveaway_result.winners) or None, + months=giveaway_result.months, + until_date=utils.timestamp_to_datetime(giveaway_result.until_date), + launch_message_id=giveaway_result.launch_msg_id, + only_new_subscribers=getattr(giveaway_result, "only_new_subscribers", None), + is_refunded=getattr(giveaway_result, "refunded", None), + launch_message=launch_message, + description=getattr(giveaway_result, "prize_description", None) or None, + client=client + ) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index ce4c636a..58fc0af2 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -420,6 +420,7 @@ class Message(Object, Update): animation: "types.Animation" = None, game: "types.Game" = None, giveaway: "types.Giveaway" = None, + giveaway_result: "types.GiveawayResult" = None, story: "types.Story" = None, video: "types.Video" = None, voice: "types.Voice" = None, @@ -518,6 +519,7 @@ class Message(Object, Update): self.animation = animation self.game = game self.giveaway = giveaway + self.giveaway_result = giveaway_result self.story = story self.video = video self.voice = voice @@ -842,6 +844,7 @@ class Message(Object, Update): venue = None game = None giveaway = None + giveaway_result = None story = None audio = None voice = None @@ -878,6 +881,9 @@ class Message(Object, Update): elif isinstance(media, raw.types.MessageMediaGiveaway): giveaway = types.Giveaway._parse(client, media, chats) media_type = enums.MessageMediaType.GIVEAWAY + elif isinstance(media, raw.types.MessageMediaGiveawayResults): + giveaway_result = await types.GiveawayResult._parse(client, media, users, chats) + media_type = enums.MessageMediaType.GIVEAWAY_RESULT elif isinstance(media, raw.types.MessageMediaStory): if media.story: story = await types.Story._parse(client, media.story, users, chats, media.peer) @@ -1017,6 +1023,7 @@ class Message(Object, Update): animation=animation, game=game, giveaway=giveaway, + giveaway_result=giveaway_result, story=story, video=video, video_note=video_note,