Skip to content

Commit

Permalink
Refactor gifts
Browse files Browse the repository at this point in the history
Renamed gifts method (removed _star part)
  • Loading branch information
KurimuzonAkuma committed Jan 5, 2025
1 parent 3286c27 commit 788677b
Show file tree
Hide file tree
Showing 21 changed files with 360 additions and 247 deletions.
35 changes: 19 additions & 16 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,17 @@ def get_title_list(s: str) -> list:
Payments
apply_gift_code
check_gift_code
convert_star_gift
convert_gift
get_payment_form
get_star_gifts
get_user_star_gifts_count
get_user_star_gifts
hide_star_gift
get_available_gifts
get_user_gifts_count
get_user_gifts
hide_gift
send_payment_form
send_star_gift
show_star_gift
transfer_star_gift
upgrade_star_gift
send_gift
show_gift
transfer_gift
upgrade_gift
""",
phone="""
Phone
Expand Down Expand Up @@ -547,7 +547,7 @@ def get_title_list(s: str) -> list:
Dice
Reaction
RefundedPayment
StarGift
Gift
VideoChatScheduled
VideoChatStarted
VideoChatEnded
Expand Down Expand Up @@ -575,7 +575,7 @@ def get_title_list(s: str) -> list:
ContactRegistered
ScreenshotTaken
WriteAccessAllowed
StarGiftAttribute
GiftAttribute
""",
bot_keyboards="""
Bot keyboards
Expand Down Expand Up @@ -845,10 +845,13 @@ def get_title_list(s: str) -> list:
ActiveSession
ActiveSession.reset
""",
star_gift="""
StarGift
StarGift.show
StarGift.hide
gift="""
Gift
Gift.show
Gift.hide
Gift.convert
Gift.upgrade
Gift.transfer
""",
animation="""
Animation
Expand Down Expand Up @@ -914,7 +917,7 @@ def get_title_list(s: str) -> list:
SentCodeType
StoriesPrivacyRules
UserStatus
StarGiftAttributeType
GiftAttributeType
""",
)

Expand Down
6 changes: 3 additions & 3 deletions compiler/docs/template/bound-methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,18 @@ ActiveSession

{active_session_toctree}

StarGift
Gift
--------

.. hlist::
:columns: 2

{star_gift_hlist}
{gift_hlist}

.. toctree::
:hidden:

{star_gift_toctree}
{gift_toctree}

Animation
---------
Expand Down
1 change: 1 addition & 0 deletions compiler/errors/source/400_BAD_REQUEST.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ PASSWORD_RECOVERY_NA The password recovery e-mail is not available
PASSWORD_REQUIRED The two-step verification password is required for this method
PASSWORD_TOO_FRESH_X The two-step verification password was added recently and you are required to wait {value} seconds
PAYMENT_PROVIDER_INVALID The payment provider was not recognised or its token was invalid
PAYMENT_REQUIRED The payment is required
PEERS_LIST_EMPTY The specified list of peers is empty.
PEER_FLOOD The method can't be used because your account is currently limited
PEER_HISTORY_EMPTY Peer history empty
Expand Down
4 changes: 2 additions & 2 deletions pyrogram/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from .profile_color import ProfileColor
from .reply_color import ReplyColor
from .sent_code_type import SentCodeType
from .star_gift_attribute_type import StarGiftAttributeType
from .gift_attribute_type import GiftAttributeType
from .stories_privacy_rules import StoriesPrivacyRules
from .user_status import UserStatus

Expand All @@ -63,7 +63,7 @@
'ProfileColor',
'ReplyColor',
'SentCodeType',
'StarGiftAttributeType',
'GiftAttributeType',
'StoriesPrivacyRules',
'UserStatus'
]
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from .auto_name import AutoName


class StarGiftAttributeType(AutoName):
"""Star gift attribute type enumeration used in :obj:`~pyrogram.types.StarGiftAttribute`."""
class GiftAttributeType(AutoName):
"""Star gift attribute type enumeration used in :obj:`~pyrogram.types.GiftAttribute`."""

