mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-06 03:47:29 +00:00
Change Message.application type to MessageApplication
Co-authored-by: Danny <1695103+Rapptz@users.noreply.github.com>
This commit is contained in:
parent
96e6261d61
commit
2b9e43dbf9
@ -254,6 +254,17 @@ class Asset(AssetMixin):
|
||||
animated=False,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _from_app_icon(
|
||||
cls, state: _State, object_id: int, icon_hash: str, asset_type: Literal['icon', 'cover_image']
|
||||
) -> Self:
|
||||
return cls(
|
||||
state,
|
||||
url=f'{cls.BASE}/app-icons/{object_id}/{asset_type}.png?size=1024',
|
||||
key=icon_hash,
|
||||
animated=False,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _from_cover_image(cls, state: _State, object_id: int, cover_image_hash: str) -> Self:
|
||||
return cls(
|
||||
|
@ -45,6 +45,7 @@ from typing import (
|
||||
)
|
||||
|
||||
from . import utils
|
||||
from .asset import Asset
|
||||
from .reaction import Reaction
|
||||
from .emoji import Emoji
|
||||
from .partial_emoji import PartialEmoji
|
||||
@ -106,6 +107,7 @@ __all__ = (
|
||||
'MessageInteraction',
|
||||
'MessageReference',
|
||||
'DeletedReferencedMessage',
|
||||
'MessageApplication',
|
||||
)
|
||||
|
||||
|
||||
@ -610,6 +612,51 @@ def flatten_handlers(cls: Type[Message]) -> Type[Message]:
|
||||
return cls
|
||||
|
||||
|
||||
class MessageApplication:
|
||||
"""Represents a message's application data from a :class:`~discord.Message`.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
|
||||
Attributes
|
||||
-----------
|
||||
id: :class:`int`
|
||||
The application ID.
|
||||
description: :class:`str`
|
||||
The application description.
|
||||
name: :class:`str`
|
||||
The application's name.
|
||||
"""
|
||||
|
||||
__slots__ = ('_state', '_icon', '_cover_image', 'id', 'description', 'name')
|
||||
|
||||
def __init__(self, *, state: ConnectionState, data: MessageApplicationPayload) -> None:
|
||||
self._state: ConnectionState = state
|
||||
self.id: int = int(data['id'])
|
||||
self.description: str = data['description']
|
||||
self.name: str = data['name']
|
||||
self._icon: Optional[str] = data['icon']
|
||||
self._cover_image: Optional[str] = data.get('cover_image')
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f'<MessageApplication id={self.id} name={self.name!r}>'
|
||||
|
||||
@property
|
||||
def icon(self) -> Optional[Asset]:
|
||||
"""Optional[:class:`Asset`]: The application's icon, if any."""
|
||||
if self._icon:
|
||||
return Asset._from_app_icon(state=self._state, object_id=self.id, icon_hash=self._icon, asset_type='icon')
|
||||
return None
|
||||
|
||||
@property
|
||||
def cover(self) -> Optional[Asset]:
|
||||
"""Optional[:class:`Asset`]: The application's cover image, if any."""
|
||||
if self._cover_image:
|
||||
return Asset._from_app_icon(
|
||||
state=self._state, object_id=self.id, icon_hash=self._cover_image, asset_type='cover_image'
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
class PartialMessage(Hashable):
|
||||
"""Represents a partial message to aid with working messages when only
|
||||
a message and channel ID are present.
|
||||
@ -1327,16 +1374,12 @@ class Message(PartialMessage, Hashable):
|
||||
|
||||
- ``type``: An integer denoting the type of message activity being requested.
|
||||
- ``party_id``: The party ID associated with the party.
|
||||
application: Optional[:class:`dict`]
|
||||
application: Optional[:class:`~discord.MessageApplication`]
|
||||
The rich presence enabled application associated with this message.
|
||||
|
||||
It is a dictionary with the following keys:
|
||||
.. versionchanged:: 2.0
|
||||
Type is now :class:`MessageApplication` instead of :class:`dict`.
|
||||
|
||||
- ``id``: A string representing the application's ID.
|
||||
- ``name``: A string representing the application's name.
|
||||
- ``description``: A string representing the application's description.
|
||||
- ``icon``: A string representing the icon ID of the application.
|
||||
- ``cover_image``: A string representing the embed's image asset ID.
|
||||
stickers: List[:class:`StickerItem`]
|
||||
A list of sticker items given to the message.
|
||||
|
||||
@ -1409,7 +1452,6 @@ class Message(PartialMessage, Hashable):
|
||||
self.reactions: List[Reaction] = [Reaction(message=self, data=d) for d in data.get('reactions', [])]
|
||||
self.attachments: List[Attachment] = [Attachment(data=a, state=self._state) for a in data['attachments']]
|
||||
self.embeds: List[Embed] = [Embed.from_dict(a) for a in data['embeds']]
|
||||
self.application: Optional[MessageApplicationPayload] = data.get('application')
|
||||
self.activity: Optional[MessageActivityPayload] = data.get('activity')
|
||||
self._edited_timestamp: Optional[datetime.datetime] = utils.parse_time(data['edited_timestamp'])
|
||||
self.type: MessageType = try_enum(MessageType, data['type'])
|
||||
@ -1461,6 +1503,14 @@ class Message(PartialMessage, Hashable):
|
||||
# the channel will be the correct type here
|
||||
ref.resolved = self.__class__(channel=chan, data=resolved, state=state) # type: ignore
|
||||
|
||||
self.application: Optional[MessageApplication] = None
|
||||
try:
|
||||
application = data['application']
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
self.application = MessageApplication(state=self._state, data=application)
|
||||
|
||||
for handler in ('author', 'member', 'mentions', 'mention_roles', 'components'):
|
||||
try:
|
||||
getattr(self, f'_handle_{handler}')(data[handler])
|
||||
@ -1559,7 +1609,8 @@ class Message(PartialMessage, Hashable):
|
||||
self.flags = MessageFlags._from_value(value)
|
||||
|
||||
def _handle_application(self, value: MessageApplicationPayload) -> None:
|
||||
self.application = value
|
||||
application = MessageApplication(state=self._state, data=value)
|
||||
self.application = application
|
||||
|
||||
def _handle_activity(self, value: MessageActivityPayload) -> None:
|
||||
self.activity = value
|
||||
|
@ -4255,6 +4255,14 @@ PartialMessage
|
||||
.. autoclass:: PartialMessage
|
||||
:members:
|
||||
|
||||
MessageApplication
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. attributetable:: MessageApplication
|
||||
|
||||
.. autoclass:: MessageApplication
|
||||
:members:
|
||||
|
||||
Intents
|
||||
~~~~~~~~~~
|
||||
|
||||
|
@ -1229,6 +1229,8 @@ The following changes have been made:
|
||||
|
||||
- :attr:`File.filename` will no longer be ``None``, in situations where previously this was the case the filename is set to `'untitled'`.
|
||||
|
||||
- :attr:`Message.application` will no longer be a raw `dict` of the API payload and now returns an instance of :class:`MessageApplication`.
|
||||
|
||||
:meth:`VoiceProtocol.connect` signature changes.
|
||||
--------------------------------------------------
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user