Skip to content

Commit

Permalink
fix(gateway): handle SUBSCRIPTION_* events correctly (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv authored Jan 28, 2025
1 parent 41553b1 commit d0abe30
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
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 @@ -3282,6 +3282,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 @@ -2005,6 +2006,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 @@ -655,18 +656,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>`

0 comments on commit d0abe30

Please sign in to comment.