Skip to content

Commit ec3cf16

Browse files
fix: premium buttons and entitlement iterator (#2490)
* Fix premium buttons * Fix EntitlementIterator call * Test yarl bool * Add TODO * style(pre-commit): auto fixes from pre-commit.com hooks * Cast `exclude_ended` to int * Fix cast * Adjust fetch_entitlements to match other iterators * Add changelog --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 109a294 commit ec3cf16

File tree

6 files changed

+50
-34
lines changed

6 files changed

+50
-34
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ These changes are available on the `master` branch, but have not yet been releas
2929
- Added `stacklevel` param to `utils.warn_deprecated` and `utils.deprecated`.
3030
([#2450](https://github.com/Pycord-Development/pycord/pull/2450))
3131
- Added support for user-installable applications.
32-
([#2409](https://github.com/Pycord-Development/pycord/pull/2409)
32+
([#2409](https://github.com/Pycord-Development/pycord/pull/2409))
3333
- Added support for one-time purchases for Discord monetization.
3434
([#2438](https://github.com/Pycord-Development/pycord/pull/2438))
3535
- Added `Attachment.title`.
@@ -93,7 +93,10 @@ These changes are available on the `master` branch, but have not yet been releas
9393
`ApplicationCommand.contexts`.
9494
([#2409](https://github.com/Pycord-Development/pycord/pull/2409))
9595
- `Message.interaction` is now deprecated in favor of `Message.interaction_metadata`.
96-
([#2409](https://github.com/Pycord-Development/pycord/pull/2409)
96+
([#2409](https://github.com/Pycord-Development/pycord/pull/2409))
97+
- Replaced `Client.fetch_entitlements` with `Client.entitlements`, which returns an
98+
`EntitlementIterator`.
99+
([#2490](https://github.com/Pycord-Development/pycord/pull/2490))
97100

98101
### Removed
99102

discord/client.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,7 @@ async def fetch_skus(self) -> list[SKU]:
20432043
data = await self._connection.http.list_skus(self.application_id)
20442044
return [SKU(data=s) for s in data]
20452045

2046-
async def fetch_entitlements(
2046+
def entitlements(
20472047
self,
20482048
user: Snowflake | None = None,
20492049
skus: list[Snowflake] | None = None,
@@ -2053,11 +2053,9 @@ async def fetch_entitlements(
20532053
guild: Snowflake | None = None,
20542054
exclude_ended: bool = False,
20552055
) -> EntitlementIterator:
2056-
"""|coro|
2057-
2058-
Fetches the bot's entitlements.
2056+
"""Returns an :class:`.AsyncIterator` that enables fetching the application's entitlements.
20592057
2060-
.. versionadded:: 2.5
2058+
.. versionadded:: 2.6
20612059
20622060
Parameters
20632061
----------
@@ -2083,24 +2081,38 @@ async def fetch_entitlements(
20832081
Whether to limit the fetched entitlements to those that have not ended.
20842082
Defaults to ``False``.
20852083
2086-
Returns
2087-
-------
2088-
List[:class:`.Entitlement`]
2084+
Yields
2085+
------
2086+
:class:`.Entitlement`
20892087
The application's entitlements.
20902088
20912089
Raises
20922090
------
20932091
:exc:`HTTPException`
20942092
Retrieving the entitlements failed.
2093+
2094+
Examples
2095+
--------
2096+
2097+
Usage ::
2098+
2099+
async for entitlement in client.entitlements():
2100+
print(entitlement.user_id)
2101+
2102+
Flattening into a list ::
2103+
2104+
entitlements = await user.entitlements().flatten()
2105+
2106+
All parameters are optional.
20952107
"""
20962108
return EntitlementIterator(
20972109
self._connection,
2098-
user_id=user.id,
2099-
sku_ids=[sku.id for sku in skus],
2110+
user_id=user.id if user else None,
2111+
sku_ids=[sku.id for sku in skus] if skus else None,
21002112
before=before,
21012113
after=after,
21022114
limit=limit,
2103-
guild_id=guild.id,
2115+
guild_id=guild.id if guild else None,
21042116
exclude_ended=exclude_ended,
21052117
)
21062118

discord/guild.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@
6868
from .flags import SystemChannelFlags
6969
from .integrations import Integration, _integration_factory
7070
from .invite import Invite
71-
from .iterators import AuditLogIterator, BanIterator, MemberIterator
71+
from .iterators import (
72+
AuditLogIterator,
73+
BanIterator,
74+
EntitlementIterator,
75+
MemberIterator,
76+
)
7277
from .member import Member, VoiceState
7378
from .mixins import Hashable
7479
from .monetization import Entitlement
@@ -4071,19 +4076,17 @@ async def create_test_entitlement(self, sku: Snowflake) -> Entitlement:
40714076
data = await self._state.http.create_test_entitlement(self.id, payload)
40724077
return Entitlement(data=data, state=self._state)
40734078

4074-
async def fetch_entitlements(
4079+
def entitlements(
40754080
self,
40764081
skus: list[Snowflake] | None = None,
40774082
before: SnowflakeTime | None = None,
40784083
after: SnowflakeTime | None = None,
40794084
limit: int | None = 100,
40804085
exclude_ended: bool = False,
40814086
) -> EntitlementIterator:
4082-
"""|coro|
4087+
"""Returns an :class:`.AsyncIterator` that enables fetching the guild's entitlements.
40834088
4084-
Fetches this guild's entitlements.
4085-
4086-
This is identical to :meth:`Client.fetch_entitlements` with the ``guild`` parameter.
4089+
This is identical to :meth:`Client.entitlements` with the ``guild`` parameter.
40874090
40884091
.. versionadded:: 2.6
40894092
@@ -4107,9 +4110,9 @@ async def fetch_entitlements(
41074110
Whether to limit the fetched entitlements to those that have not ended.
41084111
Defaults to ``False``.
41094112
4110-
Returns
4111-
-------
4112-
List[:class:`.Entitlement`]
4113+
Yields
4114+
------
4115+
:class:`.Entitlement`
41134116
The application's entitlements.
41144117
41154118
Raises
@@ -4119,7 +4122,7 @@ async def fetch_entitlements(
41194122
"""
41204123
return EntitlementIterator(
41214124
self._state,
4122-
sku_ids=[sku.id for sku in skus],
4125+
sku_ids=[sku.id for sku in skus] if skus else None,
41234126
before=before,
41244127
after=after,
41254128
limit=limit,

discord/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2980,7 +2980,7 @@ def list_entitlements(
29802980
if guild_id is not None:
29812981
params["guild_id"] = guild_id
29822982
if exclude_ended is not None:
2983-
params["exclude_ended"] = exclude_ended
2983+
params["exclude_ended"] = int(exclude_ended)
29842984

29852985
r = Route(
29862986
"GET",

discord/ui/button.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def __init__(
117117
)
118118

119119
self._provided_custom_id = custom_id is not None
120-
if url is None and custom_id is None:
120+
if url is None and custom_id is None and sku_id is None:
121121
custom_id = os.urandom(16).hex()
122122

123123
if url is not None:

discord/user.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -639,19 +639,17 @@ async def create_test_entitlement(self, sku: discord.abc.Snowflake) -> Entitleme
639639
data = await self._state.http.create_test_entitlement(self.id, payload)
640640
return Entitlement(data=data, state=self._state)
641641

642-
async def fetch_entitlements(
642+
def entitlements(
643643
self,
644644
skus: list[Snowflake] | None = None,
645645
before: SnowflakeTime | None = None,
646646
after: SnowflakeTime | None = None,
647647
limit: int | None = 100,
648648
exclude_ended: bool = False,
649649
) -> EntitlementIterator:
650-
"""|coro|
651-
652-
Fetches this user's entitlements.
650+
"""Returns an :class:`.AsyncIterator` that enables fetching the user's entitlements.
653651
654-
This is identical to :meth:`Client.fetch_entitlements` with the ``user`` parameter.
652+
This is identical to :meth:`Client.entitlements` with the ``user`` parameter.
655653
656654
.. versionadded:: 2.6
657655
@@ -675,9 +673,9 @@ async def fetch_entitlements(
675673
Whether to limit the fetched entitlements to those that have not ended.
676674
Defaults to ``False``.
677675
678-
Returns
679-
-------
680-
List[:class:`.Entitlement`]
676+
Yields
677+
------
678+
:class:`.Entitlement`
681679
The application's entitlements.
682680
683681
Raises
@@ -687,7 +685,7 @@ async def fetch_entitlements(
687685
"""
688686
return EntitlementIterator(
689687
self._state,
690-
sku_ids=[sku.id for sku in skus],
688+
sku_ids=[sku.id for sku in skus] if skus else None,
691689
before=before,
692690
after=after,
693691
limit=limit,

0 commit comments

Comments
 (0)