Skip to content

Commit

Permalink
feat(client): implement new entitlement endpoint and parameter (#1250)
Browse files Browse the repository at this point in the history
Signed-off-by: Snipy7374 <[email protected]>
  • Loading branch information
Snipy7374 authored Nov 28, 2024
1 parent f2594ee commit d24e1de
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion changelog/1113.feature.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Support application subscriptions and one-time purchases (see the :ddocs:`offici
- New types: :class:`SKU`, :class:`Entitlement`.
- New :attr:`Interaction.entitlements` attribute, and :meth:`InteractionResponse.require_premium` response type.
- New events: :func:`on_entitlement_create`, :func:`on_entitlement_update`, :func:`on_entitlement_delete`.
- New :class:`Client` methods: :meth:`~Client.skus`, :meth:`~Client.entitlements`, :meth:`~Client.create_entitlement`.
- New :class:`Client` methods: :meth:`~Client.skus`, :meth:`~Client.entitlements`, :meth:`~Client.fetch_entitlement`, :meth:`~Client.create_entitlement`.
2 changes: 1 addition & 1 deletion changelog/1186.feature.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Support application subscriptions and one-time purchases (see the :ddocs:`offici
- New types: :class:`SKU`, :class:`Entitlement`.
- New :attr:`Interaction.entitlements` attribute, and :meth:`InteractionResponse.require_premium` response type.
- New events: :func:`on_entitlement_create`, :func:`on_entitlement_update`, :func:`on_entitlement_delete`.
- New :class:`Client` methods: :meth:`~Client.skus`, :meth:`~Client.entitlements`, :meth:`~Client.create_entitlement`.
- New :class:`Client` methods: :meth:`~Client.skus`, :meth:`~Client.entitlements`, :meth:`~Client.fetch_entitlement`, :meth:`~Client.create_entitlement`.
5 changes: 5 additions & 0 deletions changelog/1249.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Support application subscriptions and one-time purchases (see the :ddocs:`official docs <monetization/overview>` for more info).
- New types: :class:`SKU`, :class:`Entitlement`.
- New :attr:`Interaction.entitlements` attribute, and :meth:`InteractionResponse.require_premium` response type.
- New events: :func:`on_entitlement_create`, :func:`on_entitlement_update`, :func:`on_entitlement_delete`.
- New :class:`Client` methods: :meth:`~Client.skus`, :meth:`~Client.entitlements`, :meth:`~Client.fetch_entitlement`, :meth:`~Client.create_entitlement`.
34 changes: 34 additions & 0 deletions disnake/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3147,6 +3147,7 @@ def entitlements(
guild: Optional[Snowflake] = None,
skus: Optional[Sequence[Snowflake]] = None,
exclude_ended: bool = False,
exclude_deleted: bool = True,
oldest_first: bool = False,
) -> EntitlementIterator:
"""Retrieves an :class:`.AsyncIterator` that enables receiving entitlements for the application.
Expand Down Expand Up @@ -3186,6 +3187,8 @@ def entitlements(
The SKUs for which entitlements are retrieved.
exclude_ended: :class:`bool`
Whether to exclude ended/expired entitlements. Defaults to ``False``.
exclude_deleted: :class:`bool`
Whether to exclude deleted entitlements. Defaults to ``True``.
oldest_first: :class:`bool`
If set to ``True``, return entries in oldest->newest order. Defaults to ``False``.
Expand All @@ -3209,9 +3212,40 @@ def entitlements(
guild_id=guild.id if guild is not None else None,
sku_ids=[sku.id for sku in skus] if skus else None,
exclude_ended=exclude_ended,
exclude_deleted=exclude_deleted,
oldest_first=oldest_first,
)

async def fetch_entitlement(self, entitlement_id: int, /) -> Entitlement:
"""|coro|
Retrieves a :class:`.Entitlement` for the given ID.
.. note::
This method is an API call. To get the entitlements of the invoking user/guild
in interactions, consider using :attr:`.Interaction.entitlements`.
.. versionadded:: 2.10
Parameters
----------
entitlement_id: :class:`int`
The ID of the entitlement to retrieve.
Raises
------
HTTPException
Retrieving the entitlement failed.
Returns
-------
:class:`.Entitlement`
The retrieved entitlement.
"""
data = await self.http.get_entitlement(self.application_id, entitlement_id=entitlement_id)
return Entitlement(data=data, state=self._connection)

async def create_entitlement(
self, sku: Snowflake, owner: Union[abc.User, Guild]
) -> Entitlement:
Expand Down
14 changes: 14 additions & 0 deletions disnake/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2365,10 +2365,12 @@ def get_entitlements(
guild_id: Optional[Snowflake] = None,
sku_ids: Optional[SnowflakeList] = None,
exclude_ended: bool = False,
exclude_deleted: bool = False,
) -> Response[List[entitlement.Entitlement]]:
params: Dict[str, Any] = {
"limit": limit,
"exclude_ended": int(exclude_ended),
"exclude_deleted": int(exclude_deleted),
}
if before is not None:
params["before"] = before
Expand All @@ -2386,6 +2388,18 @@ def get_entitlements(
)
return self.request(r, params=params)

def get_entitlement(
self, application_id: Snowflake, entitlement_id: int
) -> Response[entitlement.Entitlement]:
return self.request(
Route(
"GET",
"/applications/{application_id}/entitlements/{entitlement_id}",
application_id=application_id,
entitlement_id=entitlement_id,
)
)

def create_test_entitlement(
self,
application_id: Snowflake,
Expand Down
4 changes: 4 additions & 0 deletions disnake/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,7 @@ def __init__(
before: Optional[Union[Snowflake, datetime.datetime]] = None,
after: Optional[Union[Snowflake, datetime.datetime]] = None,
exclude_ended: bool = False,
exclude_deleted: bool = True,
oldest_first: bool = False,
) -> None:
if isinstance(before, datetime.datetime):
Expand All @@ -1059,6 +1060,7 @@ def __init__(
self.guild_id: Optional[int] = guild_id
self.sku_ids: Optional[List[int]] = sku_ids
self.exclude_ended: bool = exclude_ended
self.exclude_deleted: bool = exclude_deleted

self.state: ConnectionState = state
self.request = state.http.get_entitlements
Expand Down Expand Up @@ -1116,6 +1118,7 @@ async def _before_strategy(self, retrieve: int) -> List[EntitlementPayload]:
user_id=self.user_id,
guild_id=self.guild_id,
exclude_ended=self.exclude_ended,
exclude_deleted=self.exclude_deleted,
)

if len(data):
Expand All @@ -1133,6 +1136,7 @@ async def _after_strategy(self, retrieve: int) -> List[EntitlementPayload]:
user_id=self.user_id,
guild_id=self.guild_id,
exclude_ended=self.exclude_ended,
exclude_deleted=self.exclude_deleted,
)

if len(data):
Expand Down

0 comments on commit d24e1de

Please sign in to comment.