Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: premium buttons and entitlement iterator #2490

Merged
merged 11 commits into from
Jul 3, 2024
32 changes: 22 additions & 10 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ async def fetch_skus(self) -> list[SKU]:
data = await self._connection.http.list_skus(self.application_id)
return [SKU(data=s) for s in data]

async def fetch_entitlements(
def entitlements(
self,
user: Snowflake | None = None,
skus: list[Snowflake] | None = None,
Expand All @@ -2053,9 +2053,7 @@ async def fetch_entitlements(
guild: Snowflake | None = None,
exclude_ended: bool = False,
) -> EntitlementIterator:
"""|coro|

Fetches the bot's entitlements.
"""Returns an :class:`.AsyncIterator` that enables fetching the application's entitlements.

.. versionadded:: 2.5
tibue99 marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -2083,24 +2081,38 @@ async def fetch_entitlements(
Whether to limit the fetched entitlements to those that have not ended.
Defaults to ``False``.

Returns
-------
List[:class:`.Entitlement`]
Yields
------
:class:`.Entitlement`
The application's entitlements.

Raises
------
:exc:`HTTPException`
Retrieving the entitlements failed.

Examples
--------

Usage ::

async for entitlement in client.entitlements():
print(entitlement.user_id)

Flattening into a list ::

entitlements = await user.entitlements().flatten()

All parameters are optional.
"""
return EntitlementIterator(
self._connection,
user_id=user.id,
sku_ids=[sku.id for sku in skus],
user_id=user.id if user else None,
sku_ids=[sku.id for sku in skus] if skus else None,
before=before,
after=after,
limit=limit,
guild_id=guild.id,
guild_id=guild.id if guild else None,
exclude_ended=exclude_ended,
)

Expand Down
23 changes: 13 additions & 10 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@
from .flags import SystemChannelFlags
from .integrations import Integration, _integration_factory
from .invite import Invite
from .iterators import AuditLogIterator, BanIterator, MemberIterator
from .iterators import (
AuditLogIterator,
BanIterator,
EntitlementIterator,
MemberIterator,
)
from .member import Member, VoiceState
from .mixins import Hashable
from .monetization import Entitlement
Expand Down Expand Up @@ -4071,19 +4076,17 @@ async def create_test_entitlement(self, sku: Snowflake) -> Entitlement:
data = await self._state.http.create_test_entitlement(self.id, payload)
return Entitlement(data=data, state=self._state)

async def fetch_entitlements(
def entitlements(
self,
skus: list[Snowflake] | None = None,
before: SnowflakeTime | None = None,
after: SnowflakeTime | None = None,
limit: int | None = 100,
exclude_ended: bool = False,
) -> EntitlementIterator:
"""|coro|
"""Returns an :class:`.AsyncIterator` that enables fetching the guild's entitlements.

Fetches this guild's entitlements.

This is identical to :meth:`Client.fetch_entitlements` with the ``guild`` parameter.
This is identical to :meth:`Client.entitlements` with the ``guild`` parameter.

.. versionadded:: 2.6

Expand All @@ -4107,9 +4110,9 @@ async def fetch_entitlements(
Whether to limit the fetched entitlements to those that have not ended.
Defaults to ``False``.

Returns
-------
List[:class:`.Entitlement`]
Yields
------
:class:`.Entitlement`
The application's entitlements.

Raises
Expand All @@ -4119,7 +4122,7 @@ async def fetch_entitlements(
"""
return EntitlementIterator(
self._state,
sku_ids=[sku.id for sku in skus],
sku_ids=[sku.id for sku in skus] if skus else None,
before=before,
after=after,
limit=limit,
Expand Down
2 changes: 1 addition & 1 deletion discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2980,7 +2980,7 @@ def list_entitlements(
if guild_id is not None:
params["guild_id"] = guild_id
if exclude_ended is not None:
params["exclude_ended"] = exclude_ended
params["exclude_ended"] = int(exclude_ended)

r = Route(
"GET",
Expand Down
2 changes: 1 addition & 1 deletion discord/ui/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(
)

self._provided_custom_id = custom_id is not None
if url is None and custom_id is None:
if url is None and custom_id is None and sku_id is None:
custom_id = os.urandom(16).hex()

if url is not None:
Expand Down
16 changes: 7 additions & 9 deletions discord/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,19 +639,17 @@ async def create_test_entitlement(self, sku: discord.abc.Snowflake) -> Entitleme
data = await self._state.http.create_test_entitlement(self.id, payload)
return Entitlement(data=data, state=self._state)

async def fetch_entitlements(
def entitlements(
self,
skus: list[Snowflake] | None = None,
before: SnowflakeTime | None = None,
after: SnowflakeTime | None = None,
limit: int | None = 100,
exclude_ended: bool = False,
) -> EntitlementIterator:
"""|coro|

Fetches this user's entitlements.
"""Returns an :class:`.AsyncIterator` that enables fetching the user's entitlements.

This is identical to :meth:`Client.fetch_entitlements` with the ``user`` parameter.
This is identical to :meth:`Client.entitlements` with the ``user`` parameter.

.. versionadded:: 2.6

Expand All @@ -675,9 +673,9 @@ async def fetch_entitlements(
Whether to limit the fetched entitlements to those that have not ended.
Defaults to ``False``.

Returns
-------
List[:class:`.Entitlement`]
Yields
------
:class:`.Entitlement`
The application's entitlements.

Raises
Expand All @@ -687,7 +685,7 @@ async def fetch_entitlements(
"""
return EntitlementIterator(
self._state,
sku_ids=[sku.id for sku in skus],
sku_ids=[sku.id for sku in skus] if skus else None,
before=before,
after=after,
limit=limit,
Expand Down
Loading