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

Add support for messages with type purchase_notification #9906

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class MessageType(Enum):
guild_incident_alert_mode_disabled = 37
guild_incident_report_raid = 38
guild_incident_report_false_alarm = 39
purchase_notification = 44


class SpeakingState(Enum):
Expand Down
61 changes: 61 additions & 0 deletions discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
ClassVar,
Type,
overload,
NamedTuple,
)

from . import utils
Expand Down Expand Up @@ -76,6 +77,7 @@
MessageActivity as MessageActivityPayload,
RoleSubscriptionData as RoleSubscriptionDataPayload,
MessageInteractionMetadata as MessageInteractionMetadataPayload,
PurchaseNotificationResponse as PurchaseNotificationResponsePayload,
)

from .types.interactions import MessageInteraction as MessageInteractionPayload
Expand Down Expand Up @@ -112,6 +114,8 @@
'MessageApplication',
'RoleSubscriptionInfo',
'MessageInteractionMetadata',
'GuildProductPurchase',
'PurchaseNotification',
)


Expand Down Expand Up @@ -837,6 +841,45 @@ def __init__(self, data: RoleSubscriptionDataPayload) -> None:
self.is_renewal: bool = data['is_renewal']


class GuildProductPurchase(NamedTuple):
"""Represents a message's guild product that the user has purchased.

.. versionadded:: 2.5
"""

listing_id: int
"""The ID of the listing that the user has purchased."""
product_name: str
"""The name of the product that the user has purchased."""
Soheab marked this conversation as resolved.
Show resolved Hide resolved


class PurchaseNotification:
"""Represents a message's purchase notification data.

This is currently only attached to messages of type :attr:`MessageType.purchase_notification`.

.. versionadded:: 2.5

Attributes
-----------
guild_product_purchase: Optional[:class:`GuildProductPurchase`]
The guild product purchase that prompted the message.
"""

Soheab marked this conversation as resolved.
Show resolved Hide resolved
__slots__ = ('_type', 'guild_product_purchase')

def __init__(self, data: PurchaseNotificationResponsePayload) -> None:
self._type: int = data['type']

self.guild_product_purchase: Optional[GuildProductPurchase] = None
guild_product_purchase = data.get('guild_product_purchase')
if guild_product_purchase is not None:
Soheab marked this conversation as resolved.
Show resolved Hide resolved
self.guild_product_purchase = GuildProductPurchase(
listing_id=int(guild_product_purchase['listing_id']),
product_name=guild_product_purchase['product_name'],
)


class PartialMessage(Hashable):
"""Represents a partial message to aid with working messages when only
a message and channel ID are present.
Expand Down Expand Up @@ -1762,6 +1805,10 @@ class Message(PartialMessage, Hashable):
The poll attached to this message.

.. versionadded:: 2.4
purchase_notification: Optional[:class:`PurchaseNotification`]
The data of the purchase notification that prompted this :attr:`MessageType.purchase_notification` message.

.. versionadded:: 2.5
"""

__slots__ = (
Expand Down Expand Up @@ -1798,6 +1845,7 @@ class Message(PartialMessage, Hashable):
'position',
'interaction_metadata',
'poll',
'purchase_notification',
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -1925,6 +1973,14 @@ def __init__(
else:
self.role_subscription = RoleSubscriptionInfo(role_subscription)

self.purchase_notification: Optional[PurchaseNotification] = None
try:
purchase_notification = data['purchase_notification']
except KeyError:
pass
else:
self.purchase_notification = PurchaseNotification(purchase_notification)

for handler in ('author', 'member', 'mentions', 'mention_roles', 'components'):
try:
getattr(self, f'_handle_{handler}')(data[handler])
Expand Down Expand Up @@ -2415,6 +2471,11 @@ def system_content(self) -> str:
if self.type is MessageType.guild_incident_report_false_alarm:
return f'{self.author.name} reported a false alarm in {self.guild}.'

if self.type is MessageType.purchase_notification and self.purchase_notification is not None:
guild_product_purchase = self.purchase_notification.guild_product_purchase
if guild_product_purchase is not None:
Soheab marked this conversation as resolved.
Show resolved Hide resolved
return f'{self.author.name} has purchased {guild_product_purchase.product_name}!'

# Fallback for unknown message types
return ''

Expand Down
15 changes: 15 additions & 0 deletions discord/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ class RoleSubscriptionData(TypedDict):
is_renewal: bool


PurchaseNotificationResponseType = Literal[0]
Soheab marked this conversation as resolved.
Show resolved Hide resolved


class GuildProductPurchase(TypedDict):
listing_id: Snowflake
product_name: str


class PurchaseNotificationResponse(TypedDict):
type: PurchaseNotificationResponseType
guild_product_purchase: Optional[GuildProductPurchase]


MessageType = Literal[
0,
1,
Expand Down Expand Up @@ -151,6 +164,7 @@ class RoleSubscriptionData(TypedDict):
37,
38,
39,
44,
]


Expand Down Expand Up @@ -187,6 +201,7 @@ class Message(PartialMessage):
position: NotRequired[int]
role_subscription_data: NotRequired[RoleSubscriptionData]
thread: NotRequired[Thread]
purchase_notification: NotRequired[PurchaseNotificationResponse]


AllowedMentionType = Literal['roles', 'users', 'everyone']
Expand Down
22 changes: 22 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,12 @@ of :class:`enum.Enum`.

.. versionadded:: 2.4

.. attribute:: purchase_notification

The system message sent when a purchase is made in the guild.

.. versionadded:: 2.5

.. class:: UserFlags

Represents Discord User flags.
Expand Down Expand Up @@ -5198,6 +5204,22 @@ RoleSubscriptionInfo
.. autoclass:: RoleSubscriptionInfo
:members:

PurchaseNotification
~~~~~~~~~~~~~~~~~~~~~

.. attributetable:: PurchaseNotification

.. autoclass:: PurchaseNotification
Soheab marked this conversation as resolved.
Show resolved Hide resolved
:members:

GuildProductPurchase
+++++++++++++++++++++

.. attributetable:: GuildProductPurchase()
Soheab marked this conversation as resolved.
Show resolved Hide resolved

.. autoclass:: GuildProductPurchase()
:members:

Intents
~~~~~~~~~~

Expand Down
Loading