Skip to content

Commit

Permalink
Add get_owned_star_count method to return the number of stars owned b…
Browse files Browse the repository at this point in the history
…y the current user account
  • Loading branch information
SpEcHiDe committed Jan 11, 2025
1 parent 1dd095c commit 0870553
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def get_title_list(s: str) -> list:
sell_gift
send_gift
toggle_gift_is_saved
get_owned_star_count
""",
advanced="""
Advanced
Expand Down Expand Up @@ -674,6 +675,7 @@ def get_title_list(s: str) -> list:
RefundedPayment
ShippingQuery
PreCheckoutQuery
StarAmount
"""
)

Expand Down
1 change: 1 addition & 0 deletions docs/source/releases/changes-in-this-fork.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Changes in this Fork
| Scheme layer used: 196 |
+------------------------+

- Added the :meth:`~pyrogram.Client.get_owned_star_count` and a possibly temporary :obj:`~pyrogram.types.StarAmount`.
- Added the :obj:`~pyrogram.types.UpgradedGift` and changed return type :meth:`~pyrogram.Client.get_available_gifts` and :meth:`~pyrogram.Client.get_user_gifts`.
- Added the ``pay_for_upgrade`` in the :meth:`~pyrogram.Client.send_gift`.
- Added the parameters ``upgrade_star_count`` and ``is_for_birthday`` in :obj:`~pyrogram.types.Gift`.
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/methods/business/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .get_payment_form import GetPaymentForm
from .send_payment_form import SendPaymentForm
from .get_available_gifts import GetAvailableGifts
from .get_owned_star_count import GetOwnedStarCount
from .get_user_gifts import GetUserGifts
from .sell_gift import SellGift
from .send_gift import SendGift
Expand All @@ -43,6 +44,7 @@ class TelegramBusiness(
GetPaymentForm,
SendPaymentForm,
GetAvailableGifts,
GetOwnedStarCount,
GetUserGifts,
SellGift,
SendGift,
Expand Down
64 changes: 64 additions & 0 deletions pyrogram/methods/business/get_owned_star_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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 typing import Optional, Union

import pyrogram
from pyrogram import raw, types


class GetOwnedStarCount:
async def get_owned_star_count(
self: "pyrogram.Client",
user_id: Optional[Union[int, str]] = None,
) -> "types.StarAmount":
"""Get the number of Telegram Stars count owned by the current account or the specified bot.
.. include:: /_includes/usable-by/users.rst
Parameters:
user_id (``int`` | ``str``, *optional*):
Unique identifier (int) or username (str) of the bot for which the star count should be returned instead of the current user.
The bot should have ``can_be_edited`` property set to True.
Pass ``None`` to return the count of the current user.
Returns:
:obj:`~pyrogram.types.StarAmount`: On success, the current stars balance is returned.
Example:
.. code-block:: python
# Get stars balance
app.get_stars_balance()
# Get stars balance of a bot owned by the current user
app.get_stars_balance(user_id="pyrogrambot")
"""
if user_id is None:
peer = raw.types.InputPeerSelf()
else:
peer = await self.resolve_peer(user_id)

r = await self.invoke(
raw.functions.payments.GetStarsStatus(
peer=peer
)
)

return types.StarAmount._parse(self, r)
2 changes: 2 additions & 0 deletions pyrogram/types/business/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .shipping_address import ShippingAddress
from .shipping_option import ShippingOption
from .shipping_query import ShippingQuery
from .star_amount import StarAmount
from .successful_payment import SuccessfulPayment
from .refunded_payment import RefundedPayment

Expand All @@ -46,6 +47,7 @@
"ShippingAddress",
"ShippingOption",
"ShippingQuery",
"StarAmount",
"SuccessfulPayment",
"RefundedPayment",
]
57 changes: 57 additions & 0 deletions pyrogram/types/business/star_amount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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/>.

import pyrogram
from pyrogram import raw

from ..object import Object


class StarAmount(Object):
"""This object Describes a possibly non-integer amount of Telegram Stars.
Parameters:
star_count (``int``):
The integer amount of Telegram Stars rounded to 0.
nanostar_count (``int``):
The number of 1/1000000000 shares of Telegram Stars; from -999999999 to 999999999.
"""

def __init__(
self,
*,
star_count: int = None,
nanostar_count: int = None,
):
super().__init__()

self.star_count = star_count
self.nanostar_count = nanostar_count


@staticmethod
def _parse(
client: "pyrogram.Client",
stars_status: "raw.base.payments.StarsStatus"
) -> "StarAmount":
return StarAmount(
star_count=stars_status.balance.amount,
nanostar_count=stars_status.balance.nanos,
)

0 comments on commit 0870553

Please sign in to comment.