MODEL = raw.types.StarGiftAttributeModel
"Model attribute"
Expand Down
2 changes: 1 addition & 1 deletion pyrogram/enums/message_service_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class MessageServiceType(AutoName):
BOOST_APPLY = auto()
"Boost apply"

STAR_GIFT = auto()
GIFT = auto()
"Star gift"

CONNECTED_WEBSITE = auto()
Expand Down
4 changes: 2 additions & 2 deletions pyrogram/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ async def gift_code_filter(_, __, m: Message):

# region star_gift_filter
async def star_gift_filter(_, __, m: Message):
return bool(m.star_gift)
return bool(m.gift)


star_gift = create(star_gift_filter)
"""Filter messages that contain :obj:`~pyrogram.types.StarGift` objects."""
"""Filter messages that contain :obj:`~pyrogram.types.Gift` objects."""


# endregion
Expand Down
36 changes: 18 additions & 18 deletions pyrogram/methods/payments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@

from .apply_gift_code import ApplyGiftCode
from .check_gift_code import CheckGiftCode
from .convert_star_gift import ConvertStarGift
from .convert_gift import ConvertGift
from .get_payment_form import GetPaymentForm
from .get_star_gifts import GetStarGifts
from .get_user_star_gifts_count import GetUserStarGiftsCount
from .get_user_star_gifts import GetUserStarGifts
from .hide_star_gift import HideStarGift
from .get_available_gifts import GetAvailableGifts
from .get_user_gifts_count import GetUserGiftsCount
from .get_user_gifts import GetUserGifts
from .hide_gift import HideGift
from .send_payment_form import SendPaymentForm
from .send_star_gift import SendStarGift
from .show_star_gift import ShowStarGift
from .transfer_star_gift import TransferStarGift
from .upgrade_star_gift import UpgradeStarGift
from .send_gift import SendGift
from .show_gift import ShowGift
from .transfer_gift import TransferGift
from .upgrade_gift import UpgradeGift

class Payments(
ApplyGiftCode,
CheckGiftCode,
ConvertStarGift,
ConvertGift,
GetPaymentForm,
GetStarGifts,
GetUserStarGiftsCount,
GetUserStarGifts,
HideStarGift,
GetAvailableGifts,
GetUserGiftsCount,
GetUserGifts,
HideGift,
SendPaymentForm,
SendStarGift,
ShowStarGift,
TransferStarGift,
UpgradeStarGift
SendGift,
ShowGift,
TransferGift,
UpgradeGift
):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.


from typing import Union

import pyrogram
from pyrogram import raw


