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(gateway): handle SUBSCRIPTION_* events correctly #1275

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1275.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix missing/faulty :class:`Subscription`\-related gateway events.
4 changes: 4 additions & 0 deletions disnake/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3298,6 +3298,10 @@ async def create_entitlement(

Creates a new test :class:`.Entitlement` for the given user or guild, with no expiry.

.. note::
This is only meant to be used with subscription SKUs. To test one-time purchases,
use Application Test Mode.

Parameters
----------
sku: :class:`.abc.Snowflake`
Expand Down
13 changes: 13 additions & 0 deletions disnake/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
from .soundboard import GuildSoundboardSound
from .stage_instance import StageInstance
from .sticker import GuildSticker
from .subscription import Subscription
from .threads import Thread, ThreadMember
from .ui.modal import Modal, ModalStore
from .ui.view import View, ViewStore
Expand Down Expand Up @@ -2007,6 +2008,18 @@ def parse_entitlement_delete(self, data: gateway.EntitlementDelete) -> None:
entitlement = Entitlement(data=data, state=self)
self.dispatch("entitlement_delete", entitlement)

def parse_subscription_create(self, data: gateway.SubscriptionCreate) -> None:
subscription = Subscription(data=data, state=self)
self.dispatch("subscription_create", subscription)

def parse_subscription_update(self, data: gateway.SubscriptionUpdate) -> None:
subscription = Subscription(data=data, state=self)
self.dispatch("subscription_update", subscription)

def parse_subscription_delete(self, data: gateway.SubscriptionDelete) -> None:
subscription = Subscription(data=data, state=self)
self.dispatch("subscription_delete", subscription)

def parse_guild_soundboard_sound_create(self, data: gateway.GuildSoundboardSoundCreate) -> None:
guild_id = utils._get_as_snowflake(data, "guild_id")
guild = self._get_guild(guild_id)
Expand Down
19 changes: 16 additions & 3 deletions disnake/types/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .snowflake import Snowflake, SnowflakeList
from .soundboard import GuildSoundboardSound
from .sticker import GuildSticker
from .subscription import Subscription
from .threads import Thread, ThreadMember, ThreadMemberWithPresence, ThreadType
from .user import AvatarDecorationData, User
from .voice import GuildVoiceState, SupportedModes, VoiceChannelEffect
Expand Down Expand Up @@ -657,18 +658,30 @@ class AutoModerationActionExecutionEvent(TypedDict):
matched_keyword: NotRequired[Optional[str]]


# https://discord.com/developers/docs/monetization/entitlements#new-entitlement
# https://discord.com/developers/docs/events/gateway-events#entitlement-create
EntitlementCreate = Entitlement


# https://discord.com/developers/docs/monetization/entitlements#updated-entitlement
# https://discord.com/developers/docs/events/gateway-events#entitlement-update
EntitlementUpdate = Entitlement


# https://discord.com/developers/docs/monetization/entitlements#deleted-entitlement
# https://discord.com/developers/docs/events/gateway-events#entitlement-delete
EntitlementDelete = Entitlement


# https://discord.com/developers/docs/events/gateway-events#subscription-create
SubscriptionCreate = Subscription


# https://discord.com/developers/docs/events/gateway-events#subscription-update
SubscriptionUpdate = Subscription


# https://discord.com/developers/docs/events/gateway-events#subscription-delete
SubscriptionDelete = Subscription


# https://discord.com/developers/docs/topics/gateway-events#guild-soundboard-sound-create
GuildSoundboardSoundCreate = GuildSoundboardSound

Expand Down
5 changes: 3 additions & 2 deletions docs/api/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1559,10 +1559,11 @@ This section documents events related to Discord chat messages.
:param data: The raw event payload data.
:type data: :class:`RawTypingEvent`

Entitlements
Monetization
~~~~~~~~~~~~

This section documents events related to entitlements, which are used for application subscriptions.
This section documents events related to :ddocs:`monetization <monetization/overview>`,
including application subscriptions and entitlements.

.. function:: on_entitlement_create(entitlement)

Expand Down
11 changes: 9 additions & 2 deletions docs/api/subscriptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
.. currentmodule:: disnake

Subscriptions
===============
=============

This section documents everything related to Subscription(s), which represents a user making recurring payments for at least one SKU.
This section documents everything related to subscriptions, which represent a user making recurring payments for at least one SKU.
See the :ddocs:`official docs <monetization/overview>` for more info.

Discord Models
Expand All @@ -27,3 +27,10 @@ SubscriptionStatus

.. autoclass:: SubscriptionStatus()
:members:

Events
------

- :func:`on_subscription_create(subscription) <disnake.on_subscription_create>`
- :func:`on_subscription_update(subscription) <disnake.on_subscription_update>`
- :func:`on_subscription_delete(subscription) <disnake.on_subscription_delete>`
Loading