Add GiveawayResult type

This commit is contained in:
KurimuzonAkuma 2023-12-28 00:54:46 +03:00
parent cbf7a66508
commit 19f05bc790
7 changed files with 174 additions and 18 deletions

View File

@ -466,6 +466,7 @@ def pyrogram_api():
MyBoost
BoostsStatus
Giveaway
GiveawayResult
GiftCode
""",
bot_keyboards="""

View File

@ -72,5 +72,8 @@ class MessageMediaType(AutoName):
GIVEAWAY = auto()
"Giveaway media"
GIVEAWAY_RESULT = auto()
"Giveaway result media"
STORY = auto()
"Story media"

View File

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

View File

@ -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"
]

View File

@ -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__(

View File

@ -0,0 +1,134 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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
)

View File

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