class ConvertStarGift:
async def convert_star_gift(
class ConvertGift:
async def convert_gift(
self: "pyrogram.Client",
message_id: int
) -> bool:
Expand All @@ -43,7 +41,7 @@ async def convert_star_gift(
.. code-block:: python
# Convert gift
app.convert_star_gift(message_id=123)
app.convert_gift(message_id=123)
"""
r = await self.invoke(
raw.functions.payments.ConvertStarGift(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@
from pyrogram import raw, types


class GetStarGifts:
async def get_star_gifts(
class GetAvailableGifts:
async def get_available_gifts(
self: "pyrogram.Client",
) -> List["types.StarGift"]:
) -> List["types.Gift"]:
"""Get all available star gifts that can be sent to other users.
.. include:: /_includes/usable-by/users-bots.rst
Returns:
List of :obj:`~pyrogram.types.StarGift`: On success, a list of star gifts is returned.
List of :obj:`~pyrogram.types.Gift`: On success, a list of star gifts is returned.
Example:
.. code-block:: python
app.get_star_gifts()
app.get_available_gifts()
"""
r = await self.invoke(
raw.functions.payments.GetStarGifts(hash=0)
)

return types.List([await types.StarGift._parse(self, gift) for gift in r.gifts])
return types.List([await types.Gift._parse(self, gift) for gift in r.gifts])
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from pyrogram import raw, types


class GetUserStarGifts:
async def get_user_star_gifts(
class GetUserGifts:
async def get_user_gifts(
self: "pyrogram.Client",
chat_id: Union[int, str],
limit: int = 0,
Expand All @@ -46,12 +46,12 @@ async def get_user_star_gifts(
Maximum amount of star gifts to be returned.
Returns:
``Generator``: A generator yielding :obj:`~pyrogram.types.StarGift` objects.
``Generator``: A generator yielding :obj:`~pyrogram.types.Gift` objects.
Example:
.. code-block:: python
async for gift in app.get_user_star_gifts(chat_id):
async for gift in app.get_user_gifts(chat_id):
print(gift)
"""
peer = await self.resolve_peer(chat_id)
Expand All @@ -76,7 +76,7 @@ async def get_user_star_gifts(
users = {u.id: u for u in r.users}

user_star_gifts = [
await types.StarGift._parse_user_star_gift(self, gift, users)
await types.Gift._parse_user(self, gift, users)
for gift in r.gifts
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
log = logging.getLogger(__name__)


class GetUserStarGiftsCount:
async def get_user_star_gifts_count(
class GetUserGiftsCount:
async def get_user_gifts_count(
self: "pyrogram.Client",
chat_id: Union[int, str]
) -> int:
Expand All @@ -46,7 +46,7 @@ async def get_user_star_gifts_count(
Example:
.. code-block:: python
await app.get_user_star_gifts_count(chat_id)
await app.get_user_gifts_count(chat_id)
"""
peer = await self.resolve_peer(chat_id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from pyrogram import raw


class HideStarGift:
async def hide_star_gift(
class HideGift:
async def hide_gift(
self: "pyrogram.Client",
message_id: int
) -> bool:
Expand All @@ -40,7 +40,7 @@ async def hide_star_gift(
.. code-block:: python
# Hide gift
app.hide_star_gift(message_id=123)
app.hide_gift(message_id=123)
"""
r = await self.invoke(
raw.functions.payments.SaveStarGift(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
from pyrogram import raw, types, enums, utils


class SendStarGift:
async def send_star_gift(
class SendGift:
async def send_gift(
self: "pyrogram.Client",
chat_id: Union[int, str],
star_gift_id: int,
gift_id: int,
text: Optional[str] = None,
parse_mode: Optional["enums.ParseMode"] = None,
entities: Optional[List["types.MessageEntity"]] = None,
hide_my_name: Optional[bool] = None,
pay_for_upgrade: Optional[bool] = None
) -> bool:
"""Send star gift.
Expand All @@ -43,7 +44,7 @@ async def send_star_gift(
For your personal cloud (Saved Messages) you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
star_gift_id (``int``):
gift_id (``int``):
Unique identifier of star gift.
To get all available star gifts use :meth:`~pyrogram.Client.get_star_gifts`.
Expand All @@ -62,14 +63,19 @@ async def send_star_gift(
For userbots only.
Defaults to None.
pay_for_upgrade (``bool``, *optional*):
If True, gift upgrade will be paid from the bot’s balance, thereby making the upgrade free for the receiver.
For bots only.
Defaults to None.
Returns:
``bool``: On success, True is returned.
Example:
.. code-block:: python
# Send gift
app.send_star_gift(chat_id=chat_id, star_gift_id=123)
app.send_gift(chat_id=chat_id, gift_id=123)
"""
peer = await self.resolve_peer(chat_id)

Expand All @@ -80,8 +86,9 @@ async def send_star_gift(

invoice = raw.types.InputInvoiceStarGift(
user_id=peer,
gift_id=star_gift_id,
gift_id=gift_id,
hide_name=hide_my_name,
include_upgrade=pay_for_upgrade,
message=raw.types.TextWithEntities(text=text, entities=entities or []) if text else None
)

Expand Down
Loading

0 comments on commit 788677b

Please sign in to comment.